Solved

moving control problem in runtime.

Posted on 2004-08-10
5
300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now