Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: "Always on top" with Win32 and Tk

by elwarren (Priest)
on May 09, 2001 at 23:35 UTC ( [id://79214]=note: print w/replies, xml ) Need Help??


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

A quick Google Search gives me this snippet of VB code. You could possibly do this with the assistance of a Win32::API guru?

http://home.newsfactory.net/~DataGhost/VBing/code/Windows%20Operations/alwaysontop.htm

HTH
  • Comment on Re: Re: "Always on top" with Win32 and Tk

Replies are listed 'Best First'.
Re: Re: Re: "Always on top" with Win32 and Tk
by kevin_i_orourke (Friar) on May 11, 2001 at 15:12 UTC

    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
    
      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!
Re: Re: Re: "Always on top" with Win32 and Tk
by kevin_i_orourke (Friar) on May 10, 2001 at 17:43 UTC

    I tried the VB code, converted to Perl and using Win32::API. (the code isn't the tidiest in the world, I've missed out most of the program):

    ... use Tk; use Win32::API; # Windows constants my ($OnTop, $NoTop, $Top) = (-1, -2, 0); my ($SWP_NOMOVE, $SWP_NOSIZE) = (2, 1); ... # Create a Win32::API object for SetWindowPos my $SetWindowPos = new Win32::API("user32", "SetWindowPos", [N,N,N,N,N +,N,N], N); ... my $w = new MainWindow; ... # do the 'always on top' bit my $hwnd = $w->frame; # frame returns a hex value as a string, e.g. "0 +x1234" print "$hwnd\n"; $SetWindowPos->Call(hex($hwnd), $OnTop, 0, 0, 0, 0, $SWP_NOMOVE | $SWP +_NOSIZE); MainLoop;

    Unfortunately $w->frame returns the Tk MainWindow, not the Win32 window containing it. As a result setting $w->frame 'always on top' doesn't have any effect.

    Anyone know how to get an HWND for a window's parent out of Win32? Tk::Wm's $w->wrapper doesn't work either.

    --
    Kevin O'Rourke
    
      Anyone know how to get an HWND for a window's parent out of Win32?
      Try the GetParent API call.
Re: Re: Re: "Always on top" with Win32 and Tk
by kevin_i_orourke (Friar) on May 10, 2001 at 16:13 UTC

    Thanks, I've had a look at the VB. I assume if I can extract an HWND from Tk I could use this. It's a bit nasty but looks like the only way.

    From my reading of Tk::Wm documentation I should be able to get the HWND using $w->frame. Time for a play with Win32::API

    --
    Kevin O'Rourke
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://79214]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-06-23 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.