VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Move Controls Smoothly w/ Mouse (Runtime)

by Treehill (1 Submission)
Category: Custom Controls/Forms/Menus
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

This code demostrates how to move controls smoothly on a form without them redrawing slowly or chunky looking.

Inputs
Just click and drag the picturebox to move it anywhere on the form.
Assumes
I recycled this code that was moving a form, which when calling the SendMessage function, it would mimic the form's menu being clicked and moved, but I changed the handle (hWnd) to the pictureboxes, and it worked.
Code Returns
n/a
Side Effects
As of yet, I am unaware if I am using the correct flag (HTCAPTION) as a SendMessage param. Obviously it works, but I wasn't sure if there was a different flag that was more appropriate. Reason being, that there is no caption in a picturebox. Be aware of this and any side effects this might case in your code.
API Declarations
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function ReleaseCapture Lib "user32" () As Long

Rate Move Controls Smoothly w/ Mouse (Runtime)

'Copy this code into a form, and add a picturebox
Option Explicit
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 Declare Function ReleaseCapture Lib "user32" () As Long
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 If Button = vbLeftButton Then
  ReleaseCapture
  Call SendMessage(Picture1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
 End If
End Sub

Download this snippet    Add to My Saved Code

Move Controls Smoothly w/ Mouse (Runtime) Comments

No comments have been posted about Move Controls Smoothly w/ Mouse (Runtime). Why not be the first to post a comment about Move Controls Smoothly w/ Mouse (Runtime).

Post your comment

Subject:
Message:
0/1000 characters