On 13/06/07, Brian Rowlands (Greymouth High School)
<[EMAIL PROTECTED]> wrote:
I've a design with 8 radio buttons named rb7, rb8, … 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 = Win32::GUI::DialogBox->new(
   -size => [400,300],
   -onTimer => \&who_has_focus,
);

$mw->AddRadioButton(
   -name => "rb$_",
   -top  => $_ * 20,
   -left => 10,
   -text => 'Something',
   -tabstop => 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 = 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 = 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 = $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 =~ /^rb/;
   }

   return;
}
__END__

#!perl -w
use strict;
use warnings;

use Win32::GUI();

my $mw = Win32::GUI::DialogBox->new(
   -size => [400,300],
);

$mw->AddRadioButton(
   -name       => "rb$_",
   -top        => $_ * 20,
   -left       => 10,
   -text       => 'Something',
   -tabstop    => 1,
   -notify     => 1,    # Required (see BN_SETFOCUS on MSDN)
   -onGotFocus => \&has_focus,
) for (1..8);

$mw->Show();
Win32::GUI::Dialog();
$mw->Hide();
exit(0);

sub has_focus {
   my ($self) = @_;

   # 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 = $self->{-name};
   print "$name got focus.\n";

   return;
}
__END__

Reply via email to