Hi,

I want to remember the object that has the focus, so the next time the user
switches to the application, the focus is set to the last object that had
the focus when the application was put in the background (with alt+tab).

I don't know if I have used the right method, but here is what I've done:

I have set a -onGotFocus event for each object, and in the method that
handles this event I have set a global var $last_focus in which I put the
handle of that object.
In the Win_Activate method, I have put the program to SetFocus() to the
object that has the handle from $last_focus.

Well, it works pretty fine, but the issue is that not all the objects have
an -onGotFocus event, or this is what it seems to be.
Or am I doing something wrong?

Here is a test program that shows that the Textfield has that event, but the
Button object doesn't have it.
I have also noticed that the AxWindow object doesn't have this event either.

(I have also put that method to print a MessageBox, for seeing this easier).

use strict;
use Win32::GUI;

my $last_focus;

my $Win = Win32::GUI::Window->new(
-name => "Win",
-text => "Test",
-size => [400, 300],
-dialogui => 1,
-onActivate => \&Win_Activate,
);

my $Label = $Win->AddLabel(
-name => "Label",
-text => "Name",
-pos => [0, 0],
-size => [50, 0],
);

my $Edit = $Win->AddTextfield(
-name => "Edit",
-pos => [50, 0],
-size => [100, 20],
-tabstop => 1,
-onGotFocus => \&Edit_GotFocus,
);

my $Button = $Win->AddButton(
-name => "Button",
-text => "Ok",
-pos => [150, 0],
-size => [50, 20],
-tabstop => 1,
-onGotFocus => \&Button_GotFocus,
);

$Win->Show();
Win32::GUI::Dialog();

sub Win_Activate {
Win32::GUI::SetFocus($last_focus) if $last_focus;
}

sub Edit_GotFocus {
my $self = shift;
$last_focus = $self->GetFocus();
}

sub Button_GotFocus {
my $self = shift;
Win32::GUI::MessageBox($self, "button", "but");
$last_focus = $self->GetFocus();
}

Teddy


Reply via email to