【问题标题】:Form with custom title bar & formborderstyle = None remains draggable when maximized具有自定义标题栏和 formborderstyle = None 最大化时保持可拖动的表单
【发布时间】:2012-10-09 21:42:46
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net custom-controls draggable maximize


    【解决方案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

    【讨论】:

    • 我在一些程序中做过这种事情,但我没有最大化按钮。
    【解决方案2】:

    我使用 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
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多