Solved

pic box mouse down move form

Posted on 2004-09-06
3
219 Views
Last Modified: 2011-09-20
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.


0
Comment
Question by:TheRoyalFalcon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11993460
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
 
LVL 4

Accepted Solution

by:
Javert93 earned 100 total points
ID: 12010396
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
 
LVL 1

Author Comment

by:TheRoyalFalcon
ID: 12044745
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question