VB.NET移动动态嵌入无边框形式

时间:2012-05-07 04:07:33

标签: vb.net forms dynamic drag-and-drop move

从下面提供的代码和标题中可以看出,我在试图弄清楚如何在VB.NET中移动这种动态嵌入的无边框形式时遇到了麻烦。

Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
        Dim WidgetForm As Form
        WidgetForm = New Form()
        WidgetForm.ShowInTaskbar = False
        WidgetForm.TopMost = True
        WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
        WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
        WidgetForm.Show()

        opac = Me.OpacInt.Text
        WidgetForm.Opacity = opac

        sizew = Me.WidgetW.Text
        sizey = Me.WidgetY.Text
        WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
    End Sub

我过去曾经多次使用过这种方法(下面的代码),它似乎总是适用于移动表单,但我不确定如何将它应用于动态嵌入表单。

非常感谢任何帮助。

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - Me.Left
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    If drag Then
        Me.Top = Windows.Forms.Cursor.Position.Y - mousey
        Me.Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    Timer1.Enabled = False
    drag = False
End Sub

1 个答案:

答案 0 :(得分:1)

动态表单的执行方式与普通表单相同。首先,您需要在创建动态表单时为其添加一些事件

Public WidgetForm As Form

Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click

    WidgetForm = New Form()
    WidgetForm.ShowInTaskbar = False
    WidgetForm.TopMost = True
    WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
    WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
    WidgetForm.Show()

    opac = Me.OpacInt.Text
    WidgetForm.Opacity = opac

    sizew = Me.WidgetW.Text
    sizey = Me.WidgetY.Text
    WidgetForm.Size = New System.Drawing.Size(sizew, sizey)

    'Add the event here
    AddHandler WidgetForm.MouseDown, AddressOf WidgetForm_MouseDown
    AddHandler WidgetForm.MouseMove, AddressOf WidgetForm_MouseMove
    AddHandler WidgetForm.MouseUp, AddressOf WidgetForm_MouseUp
End Sub

然后您可以通过事件处理程序

移动表单
Private Sub WidgetForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - CType(sender, Form).Left
        mousey = Windows.Forms.Cursor.Position.Y - CType(sender, Form).Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub WidgetForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If drag Then
        CType(sender, Form).Top = Windows.Forms.Cursor.Position.Y - mousey
        CType(sender, Form).Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub WidgetForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 
    Timer1.Enabled = False
    drag = False
End Sub