HyperMesh二次開發技術—鼠標調整插件UI界面位置

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.啟動插件,利用鼠標調整界面位置

HyperMesh二次開發01.png

HyperMesh二次開發技術—鼠標調整插件UI界面位置的圖2

HyperMesh二次開發技術—鼠標調整插件UI界面位置的圖3

案例源碼進群:756716776下載

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

2
1