1

正如您从下面提供的代码和标题中看到的那样,我在试图弄清楚如何在 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
4

1 回答 1

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
于 2012-05-07T04:24:52.883 回答