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 に答える