Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-12-17
5
Medium Priority
?
159 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 280 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

783 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