Aaaarggg!!! I started poking around in GUI.xs, looking for a solution to my double event problem, and found the mother of all undocumented features:

True modal dialog boxes in Win32::GUI. Well very close anyway...

I _always_ thought this wasn't possible. Heck, I even wrote Win32::GUI::Modalizer just to hack (and it is a sorry hack) around that absent feature.

In the event that this is common knowledge and I'm the only one who didn't get this obvious thing, I have already prepared a big "Doh!" for myself :) If not, this is how to do it.

Test the script below.

What I found was that Win32::GUI::Dialog accepts a hwnd parameter. So you don't have to call it like this:

Win32::GUI::Dialog();

you can also call it like this:

$win->Dialog();

and then the event loop is restricted to $win, which means other windows don't get processed.

There are still a few things not 100% correct with this:

- Your windows aren't really connected to the modal window. This is only noticable when you switch between other applications and your GUI program, in that they don't get moved on top of the other application when your modal window gets focus again.

- There seems to be some problem with mouse events when clicking in the main window, and then clicking on the title bar on your modal window (the modal window jumps to a new location relative to the previous click. this is obviously wrong).

Apart from that it seems to work. This goes for 0.0.558. Can someone confirm this behaviour with 0.0.665?


In brief:

- Call it like $win->Dialog();
- In the Terminate event, or a close-button Click event, Hide() the modal window and return -1 to break out of the main loop. The modal window isn't destroyed.

- You can also make a MessageBox modal to a window like this:
$winModeless->MessageBox("Clicked the Test button", "Test");



#!/usr/local/bin/perl -w

use strict;
use Win32::GUI;

my $winModeless = new Win32::GUI::Window(
        -left   => 13,
        -top    => 32,
        -width  => 439,
        -height => 260,
        -name   => "winModeless",
        -text   => "Win32::GUI::Modalizer Synopsis [1]"
        );
$winModeless->AddButton(
        -text    => "&Open",
        -name    => "btnModelessOpen",
        -left    => 358,
        -top     => 207,
        -width   => 70,
        -height  => 21,
        );
$winModeless->AddButton(
        -text    => "&Test",
        -name    => "btnModelessTest",
        -left    => 358,
        -top     => 20,
        -width   => 70,
        -height  => 21,
        );

my $winModeless2 = new Win32::GUI::Window(
        -left   => 100,
        -top    => 100,
        -width  => 439,
        -height => 260,
        -name   => "winModeless2",
        -text   => "Win32::GUI::Modalizer Synopsis [2]"

        );

my $winDialog = new Win32::GUI::DialogBox(
        -left   => 50,
        -top    => 50,
        -width  => 439,
        -height => 260,
        -name   => "winDialog",
        -text   => "Dialog Box",
        );
$winDialog->AddButton(
        -text    => "&Ok",
        -name    => "btnDialogOk",
        -left    => 358,
        -top     => 207,
        -width   => 70,
        -height  => 21,
        );


$winModeless->Show();
$winModeless2->Show();



#Go modal
Win32::GUI::Dialog();


##Event handlers

#Modeless window
sub winModeless_Terminate {
    return(-1);
    }
sub btnModelessOpen_Click {
        $winDialog->Show();
        $winDialog->Dialog();
    return(1);
    }
sub btnModelessTest_Click {
    $winModeless->MessageBox("Clicked the Test button", "Test Button");
    return(1);
    }

#Modeless2 window
sub winModeless2_Terminate {
    return(-1);
    }

#Dialog window
sub winDialog_Terminate {
    $winDialog->Hide();
    return -1;
    }
sub btnDialogOk_Click {
        $winDialog->Hide();
        print "Ok\n";
    return(-1);
    }

__END__


/J
-------- ------ ---- --- -- --  --  -    -     -      -         -
Johan Lindström    Sourcerer @ Boss Casinos     [EMAIL PROTECTED]

Latest bookmark: "XEmacs XEmacs and Supporting Libraries and Prog..."
http://xemacs.org/Download/win32/
dmoz: /Computers/Open_Source/Software/Editors/ 98



Reply via email to