Solved

pic box mouse down move form

Posted on 2004-09-06
3
218 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 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