Could you tell me how to change the commands below
so that moving the mouse with right button down
will move the picture box ?
Thanks in advance
John S
---------
Private Declare Function SendMessage Lib "User32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Sub ReleaseCapture Lib "User32" ()
Const WM_NCLBUTTONDOWN = &HA1 '???
Const HTCAPTION = 2
Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = 2 Then 'right button
Call ReleaseCapture
lngReturnValue = SendMessage(Form1.hWnd, WM_NCLBUTTONDOWN, _
HTCAPTION, 0&)
End If
End Sub
*** Sent via Developersdex http://www.developersdex.com ***
The code on which this is based:
If Button = vbLeftButton Then
ReleaseCapture
SendMessage Picture1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
End If
... is saying: if the left button is pressed, send a message to the picture
box via the picture box hwnd to indicate that the picture box has received a
HTCAPTION hit via a left mouse button in the non-client area. This mimics
the message that a window receives when the left button is pressed on a
titlebar. The result is you move the form, or, with the example above
because a picture box hwnd is supplied, you move the picture box. This
functionality works because not only are you telling the hwnd to process a
fake left button non-client area click, but in fact the physical left button
is depressed meaning there really is a legitimate left button click taking
place.
For a right button however things don't work the same. Since this message
simulates, to the pix box, the same signal that a left click on a title bar
would generate to a form, specifying vbRightButton will not change the
procedure to cause movement with the right button. Nor will substituting
WM_NCRBUTTONDOWN (&HA4) in the SendMessage call affect the desired result.
This is because, were a right button clicked on a title bar area, the action
would display the system context menu, not invoke a message to move the
window. Therefore you can't "trick" Windows into processing a right mouse
click to perform an action that would take place with a left mouse click.
The net result of :
If Button = vbRightButton Then
ReleaseCapture
SendMessage Picture1.hwnd, WM_NCRBUTTONDOWN, HTCAPTION, ByVal 0&
End If
... is nothing happens. If something were to happen, this code would pop up
the system context menu over the pixbox (which it won't do). And if the code
interspersed the right button trap with the left button message:
If Button = vbRightButton Then
ReleaseCapture
SendMessage Picture1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
End If
... you will succeed in requiring that the user first click the left button,
then hold it down while clicking the right button, to affect the picture box
move.
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
Please reply to the newsgroups so all can participate.
"John S" <nospam> wrote in message
news:uY$wLNbvG...@TK2MSFTNGP06.phx.gbl...
> Could you tell me how to change the commands
> below so that moving the mouse with right button
> down will move the picture box ?
As Randy has said, you can't trick Windows into doing what you're after
using the method you are currently using. You can however use a completely
different method to do the job. In that way you can make it respond to the
left button or the right or middle button or any combination of them. Here's
one way of doing it. This specific example responds only to the right
button, but you can easily modify it to respond to the left button as well
if you want:
Mike Williams
MS MVP Visual Basic
Option Explicit
Private dragging As Boolean
Private xmouse As Single, ymouse As Single
Private Sub Form_Load()
Picture1.ScaleMode = Me.ScaleMode
End Sub
Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
xmouse = X
ymouse = Y
dragging = True
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim x1 As Single, y1 As Single
If dragging Then
x1 = Picture1.Left + X - xmouse
y1 = Picture1.Top + Y - ymouse
If (Picture1.Left <> x1) Or (Picture1.Top <> y1) Then
Picture1.Move x1, y1
End If
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
dragging = False
End If
End Sub
Needs :
1 Form
1 Picturebox
Dim xRest, yRest
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button <> 2 Then Exit Sub
Picture1.Move X - xRest, Y - yRest ' Move the Picturebox
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single,
Y As Single)
Picture1.Enabled = True
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button <> 2 Then Exit Sub
xRest = X: yRest = Y: Picture1.Enabled = False
End Sub