Solved

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

Posted on 1998-12-17
5
149 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
Comment Utility
Edited text of question
0
 

Accepted Solution

by:
DPickering earned 70 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Fantastic,
THANX

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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

7 Experts available now in Live!

Get 1:1 Help Now