Solved

moving control problem in runtime.

Posted on 2004-08-10
5
297 Views
Last Modified: 2010-05-02
helloo experts
here is the problem
I am trying to move label from location to another on a form in runtime by using mousedown and mousemove events but the problem is when the mouse goes over a control on the form while moving the label I lost the actual form.mouse(x,y),,,, in another meaning it becomes the x,y of that control.
I have alote of controls on the form so it is hard to write mousemove event for each one.
my Question is how to keep this label stick to mouse whatever the control under mouse or how to get the actual form x,y.
regards
0
Comment
Question by:fadloun
5 Comments
 
LVL 3

Expert Comment

by:TILL
ID: 11770184
You have to create an api hook and on WM_MOUSEMOVE message use GetCursorPos to extract the position of the cursor relative to the screen.
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 50 total points
ID: 11771334
I wrote this code and works fine:


Dim MouseD As Boolean
Dim curX As Integer
Dim curY As Integer

Private Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   MouseD = True
   curX = Label1.Left
   curY = Label1.Top
End Sub

Private Sub label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
   If MouseD = True Then
      X = curX + X
      Y = curY + Y
      Label1.Move X, Y
      Label2.Caption = X
      Label3.Caption = Y
      curX = X
      curY = Y
      Label1.Refresh
   End If
End Sub

Private Sub label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
   MouseD = False
End Sub
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 75 total points
ID: 11772544
Option Explicit

Private mouseDown As Boolean
Private startX As Single, startY As Single

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        startX = X
        startY = Y
        mouseDown = True
    End If
End Sub

Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton And mouseDown Then
        Label1.Move Label1.Left + X - startX, Label1.Top + Y - startY
    End If
End Sub

Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton And mouseDown Then
        mouseDown = False
    End If
End Sub
0
 
LVL 5

Author Comment

by:fadloun
ID: 11773242
Idle mind
Good Good Good ,,,,
have a good day ,,,,thanx

0
 
LVL 5

Author Comment

by:fadloun
ID: 11773284
sorry jyparask
I did not read your solution
it is also working
gonna split points
thanx allll
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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

20 Experts available now in Live!

Get 1:1 Help Now