From: Robert M. <rob...@us...> - 2007-06-14 00:06:53
|
On 13/06/07, Brian Rowlands (Greymouth High School) <Row...@gr...> wrote: > I've a design with 8 radio buttons named rb7, rb8, =85 rb14. What I want = to do > is determine which one has the focus. Can someone pease help me find an > efficient way to do this please? Two was that I can think to do it. First calls GetFocus(); Second catches the radio button getting the focus. Regards, Rob. #!perl -w use strict; use warnings; use Win32::GUI(); my $mw =3D Win32::GUI::DialogBox->new( -size =3D> [400,300], -onTimer =3D> \&who_has_focus, ); $mw->AddRadioButton( -name =3D> "rb$_", -top =3D> $_ * 20, -left =3D> 10, -text =3D> 'Something', -tabstop =3D> 1, ) for (1..8); $mw->AddTimer('T1', 1000); $mw->Show(); Win32::GUI::Dialog(); $mw->Hide(); exit(0); sub who_has_focus { # GetFocus() gives us the window handle of the # window in the current thread that has focus, # or 0 if there isn't one. my $hwnd_focus =3D Win32::GUI::GetFocus(); # GetWindowObject() retrieves the perl object # from the window handle - this function is documented # as INTERNAL, but is the easiest way to go from a # window handle back to the object - if you don't want to # use this internal function, then it would be possible to # walk the object tree to find the object with that handle, # but that would require understanding the internal object # structure anyway ... my $win_focus =3D Win32::GUI::GetWindowObject($hwnd_focus); if ($win_focus) { # Finally we pull the name from the object - we shouldn't # access the internals of the object like this, but there # is no GetName() method. my $name =3D $win_focus->{-name}; # and we need to check that the name is one of the ones # we want, as there could be other controls on the window # that could have focus print "$name has focus.\n" if $name =3D~ /^rb/; } return; } __END__ #!perl -w use strict; use warnings; use Win32::GUI(); my $mw =3D Win32::GUI::DialogBox->new( -size =3D> [400,300], ); $mw->AddRadioButton( -name =3D> "rb$_", -top =3D> $_ * 20, -left =3D> 10, -text =3D> 'Something', -tabstop =3D> 1, -notify =3D> 1, # Required (see BN_SETFOCUS on MSDN) -onGotFocus =3D> \&has_focus, ) for (1..8); $mw->Show(); Win32::GUI::Dialog(); $mw->Hide(); exit(0); sub has_focus { my ($self) =3D @_; # pull the name from the object - we shouldn't # access the internals of the object like this, but there # is no GetName() method. my $name =3D $self->{-name}; print "$name got focus.\n"; return; } __END__ |