Solved

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

Posted on 1998-12-17
5
153 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
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…
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…

679 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