Perl实现拖拽的例子
来源:互联网 发布:js弹出输入框 并取值 编辑:程序博客网 时间:2024/05/23 07:23
#!/usr/bin/perl -w
use Tk;
use Tk::DragDrop;
use Tk::DropSite;
use Tk::HList;
use strict;
use vars qw($top $f $lb_src $lb_dest $dnd_token $drag_entry);
$top = new MainWindow;
$top->Label(-text => "Drag items from the left HList to the right one"
)->pack;
$f = $top->Frame->pack;
$lb_src = $f->Scrolled('HList', -scrollbars => "osoe", -selectmode => 'dragdrop')
->pack(-side => "left");
$lb_dest = $f->Scrolled('HList', -scrollbars => "osoe", -selectmode => 'dragdrop')
->pack(-side => "left");
my $i=0;
foreach (sort keys %ENV) {
$lb_src->add($i++, -text => ___FCKsi___0
);
}
# Define the source for drags.
# Drags are started while pressing the left mouse button and moving the
# mouse. Then the StartDrag callback is executed.
$dnd_token = $lb_src->DragDrop
(-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub { StartDrag($dnd_token) },
);
# Define the target for drops.
$lb_dest->DropSite
(-droptypes => ['Local'],
-dropcommand => [ /&Drop, $lb_dest, $dnd_token ],
);
MainLoop;
sub StartDrag {
my($token) = @_;
my $w = $token->parent; # $w is the source hlist
my $e = $w->XEvent;
$drag_entry = $w->nearest($e->y); # get the hlist entry under cursor
if (defined $drag_entry) {
# Configure the dnd token to show the hlist entry
$token->configure(-text => $w->entrycget($drag_entry, '-text'));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
}
}
# Accept a drop and insert a new item in the destination hlist and delete
# the item from the source hlist
sub Drop {
my($lb, $dnd_source) = @_;
my $end = ($lb->info("children"))[-1];
my @pos = (-after => $end) if defined $end;
my $y = $lb->pointery - $lb->rooty;
my $nearest = $lb->nearest($y);
if (defined $nearest) {
my(@bbox) = $lb->infoBbox($nearest);
if ($y > ($bbox[3]-$bbox[1])/2+$bbox[1]) {
@pos = (-after => $nearest);
} else {
@pos = (-before => $nearest);
}
}
$lb->add($drag_entry, @pos, -text => $dnd_source->cget(-text));
$lb_src->delete("entry", $drag_entry);
$lb->see($drag_entry);
}
__END__
use Tk;
use Tk::DragDrop;
use Tk::DropSite;
use Tk::HList;
use strict;
use vars qw($top $f $lb_src $lb_dest $dnd_token $drag_entry);
$top = new MainWindow;
$top->Label(-text => "Drag items from the left HList to the right one"
)->pack;
$f = $top->Frame->pack;
$lb_src = $f->Scrolled('HList', -scrollbars => "osoe", -selectmode => 'dragdrop')
->pack(-side => "left");
$lb_dest = $f->Scrolled('HList', -scrollbars => "osoe", -selectmode => 'dragdrop')
->pack(-side => "left");
my $i=0;
foreach (sort keys %ENV) {
$lb_src->add($i++, -text => ___FCKsi___0
___FCKpd___0
);
}
# Define the source for drags.
# Drags are started while pressing the left mouse button and moving the
# mouse. Then the StartDrag callback is executed.
$dnd_token = $lb_src->DragDrop
(-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub { StartDrag($dnd_token) },
);
# Define the target for drops.
$lb_dest->DropSite
(-droptypes => ['Local'],
-dropcommand => [ /&Drop, $lb_dest, $dnd_token ],
);
MainLoop;
sub StartDrag {
my($token) = @_;
my $w = $token->parent; # $w is the source hlist
my $e = $w->XEvent;
$drag_entry = $w->nearest($e->y); # get the hlist entry under cursor
if (defined $drag_entry) {
# Configure the dnd token to show the hlist entry
$token->configure(-text => $w->entrycget($drag_entry, '-text'));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
}
}
# Accept a drop and insert a new item in the destination hlist and delete
# the item from the source hlist
sub Drop {
my($lb, $dnd_source) = @_;
my $end = ($lb->info("children"))[-1];
my @pos = (-after => $end) if defined $end;
my $y = $lb->pointery - $lb->rooty;
my $nearest = $lb->nearest($y);
if (defined $nearest) {
my(@bbox) = $lb->infoBbox($nearest);
if ($y > ($bbox[3]-$bbox[1])/2+$bbox[1]) {
@pos = (-after => $nearest);
} else {
@pos = (-before => $nearest);
}
}
$lb->add($drag_entry, @pos, -text => $dnd_source->cget(-text));
$lb_src->delete("entry", $drag_entry);
$lb->see($drag_entry);
}
__END__
- Perl实现拖拽的例子
- perl的一个例子
- perl 引用的例子
- Perl DBI模块的例子
- perl入门的小例子
- perl的中文字符集转换的例子
- HTML5拖拽实现的小例子
- 一个perl操作excel的例子
- perl入门的一个小例子
- perl Data::Dumper和Storable的例子
- perl 实现的自定义grep
- perl实现的插入排序
- perl程序例子
- Perl Socket 简单例子
- perl bless例子
- perl 继承小例子
- 一个简单的perl函数调用的例子
- perl 一个简单的面向对象的例子
- adodb+smarty+myClass 结合-数据类的智能操作
- 从数据集更新数据库
- 程序设计-游戏
- PerlTidy的中文问题[转]
- C语言之四书五经
- Perl实现拖拽的例子
- PythonCard中的一段代码...
- WinCE中基于Media Player的多媒体开发
- MapPoint+SmartPhone+C#开发示例
- 正式登陆CSDN blog
- 设计模式
- 哦,是这样的
- Windows Media 元数据使用指南
- boost新版本推出:1.33.1