Reputation: 1453
When calling Perl Win32::GUI::BrowseForFolder()
, the desktop (naturally) has several earlier windows on it. It turns out that the BrowseForFolder()
window hides behind those windows. This is sometimes confusing and annoying, because you may not realize the program is asking you for a folder name, and you may think there is a bug. (This is in difference from (for example) Win32::GUI::GetOpenFileName()
or GetSaveFileName()
, which do pop up at the foreground!).
My question is: how to make BrowseForFolder()
come to the foreground?
One possibility is to find its handle, and then raise it to the foreground.
Note that -addexstyle => WS_EX_TOPMOST
doesn't have an effect (see below).
Here is a complete (compileable and running) code example. (5.014 is not essential)
use strict;
use warnings;
use 5.014;
use Win32::GUI();
use Win32::GUI qw{ WS_EX_TOPMOST};
my ($InputDir, $TopDir, $InputFileName, $dw, $dh, $desktop, $Window);
$TopDir = 'D:\My documents'; # Change this to an existing direcotry of yours
$desktop = Win32::GUI::GetDesktopWindow();
$dw = Win32::GUI::Width($desktop);
$dh = Win32::GUI::Height($desktop);
$Window = Win32::GUI::Window->new( -name => 'main', -text => 'Main window',
-pos => [20/100*$dw, 20/100*$dh], -size => [50/100*$dw, 60/100*$dh],
-onTerminate => \&TerminateWindow,
-addexstyle => WS_EX_TOPMOST, -dialogui => 1, -tabstop => 1, -cancel => 1, );
$Window -> AddButton ( -name => 'ButtonCommit', -pos => [10,10],
-size =>[16/100*$dw,3.5/100*$dh], -text => 'Commit changes', -onClick => \&Commit);
sub Commit {
$InputDir = Win32::GUI::BrowseForFolder( -root => $TopDir, -includefiles => 1,
-title => 'Select directory for parameter file',
-text =>'Selext directory for parameter file',
-size => [50/100*$dw, 50/100*$dh], -addexstyle => WS_EX_TOPMOST,);
$InputFileName = Win32::GUI::GetOpenFileName( -title => 'Select the input file',
-directory => $InputDir, -file => "\0" . " " x 256,
-filter => ["Text files (*.txt)" => "*.txt", "All files", "*.*", ],
-text => 'text Select input file');
} # end sub Commit
$Window ->Show();
Win32::GUI::Dialog();
sub TerminateWindow {
return -1;
}
Click the "Commit changes" button, then minimize the main window, and only then you see the "hiding" BrowseForFolder()
window.
Note that the BrowseForFolder()
window hides behind other windows, and you need to minimize other windows to get to it. All other Perl Win32::GUI
windows do open up at the foreground.
Furthermore, note that -addexstyle => WS_EX_TOPMOST
doesn't have an effect, while it usually does in Perl Win32::GUI
windows.
Note: change the D:\My documents
to an existing directory.
Related question: How does one find the desktop windows handles? If we could find BrowseForFolder()
's handle, we could raise it to the foreground.
(Note: similar question cross-posted at PerlMonks: http://perlmonks.org/?node_id=994815)
Upvotes: 0
Views: 967
Reputation: 9697
If you remove
-addexstyle => WS_EX_TOPMOST,
from main window (and also from the Browser), it jumps to foreground. The flag is used for windows that should be on top no matter what - typically some widgets.
Upvotes: 3