Results 1 to 4 of 4

Thread: Drag form with image on top

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Location
    Athens
    Posts
    168

    Drag form with image on top

    Hi all ,

    l have a form (actually is a circle form) and on top of it is a round ImageBox of with exactly the same dimensions covering the whole
    of the form .
    How by MouseDown on the image to move the form and consequently the image

    Have tried but found nothing .

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drag form with image on top

    Code:
    Private Const HTCAPTION                      As Long = 2
    Private Const WM_NCLBUTTONDOWN               As Long = &HA1
    
    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 Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 Then
            ReleaseCapture
            SendMessage hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
        End If
    End Sub

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Drag form with image on top

    No need for an Image control, just assign your photo to the Form's Picture proerty.

    Code:
    Option Explicit
    
    'Left-click and drag, right-click or ESC to exit.
    
    Private Enum BOOL
        BOOL_FALSE = 0
        BOOL_TRUE = 1
    End Enum
    
    Private Declare Function CreateEllipticRgn Lib "gdi32" ( _
        ByVal X1 As Long, _
        ByVal Y1 As Long, _
        ByVal X2 As Long, _
        ByVal Y2 As Long) As Long
    
    Private Declare Function SetWindowRgn Lib "user32" ( _
        ByVal hWnd As Long, _
        ByVal hRgn As Long, _
        ByVal bRedraw As BOOL) As Long
    
    Private MouseDownX As Single
    Private MouseDownY As Single
    
    Private Sub Form_Initialize()
        SetWindowRgn hWnd, _
                     CreateEllipticRgn(0, _
                                       0, _
                                       ScaleX(ScaleWidth, ScaleMode, vbPixels), _
                                       ScaleY(ScaleHeight, ScaleMode, vbPixels)), _
                     BOOL_FALSE
    End Sub
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyEscape Then Unload Me
        KeyAscii = 0
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button And vbLeftButton Then
            MouseDownX = X
            MouseDownY = Y
        End If
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button And vbLeftButton Then
            Move Left + (X - MouseDownX), Top + (Y - MouseDownY)
        End If
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        'Capture the click so it doesn't leak through to any window beneath us on the screen:
        If Button And vbRightButton Then Unload Me
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Location
    Athens
    Posts
    168

    Re: Drag form with image on top

    Thank you very much Steve . What can l say . Fantastic . Works great.
    l haven't seen anything near it , to questions similar to mine . Short and precise .
    Thank you again .

    @dilettante
    Thank you but the image is necessary and important . You see , it's a golden ring (transparent) which allows
    everything on the form to be visible and accessible , and on Form_Load the Function CreateEllipticRgn Lib and
    the appropriate code makes the whole thing a circle .
    Last edited by vbgeobas; Jul 30th, 2021 at 02:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width