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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.