QuestionSolvedHelp Moving Borderless Window

Posts 111 of 11 · Page 1 of 1
You mean move the controls around on the form after runtime?
You have to provide a image of it in order for it to be approved.

From what I see, you want to know how to move the form when you uses like none on the formstyle.

Something I found on the net:
Code:
Public Class Form1
    'Declare the variables
    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        drag = True 'Sets the variable drag to true.
        mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'If drag is set to true then move the form accordingly.
        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
        drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
    End Sub
End Class
@Jorndel
A far cleaner and better solution would be to use the form mouse down event and WndProc.
Code:
    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Capture = False
        WndProc(Message.Create(Handle, &HA1, New IntPtr(2), IntPtr.Zero))
    End Sub
Thats all the code needed to move a borderless form.
The other method works just fine aswell but alot more messing about.
I'v used both methods in the past, depends on my situation.
Thanks for the replies.

But would the code you've mentioned help me drag the window since I used none as a form design.
Yes both codes will let you drag it. And if you wish to resize the form aswell, this will do it
Code:
    Protected Overrides Sub WndProc(ByRef Msg As Message)
        If (Msg.Msg = &H84) Then
            Dim _Point As Point = PointToClient(New Point(CInt(Msg.LParam.ToInt64 + &HFFFF), CInt((Msg.LParam.ToInt64 + &HFFFF0000) >> &H10)))
            Dim _Size As Size = ClientSize
            If (_Point.X >= _Size.Width - &H10 AndAlso _Point.Y >= _Size.Height - &H10 AndAlso _Size.Height >= &H10) Then
                Msg.Result = If(IsMirrored, New IntPtr(&H10), New IntPtr(&H11))
                Return
            End If
        End If
        MyBase.WndProc(Msg)
    End Sub
Dragging the bottom right corner of the form allows you to resize.
Thanks Pingo!It works nicely.

---------- Post added at 09:07 PM ---------- Previous post was at 08:58 PM ----------

Btw,do you guys happen to know how to map a function to a certain keyboard key?Like mapping "ENTER" with the equal sign for the above. o-o[COLOR="Silver"]
Quote Originally Posted by calvin839 View Post
Thanks Pingo!It works nicely.

---------- Post added at 09:07 PM ---------- Previous post was at 08:58 PM ----------

Btw,do you guys happen to know how to map a function to a certain keyboard key?Like mapping "ENTER" with the equal sign for the above. o-o[COLOR="Silver"]
Try to add an keydown event for the button?
And do a check. e.KeyData = Keys.Enter

And on the form set this to True: Key Preview
Hey dumbfucks, draw on a Form control. Set the FormBorderStyle to "None". Winner winner chicken dinner.
Try this code:

This lets you Maximize the window as well.

Code:
#Region "Move Form"
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region
Code:
Public Class Form1
    Private MausLocation As Point

    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
            MausLocation = e.Location
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Location = e.Location - MausLocation + Me.Location
        End If
    End Sub
End Class
I think that works
Posts 111 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Need help?