熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

編程必學:控件的拖動和縮放技術全解

2022-06-13   來源: .NET編程 

  光標的指示
  當光標移動到要拖動或縮放的控件上時應顯示相應的動作箭頭定義一個枚舉的變量來標識對應的狀態
  
  Enum EnumMousePointPosition
  
  MouseSizeNone =
  
  MouseSizeRight = 拉伸右邊框
  
  MouseSizeLeft = 拉伸左邊框
  
  MouseSizeBottom = 拉伸下邊框
  
  MouseSizeTop = 拉伸上邊框
  
  MouseSizeTopLeft = 拉伸左上角
  
  MouseSizeTopRight = 拉伸右上角
  
  MouseSizeBottomLeft = 拉伸左下角
  
  MouseSizeBottomRight = 拉伸右下角
  
  MouseDrag = 鼠標拖動
  
  End Enum
  
  Dim m_MousePointPosition As EnumMousePointPosition
  
  在MouseMove事件中讀取進入控件的光標位置用下面函數判斷光標的狀態
  
  m_MousePointPosition = MousePointPosition(senderSize e)
  
  按對應的狀態顯示光標形狀
  
  Select Case m_MousePointPosition
  
  Case EnumMousePointPositionMouseSizeNone
  
  MeCursor = CursorsArrow    箭頭
  
  Case EnumMousePointPositionMouseDrag
  
  MeCursor = CursorsSizeAll   四方向
  
  Case EnumMousePointPositionMouseSizeBottom
  
  MeCursor = CursorsSizeNS   南北
  
  Case EnumMousePointPositionMouseSizeTop
  
  MeCursor = CursorsSizeNS   南北
  
  Case EnumMousePointPositionMouseSizeLeft
  
  MeCursor = CursorsSizeWE   東西
  
  Case EnumMousePointPositionMouseSizeRight
  
  MeCursor = CursorsSizeWE   東西
  
  Case EnumMousePointPositionMouseSizeBottomLeft
  
  MeCursor = CursorsSizeNESW  東北到南西
  
  Case EnumMousePointPositionMouseSizeBottomRight
  
  MeCursor = CursorsSizeNWSE  東南到西北
  
  Case EnumMousePointPositionMouseSizeTopLeft
  
  MeCursor = CursorsSizeNWSE  東南到西北
  
  Case EnumMousePointPositionMouseSizeTopRight
  
  MeCursor = CursorsSizeNESW  東北到南西
  
  End Select
  
  如果光標離開控件MouseMove事件將不響應因此用需用MouseLeave事件來接力完成光標狀態的標識和顯示
  
  Private Sub MyMouseLeave(ByVal sender As Object ByVal e As SystemEventArgs)
  
  m_MousePointPosition = EnumMousePointPositionMouseSizeNone
  
  MeCursor = CursorsArrow
  
  End Sub
  
  拖動
  拖動的原理是移動控件的Location到當前光標的位置並保持控件的高度和寬度不變如果記錄起始光標點為p當前光標點為e那麼x方向的移動距離是eX pXx方向的移動距離是eY pY
  
  在鼠標按下時即是拖動的開始在MouseDown事件中記錄光標開始拖動點
  
  p = New Point(eX eY)
  
  在MouseMove事件檢測是否持續按著鼠標左鍵如果是則更新控件的起點位置
  
  If eButton = MouseButtonsLeft Then
  
  senderLocation = New Point(senderLeft + eX pX senderTop + eY pY)
  
  End If
  
  縮放
  縮放的原理和拖動類似如果縮放底邊就是根據光標的移動改變控件的高度而不改變控件的起點位置
  
  同樣如果縮放底邊在鼠標按下時即是縮放的開始在MouseDown事件中記錄光標開始拖動點
  
  p = New Point(eX eY)
  
  在MouseMove事件檢測是否持續按著鼠標左鍵如果是則更新控件的高度
  
  If eButton = MouseButtonsLeft Then
  
  senderSize = New Size(senderWidth senderHeight + eY pY)
  
  p = New Point(eX eY)  記錄光標拖動的當前點
  
  End If
  
  這裡和拖動有些區別就是高度的變化會導致光標位置數值的變化因此需重新用p定位光標起始點的位置所有涉及控件底邊和右邊的縮放都要這樣處理而左邊和頂邊則不用
  
  其它所有各方向的拖動都可以類推
  
  委派
  所有的操作一共總結為三個函數MyMouseDownMyMouseMoveMyMouseLeave
  
  動態創建控件後需委派相應的事件到這三個函數如果創建一個按鈕
  
  Dim Button As New Button
  
  ControlsAdd(Button)
  
  AddHandler ButtonMouseDown AddressOf MyMouseDown
  
  AddHandler ButtonMouseMove AddressOf MyMouseMove
  
  AddHandler ButtonMouseLeave AddressOf MyMouseLeave
  
  源碼
  建立一個窗體放置一個按鈕點擊該按鈕創建動態建立一個可以拖動和縮放的按鈕全部源碼如下
  
  Public Class Form
  
  Inherits SystemWindowsFormsForm
  
  Enum EnumMousePointPosition
  
  MouseSizeNone =
  
  MouseSizeRight = 拉伸右邊框
  
  MouseSizeLeft = 拉伸左邊框
  
  MouseSizeBottom = 拉伸下邊框
  
  MouseSizeTop = 拉伸上邊框
  
  MouseSizeTopLeft = 拉伸左上角
  
  MouseSizeTopRight = 拉伸右上角
  
  MouseSizeBottomLeft = 拉伸左下角
  
  MouseSizeBottomRight = 拉伸右下角
  
  MouseDrag = 鼠標拖動
  
  End Enum
  
  Dim m_MousePointPosition As EnumMousePointPosition
  
  Dim p p As Point
  
  Private Sub MyMouseDown(ByVal sender As Object ByVal e As SystemWindowsFormsMouseEventArgs)
  
  p = New Point(eX eY)  記錄光標開始拖動點
  
  p = New Point(eX eY)
  
  End Sub
  
  Private Sub MyMouseLeave(ByVal sender As Object ByVal e As SystemEventArgs)
  
  m_MousePointPosition = EnumMousePointPositionMouseSizeNone
  
  MeCursor = CursorsArrow
  
  End Sub
  
  Private Sub MyMouseMove(ByVal sender As Object ByVal e As SystemWindowsFormsMouseEventArgs)
  
  If eButton = MouseButtonsLeft Then
  
  Select Case m_MousePointPosition
  
  Case EnumMousePointPositionMouseDrag
  
  senderLocation = New Point(senderLeft + eX pX senderTop + eY pY)
  
  Case EnumMousePointPositionMouseSizeBottom
  
  senderSize = New Size(senderWidth senderHeight + eY pY)
  
  p = New Point(eX eY)  記錄光標拖動的當前點
  
  Case EnumMousePointPositionMouseSizeBottomRight
  
  senderSize = New Size(senderWidth + eX pX senderHeight + eY pY)
  p = New Point(eX eY)
  
  Case EnumMousePointPositionMouseSizeRight
  
  senderSize = New Size(senderWidth + eX pX senderHeight)
  
  p = New Point(eX eY)
  
  Case EnumMousePointPositionMouseSizeTop
  
  senderLocation = New Point(senderLeft senderTop + (eY pY))
  
  senderSize = New Size(senderWidth senderHeight (eY pY))
  
  Case EnumMousePointPositionMouseSizeLeft
  
  senderLocation = New Point(senderLeft + eX pX senderTop)
  
  senderSize = New Size(senderWidth (eX pX) senderHeight)
  
  Case EnumMousePointPositionMouseSizeBottomLeft
  
  senderLocation = New Point(senderLeft + eX pX senderTop)
  
  senderSize = New Size(senderWidth (eX pX) senderHeight + eY pY)
  p = New Point(eX eY)
  
  Case EnumMousePointPositionMouseSizeTopRight
  
  senderLocation = New Point(senderLeft senderTop + (eY pY))
  
  senderSize = New Size(senderWidth + (eX pX) senderHeight (eY pY))
  p = New Point(eX eY)
  
  Case EnumMousePointPositionMouseSizeTopLeft
  
  senderLocation = New Point(senderLeft + eX pX senderTop + (eY pY))
  
  senderSize = New Size(senderWidth (eX pX) senderHeight (eY pY))
  
  End Select
  
  Else
  
  m_MousePointPosition = MousePointPosition(senderSize e) 判斷光標的位置狀態
  
  Select Case m_MousePointPosition  改變光標
  

From:http://tw.wingwit.com/Article/program/net/201311/13842.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.