Link to home
Start Free TrialLog in
Avatar of TheRoyalFalcon
TheRoyalFalcon

asked on

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.


Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Javert93
Javert93

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TheRoyalFalcon
TheRoyalFalcon

ASKER

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.