HyperMesh二次开发技术—鼠标调整插件界面位置功能开发(二)
1.代码实例
#新建过程subWindow
proc subWindow {} {
toplevel .subWindow -background {black}
wm overrideredirect .subWindow true
wm attribute .subWindow -topmost true
wm geometry .subWindow 296x120+300+250
frame .subWindow.f -relief flat
label .subWindow.f.label01 -text "" -width 1 -height 5 -background {black}
grid .subWindow.f.label01 -column 0 -row 0 -padx 2 -sticky ew
labelframe .subWindow.f.part01 -text "workPath" -width 25 -height 100
label .subWindow.f.part01.label00 -text "HomeDir" -font {arial 8 bold} -width 8
label .subWindow.f.part01.label10 -text "" -width 20 -height 2 -relief groove -borderwidth 4
button .subWindow.f.part01.button -text "Select" -width 5 -font {arial 8 bold}
grid .subWindow.f.part01.label00 -column 0 -row 0 -pady 18 -padx 2 -sticky ew
grid .subWindow.f.part01.label10 -column 1 -row 0 -pady 18 -padx 2 -sticky ew
grid .subWindow.f.part01.button -column 1 -row 1 -pady 5 -padx 4 -sticky e
grid .subWindow.f.part01 -column 1 -row 0 -padx 3 -pady 1 -sticky ew
labelframe .subWindow.f.part02 -text "Import/Export" -width 23 -height 100
button .subWindow.f.part02.button00 -text "Import" -width 5 -font {arial 8 bold}
button .subWindow.f.part02.button01 -text "Export" -width 5 -font {arial 8 bold}
button .subWindow.f.part02.button02 -text "-Back-" -width 5 -font {arial 8 bold} -command {destroy .subWindow}
grid .subWindow.f.part02.button00 -column 0 -row 0 -pady 5 -padx 2 -sticky ew
grid .subWindow.f.part02.button01 -column 0 -row 1 -pady 5 -padx 2 -sticky ew
grid .subWindow.f.part02.button02 -column 0 -row 2 -pady 5 -padx 2 -sticky ew
grid .subWindow.f.part02 -column 2 -row 0 -padx 3 -pady 1 -sticky ew
pack .subWindow.f -padx 0 -pady 1
global Dx Dy nextId
set nextId 0
set Dx 0
set Dy 0
set Mx 0
set My 0
#绑定鼠标左键,当鼠标左键被按下执行"{}"内脚本
bind .subWindow.f.label01 <ButtonPress-1> {
set Mx %X;
set My %Y;
set relPosit [mouRelSubPosit $Mx $My];
set Dx [lindex $relPosit 0];
set Dy [lindex $relPosit 1];
set nextId 1
}
#绑定鼠标光标,当光标移动执行"{}"内脚本
bind .subWindow.f.label01 <Motion> {
set Mx %X;
set My %Y;
set windPosit [windowPosit $Mx $My $Dx $Dy];
if {$nextId == 1} {
wm geometry .subWindow +[lindex $windPosit 0]+[lindex $windPosit 1]
}
}
#绑定鼠标,当鼠标被释放执行"{}"内脚本
bind .subWindow.f.label01 <ButtonRelease> {
set nextId 0
break
}
}
#新建过程,计算光标位置与界面左顶点X与Y方向距离
proc mouRelSubPosit {Mx My} {
upvar #1 Mx mx
upvar #1 My my
set relPosit ""
set SizePosit [wm geometry .subWindow]
set PositX [lindex [split $SizePosit +] 1]
set PositY [lindex [split $SizePosit +] 2]
set dx [expr $mx-$PositX]
set dy [expr $my-$PositY]
set relPosit [concat $relPosit $dx $dy]
return $relPosit
}
#新建过程,计算界面新位置坐标
proc windowPosit {Mx My Dx Dy} {
upvar #1 Mx mx
upvar #1 My my
upvar #1 Dx dx
upvar #1 Dy dy
set windPosit ""
set windPositX [expr $mx-$dx]
set windPositY [expr $my-$dy]
set windPosit [concat $windPosit $windPositX $windPositY]
return $windPosit
}
#调用subWindow过程
subWindow
2.代码解析
(1) 绑定鼠标左键,当鼠标左键被按下执行"{}"内脚本
bind .subWindow.f.label01 <ButtonPress-1> {
#将光标x坐标值赋给变量Mx
set Mx %X;
#将光标y坐标值赋给变量My
set My %Y;
#调用mouRelSubPosit过程,返回光标位置与界面左顶点的距离数列赋给变量relPosit,0位置为x方向距离,1位置为y方向 #距离
set relPosit [mouRelSubPosit $Mx $My];
#数列"relPosit" 0 位置数值赋给变量Dx
set Dx [lindex $relPosit 0];
#数列"relPosit" 0 位置数值赋给变量Dy
set Dy [lindex $relPosit 1];
#改变nextId,用于辨识是否改变界面位置
set nextId 1;
}
#新建mouRelSubPosit过程,功能:返回光标位置与界面左顶点的距离数列
proc mouRelSubPosit {Mx My} {
upvar #1 Mx mx
upvar #1 My my
set relPosit ""
#获取界面尺寸及位置信息,赋值给变量SizePosit
set SizePosit [wm geometry .subWindow]
#获得界面x坐标信息,赋值给变量PositX
set PositX [lindex [split $SizePosit +] 1]
#获得界面y坐标信息,赋值给变量PositY
set PositY [lindex [split $SizePosit +] 2]
#光标位置与界面左顶点的x方向距离,赋值给变量dx
set dx [expr $mx-$PositX]
#光标位置与界面左顶点的y方向距离,赋值给变量dy
set dy [expr $my-$PositY]
#将dx及dy连接成数列relPosit
set relPosit [concat $relPosit $dx $dy]
#将数列返回
return $relPosit
}
(2) 绑定鼠标光标,当光标移动执行"{}"内脚本
bind .subWindow.f.label01 <Motion> {
#将光标x坐标值赋给变量Mx
set Mx %X;
#将光标y坐标值赋给变量My
set My %Y;
#调用windowPosit过程,返回界面的新位置坐标数列,0位置为x方向坐标,1位置为y方向坐标
set windPosit [windowPosit $Mx $My $Dx $Dy];
#更改界面位置
if {$nextId == 1} {
wm geometry .subWindow +[lindex $windPosit 0]+[lindex $windPosit 1]
}
}
#新建过程,计算界面新位置坐标
proc windowPosit {Mx My Dx Dy} {
upvar #1 Mx mx
upvar #1 My my
upvar #1 Dx dx
upvar #1 Dy dy
set windPosit ""
#计算界面x方向位置值
set windPositX [expr $mx-$dx]
#计算界面y方向位置值
set windPositY [expr $my-$dy]
#将 windPositX及windPositY连接成数列windPosit
set windPosit [concat $windPosit $windPositX $windPositY]
#将数列返回
return $windPosit
}
(3) 绑定鼠标,当鼠标被释放执行"{}"内脚本
bind .subWindow.f.label01 <ButtonRelease> {
#恢复辨识码nextId
set nextId 0
#跳出绑定
break
}
(4) bind命令创建绑定
bind "组件" <事件> {执行脚本}
<事件>:Key或者KeyPress—按下按键
KeyRelease—释放按键
Button或者ButtonPress—按下鼠标
ButtonRelease—释放鼠标键
Enter—移动鼠标到组件内
Leave—从组件上移开鼠标光标
Motion—在某个组件内,将鼠标光标移到另一个点
MouseWheel—用户移动鼠标滚轮
FocusIn—组件接收键盘焦点
FocusOut—组件失去键盘焦点
Configure—在开始时显示组件,或者改变它的尺寸、位置或边缘宽度
Map—组件可见
Unmap—组件不再可见
Destroy—删除组件
3.启动插件,利用鼠标调整界面位置
案例源码进群:756716776下载