in reply to Re: Re: "Always on top" with Win32 and Tk
in thread "Always on top" with Win32 and Tk

I've got it working now! As before I've removed most of the code from my program.

use Tk; use Win32::API; ... # Windows constants my ($OnTop, $NoTop, $Top) = (-1, -2, 0); my ($SWP_NOMOVE, $SWP_NOSIZE) = (2, 1); # 'always on top' state my $isOnTop = $NoTop; ... # Create a Win32::API object for SetWindowPos my $SetWindowPos = new Win32::API("user32", "SetWindowPos", [N,N,N,N,N +,N,N], N); # and one for FindWindow my $FindWindow = new Win32::API("user32", "FindWindow", [P,P], N); ... my $w = new MainWindow; ... my $aotCBut = $w->Checkbutton(-text => "Always on top", -onvalue => $OnTop, -offvalue => $NoTop, -variable => \$isOnTop, -command => sub { # get a handle to the toplevel window containing our Perl/Tk a +pp my $class = "TkTopLevel"; my $name = $w->title; my $NULL = 0; my $topHwnd = $FindWindow->Call($class, $name); if ($topHwnd != $NULL) { # change 'always on top' state $SetWindowPos->Call($topHwnd, $isOnTop, 0, 0, 0, 0, $SWP_N +OMOVE | $SWP_NOSIZE); }; }) ->pack(-side => "bottom", expand => "yes", -fill => "x"); ... # start the UI MainLoop;

The Win32 FindWindow API call will return a handle to a window with the specified title and class. Perl/Tk creates windows of the class TkTopLevel. You must call this after the window has been mapped (i.e. after MainLoop), otherwise you won't get a valid window handle.

A useful reference when using Win32::API is here, it talks about Visual Basic but you can fairly easily convert it to Perl.

Kevin O'Rourke

Replies are listed 'Best First'.
Re^4: "Always on top" with Win32 and Tk
by donato (Novice) on Oct 29, 2004 at 04:23 UTC
    Hi, I'm kind of a newbie in perl but i noticed your code had some trouble running on my pc... the corrections made were in the creation of the api SetWindowPos object which now becomes Win32::API->new('user32', 'SetWindowPos', ['N','N','N','N','N','N','N'], 'N'); or you might as well code it [NNNNNNN], N, i think it will work (havent tested...) and the creation of the FindWindow obj also: Win32::API->new('user32', 'FindWindow', ['P','P'], 'N'); again [PP], N... that made it work on my pc... anyway, thanks for the code!