Solved

How to drag a object like , but the object must move with the mouse. Like a form

Posted on 1998-12-17
5
150 Views
Last Modified: 2010-05-03
I want to drag ,by example, a button. And when I move my mouse, the button must move directly, like a form.

How?

0
Comment
Question by:graat
  • 3
  • 2
5 Comments
 

Author Comment

by:graat
ID: 1450960
Edited text of question
0
 

Accepted Solution

by:
DPickering earned 70 total points
ID: 1450961
Create a new VB Project, add a command button to it, then paste the following code to the form...

Cheers,
Dave

<CODE STARTS HERE>

Private Declare Function ReleaseCapture Lib "user32" () As Long
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 Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Const WM_SYSCOMMAND = &H112

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Command1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
       
    ' Or use:
    'SendMessage Command1.hwnd, WM_SYSCOMMAND, &HF012&, 0&
End Sub

0
 

Author Comment

by:graat
ID: 1450962
If i want to drag the button, the message "sub or function not defined" pops up.

He thinks releasecapture doesn't excists. How can I solve this problem?

0
 

Expert Comment

by:DPickering
ID: 1450963
Who thinks ReleaseCapture doesn't exist? It does exist and should work as long as you're not using VB3 or VB4-16 bit. In that case you'll need to use the API browser to get the 16-bit declaration for it. It also requires Windows 95 or later or Windows NT 3.1 or later.

Also, why do you want the user to click a Command Button and have it move around the screen? Not exactly Windows standards. Regardless of that however, this works with any control, even a form. All controls and forms are simply Window objects (anything which has a hWnd property is a Window) and any Window object can be used for this example. You could for example have the same code in the Form_MouseDown event to move the form around without having to click on the title bar. ReleaseCapture needs to be called otherwise this whole thing won't work.

I've used this on Windows 95, 98 and Windows NT 4 and it works. Open a new project and then open the Code Pane for Form1. Then delete whatever code is in the Form (maybe Option Explicit or the Form_Load event) and then paste the code I supplied (after <CODE STARTS HERE>) into the Code Pane. Compile it and if this still doesn't work goto http://www.desertware.com/vbuniverse/moveform.html for the same example - just to show you that's the way its done.

Dave
0
 

Author Comment

by:graat
ID: 1450964
Fantastic,
THANX

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now