Win32::GUI doesn't allow you to destroy controls yourself, but if you really need to, you can remove the reference to the control from it's parent Window object and any other references to the control, if any. Example:
#!perl
use strict;
use warnings;
use feature qw(:5.12);
use Win32::GUI qw();
use Win32::GUI::Constants qw(CW_USEDEFAULT);
my $win = Win32::GUI::Window->new(
-name => 'win',
-text => 'Destroy Window',
-size => [ 320, 240 ],
-left => CW_USEDEFAULT,
);
my $label = $win->AddLabel(
-name => 'label',
-text => 'Label',
-size => [ 80, 25 ],
-pos => [ 10, 10 ],
);
my $button = $win->AddButton(
-name => 'button',
-text => 'Remove',
-size => [ 80, 25 ],
-pos => [ 10, 50 ],
-onClick => sub {
state $show = 1;
if($show){
undef $label; # remove stored reference to cont
+rol
delete $win->{label}; # remove reference stored in pare
+nt window
$show = 0;
}
else {
$label = $win->AddLabel( # recreate control
-name => 'label',
-text => 'Label',
-size => [ 80, 25 ],
-pos => [ 10, 10 ],
);
$show = 1;
}
return 1;
},
);
$win->Show();
Win32::GUI::Dialog();
__END__
This usually isn't the best idea. As mentioned by zentara above, it's usually best to just show and hide the control instead. Example:
#!perl
use strict;
use warnings;
use feature qw(:5.12);
use Win32::GUI qw();
use Win32::GUI::Constants qw(CW_USEDEFAULT);
my $win = Win32::GUI::Window->new(
-name => 'win',
-text => 'Destroy Window',
-size => [ 320, 240 ],
-left => CW_USEDEFAULT,
);
my $label = $win->AddLabel(
-name => 'label',
-text => 'Label',
-size => [ 80, 25 ],
-pos => [ 10, 10 ],
);
my $button = $win->AddButton(
-name => 'button',
-text => 'Remove',
-size => [ 80, 25 ],
-pos => [ 10, 50 ],
-onClick => sub {
if($label->IsVisible()){
$label->Hide();
}
else {
$label->Show();
}
return 1;
},
);
$win->Show();
Win32::GUI::Dialog();
__END__
|