pic box mouse down move form

i've seen quite a few examples showing the ms override form mouse down example for moving a form. i need to perform the same task but by mouse down on a picture box.

Specifically when a user clicks down on the picture box (while the mouse is still down) i would like the the user to be able to keep moving the form until released.

I'm looking for something similar to the override. specifically i would like to override the the click event of a specific picture box (i'd rather it not be all picture boxes though).

Thanks for your help.


LVL 1
TheRoyalFalconAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Javert93Connect With a Mentor Commented:
An easier way would be to use the Win32 API:

    Public Declare Auto Function ReleaseCapture Lib "user32.dll" () As Integer
    Public Declare Ansi Function MoveForm Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, Optional ByVal Message As Integer = WM_NCLBUTTONDOWN, Optional ByVal HitTest As Integer = HTCAPTION, Optional ByVal wParam As Integer = 0) As Integer

    Public Const WM_NCLBUTTONDOWN As Integer = &HA1
    Public Const HTCAPTION As Integer = &H2

    Public Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        ReleaseCapture()
        MoveForm(Me.Handle)
    End Sub

This code should perform better and reduce any "flickering" that might occur.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private startX As Integer
    Private startY As Integer

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        startX = e.X
        startY = e.Y
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
            Me.Location = New Point(Me.Left + e.X - startX, Me.Top + e.Y - startY)
        End If
    End Sub

End Class
0
 
TheRoyalFalconAuthor Commented:
Idle_Mind

This does allow me to move the form but it causes a lot for flickering if there is much in the manner of controls on the form. But thank you very much for the idea, as it was worth a shot.

Javert93

I feel a little silly. I’ve used something similar to this API call in my VB6 apps for a long time. When I got started down the vb.net route I ran into a problem with one of my regular api calls from my api grab bag. Thereafter I guess I got in the mindset of not even checking (oops).

This works really well. And it is a nice alternative to the override example floating around where you make the form behave like the title bar.

####
    Private Const WM_NCHITTEST As Integer = &H84
    Private Const HTCLIENT As Integer = &H1
    Private Const HTCAPTION As Integer = &H2

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Debug.Write(m.Msg)

        Select Case m.Msg

            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If (m.Result.ToInt32 = HTCLIENT) Then
                    m.Result = IntPtr.op_Explicit(HTCAPTION)
                End If
                Exit Sub
        End Select

        MyBase.WndProc(m)
    End Sub
###

That works fine until your users accidentally double click the form. Because it is to behave like the title bar, a double click forces a maximize form event (that’s what happens when you double click a title bar) and you get some odd results if you weren’t planning on it (very confusing for end users). The api route is a far easier to deal with.

Thank you both for taking the time to respond to my posting. I really appreciate it.

0
All Courses

From novice to tech pro — start learning today.