表格带有自定义标题栏& formborderstyle =无法在最大化时保持可拖动状态

时间:2012-10-19 09:40:47

标签: vb.net custom-controls draggable maximize

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

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<code>

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:-2)

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