vbnet对象拖动/调整大小代码

时间:2013-07-29 04:35:02

标签: vb.net resize drag

我写了一些代码来拖动和调整窗体上的任何文本框,而在'dragmode'中这是一个正在发生的事情的gif,而不是正确拖动文本框,

img

代码:

#Region "Texbox Dragging"
    Private txt As TextBox
    Private txtptX, txtptY As Integer
    Private txtdrag As Boolean
    Private txtresize As Boolean
    Private Sub txt_MouseLeave(sender As Object, e As EventArgs)
  Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub txt_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  If DragMode = True Then
    If e.Button = MouseButtons.Left Then
    txtdrag = True
    txtresize = True
    txt = CType(sender, TextBox)
    txtptX = e.X : txtptY = e.Y
    End If
  End If
    End Sub

    Private Sub txt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

  If txtdrag = True Then
    txt.Location = New Point(txt.Location.X + e.X - txtptX, txt.Location.Y + e.Y - txtptY)
    Me.Refresh()
    txtdrag = True
  End If
  If txtresize = True Then
    txtdrag = False
    If txt.Cursor = Cursors.Cross Then
    txt.Width = e.X
    txt.Height = e.Y
    Else
    If e.X >= txt.Width - 10 Then
    txt.Cursor = Cursors.Cross
    Else
    txt.Cursor = Cursors.IBeam
    End If

    If e.Y >= txt.Height - 10 Then
    txt.Cursor = Cursors.Cross
    Else
    txt.Cursor = Cursors.IBeam
    End If
    End If
  End If
    End Sub

    Private Sub txt_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  txt = CType(sender, TextBox)
  If txt.Cursor = Cursors.Cross Then
    txt.Cursor = Cursors.IBeam
  End If

  txtdrag = False
  txtresize = False
    End Sub
#End Region

我为一个混乱的程序员道歉,但这是关于尝试12,我正在努力使其成功...

  • 他们都独立工作但是我得到了这个奇怪的错误,我只能在他们在一起时拖动0.25秒......

2 个答案:

答案 0 :(得分:1)

两个动作(调整大小和移动)不能同时执行(在MouseMove方法中):拖动部分(顶部的一个)与调整大小重叠。另一方面,您不希望两个动作同时发生(用户如何仅通过单击和移动来处理调整大小和重定位?)。您必须设置另一个条件以允许两个功能并行工作;例如:双击txt

全球旗帜:

Dim nowDragging As Boolean = True

双击事件,为这些值分配正确的值:

Private Sub txt_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles txt.MouseDoubleClick
    nowDragging = Not nowDragging
End Sub

更新MouseMove以解释他们:

If txtdrag = True And nowDragging Then 
'...
If txtresize = True And Not nowDragging Then
'..

此代码默认执行拖动;如果用户双击txt,则此功能将转换为调整大小(等等)。这是一个简单的方法,只需获得基本思路:启用从一种功能转移到另一种功能的方法。

PS:很好地展示你的确切问题。

答案 1 :(得分:1)

我做到了!在大声朗读代码并遵循流程之后,我意识到你的陈述中你是正确的,我不能在MouseMove事件中同时包含拖动和调整大小。但是:-),我可以在MouseDown事件中同时使用,而在正在发生时,另一个不能被触发,它可以完美地工作。下面是代码的.gif以及代码!

.gif没有像我一样记录,你只需要接受我的话哈哈。

#Region "Texbox Dragging"
Private txt As TextBox
Private txtptX, txtptY As Integer
Private Sub txt_MouseLeave(sender As Object, e As EventArgs)
    Me.Cursor = Cursors.Arrow
End Sub
Dim MoveMode As Boolean
Private Sub txt_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If DragMode = True Then
        MoveMode = True
        If e.Button = MouseButtons.Left Then
            txt = CType(sender, TextBox)
            txtptX = e.X : txtptY = e.Y
            If e.X >= txt.Width - 10 Then
                txt.Cursor = Cursors.Cross
            Else
                txt.Cursor = Cursors.IBeam
            End If

            If e.Y >= txt.Height - 10 Then
                txt.Cursor = Cursors.Cross
            Else
                txt.Cursor = Cursors.IBeam
            End If
        End If
    End If
End Sub

Private Sub txt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    If MoveMode = True Then
        If txt.Cursor = Cursors.Cross Then
            txt.Width = e.X
            txt.Height = e.Y
        Else
    txt.Location = New Point(txt.Location.X + e.X - txtptX, txt.Location.Y + e.Y - txtptY)
            Me.Refresh()
        End If
    End If
End Sub

Private Sub txt_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    txt = CType(sender, TextBox)
    If txt.Cursor = Cursors.Cross Then
        txt.Cursor = Cursors.IBeam
    End If
    MoveMode = False
End Sub
#End Region