Solved

pic box mouse down move form

Posted on 2004-09-06
3
212 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
3 Comments
 
LVL 85

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now