1

我使用此代码来最大化和恢复我的自定义表单。但是当窗体最大化时,它仍然可以拖动,我使用计时器来拖动窗体。

Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click, lblTitle.MouseDoubleClick
    Dim maximizeHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
    Dim maximizeWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
    Dim maximizeLocation As Point = New Point(0, 0)
    Dim fullscreen As Boolean = False

    If Me.Height = maximizeHeight Or Me.Width = maximizeWidth Or Me.Location = maximizeLocation Then
        fullscreen = True
    Else
        fullscreen = False
    End If

    If fullscreen = True Then
        Me.Size = New Size(1000, 500)
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
    ElseIf fullscreen = False Then
        Me.Location = New Point(0, 0)
        Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
    End If
End Sub

Private Sub pnlBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseDown
    MoveTmr.Start()
    refpositions()
End Sub

Private Sub MoveTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveTmr.Tick
    Me.Location = oloc - ocur + System.Windows.Forms.Cursor.Position
    refpositions()
End Sub

Private Sub pnlBar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseUp
    MoveTmr.Stop()
    refpositions()
End Sub

Private Sub RszTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles RszTmr.Tick
    Me.Size = appSize - curPos + Cursor.Position
    refpositions()
End Sub
4

3 回答 3

1

制作:

Dim fullscreen As Boolean = False

一个类变量。

然后编辑此代码以适合您的变量:

Private Sub Mover_Tick(sender As System.Object, e As System.EventArgs) Handles Mover.Tick
    If fullscreen = false Then
         Dim pt As New Point((Me.Location.X + (MousePosition.X - mPosX)), (Me.Location.Y + (MousePosition.Y - mPosY)))
         Me.Location = pt
         mPosX = MousePosition.X
         mPosY = MousePosition.Y
    End If
End Sub

编辑:

也实现这个:

Private Sub Title_StartDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseDown
    mPosX = MousePosition.X
    mPosY = MousePosition.Y
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Mover.Start()
    End If
End Sub

Private Sub Title_StopDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseUp
    Mover.Stop()
End Sub

你也可能想通过说 Me.WindowState = FormWindowState.Maximized 来使它更简单

于 2012-10-20T16:41:05.097 回答
0

我使用 Mousedown、Mouseup 和 Mousemove 事件来移动我的表单。

Public Class Form1

Private Is_Dragged As Boolean = False
Private M_DownX As Integer
Private M_DownY As Integer

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown

    If e.Button = MouseButtons.Left Then
        Is_Dragged = True
        M_DownX = e.X
        M_DownY = e.Y
    End If

End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp

    If e.Button = MouseButtons.Left Then
        Is_Dragged = False
    End If

End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove

    If Is_Dragged Then
        Dim tmp_pnt As Point = New Point()

        tmp_pnt.X = Me.Location.X + (e.X - M_DownX)
        tmp_pnt.Y = Me.Location.Y + (e.Y - M_DownY)
        Me.Location = tmp_pnt
        tmp_pnt = Nothing

    End If

End Sub

End Class

并最大化我的形式。

Private Sub Btn_Main_Max_Click(sender As Object, e As EventArgs) Handles Btn_Main_Max.Click

    Static IsAlreadyResized As Boolean

    If Not IsAlreadyResized Then
        Me.WindowState = FormWindowState.Maximized
        IsAlreadyResized = True
        Exit Sub
    End If

    If IsAlreadyResized Then
        Me.WindowState = FormWindowState.Normal
        IsAlreadyResized = False

    End If

 End Sub
于 2017-11-07T14:25:45.367 回答
-2

您可以在代码中或视觉上创建一个面板并放置 dock.top 然后您可以在侧面或中间放置一个标签,并在窗口左侧创建一个图片框创建关闭最小化和最大化或其他按钮并添加上面的朋友代码通过标题栏移动表单,就像在 Windows 中一样

于 2021-03-06T13:30:06.853 回答