Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: "Always on top" with Win32 and Tk

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


in reply to "Always on top" with Win32 and Tk

There are third party programs you can run that can alter properties of existing windows to force them on top. Nice to keep a copy of notepad or something open on top of everything else. One that I used would allow you to shell out and execute it and it would update the window of the program it was called from.

While not a perl solution, it's close...

Replies are listed 'Best First'.
Re: Re: "Always on top" with Win32 and Tk
by elwarren (Priest) on May 09, 2001 at 23:35 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!

      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.

      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://79210]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-06-16 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.