Re: Problem with Mousemove event and moving button

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

Don_at_home.com
Date: 07/21/04


Date: Wed, 21 Jul 2004 19:06:35 GMT

On Wed, 21 Jul 2004 11:52:49 -0700, "ohaya"
<anonymous@discussions.microsoft.com> wrote:

>Hi,
>
>I'm writing a program where the user can place new
>(Command) buttons on the form by clicking on the form, and
>where they can then move the button by clicking on it,
>then dragging it.
>
>I've been able to get the create new button part working,
>but in order to determine where to put the button on the
>Form, I have a Form_Click event that saves the X and Y
>values off to mouse_X and mouse_Y.
>
>Then in my CommandX_Mousedown event, I set a flag
>(indicating that the user has clicked on the button, and
>in the CommandX_Mousemove event, I do the .Move X,Y to
>move the button as the mouse is dragged (with the button
>still down).
>
>This is kind of working, but about 1/2 the time, when I
>click down on a Command button, and then drag the mouse,
>and then release the mouse button, the Command button
>jumps to some (somewhat random) location on the form.
>
>Does anyone have any idea what might be causing this? I
>would have wanted not to use the Form_Mousemove, but I
>couldn't find any other way to capture the X,Y of the
>mouse pointer when it was clicked on the Form.
>
>Thanks,
>Jim

Easiest way to move any control that has a .hwnd property
Start a new project and add a command button and the code below...

Option Explicit
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" ()
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)

Dim lRtn As Long
    If Button = 1 Then
        Call ReleaseCapture
        lRtn = SendMessage(Command1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    End If
End Sub

Have a good day...

Don



Relevant Pages

  • Re: Detecting a Mouse button being held down
    ... This needs a command button and a timer (another, ... Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As ... Private Function MouseIsDown() As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Re: Scrollbar / Command Button question
    ... a command button and a textbox to the form. ... Private Const WM_KEYDOWN = &H100 ... Public Sub Hook ... to the bottom of the text before the command button becomes activated. ...
    (microsoft.public.vb.controls)
  • RE: Centralizing a command button on a userform
    ... command button, then your button would no longer be "centered." ... Private Sub CommandButton1_Click ... The main userform sits in the ... This autosizes to fit the screen whatever the screen resolution is using the ...
    (microsoft.public.excel.programming)
  • Re: Calling Click event of another form
    ... If you study the procedure declaration that Access gave you for the command ... Private Sub cmdMyButton_Click ...
    (microsoft.public.access.formscoding)
  • RE: Centralizing a command button on a userform
    ... Private Sub CommandButton1_Click ... The main userform sits in the ... This autosizes to fit the screen whatever the screen resolution is using the ... If you know the height and width of your command button, ...
    (microsoft.public.excel.programming)