dcsimg
Results 1 to 2 of 2

Thread: Moving a borderless Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    340

    Moving a borderless Form

    I have a borderless Form (Form2) that I attached to the main Form (Form1) using SetParent. I need to move this form around but I have found only one method to do this but the problem is I can't track the mouse movement as I move the Form around. Here are the methods that I have tried

    All variables and APIs have been properly defined but do not show in the code

    This in the only one that works however I can't track the mouse movement using this
    approach

    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       MoveWindow Me.hwnd
     End If
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       ReleaseCapture
       SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
     End If
    End Sub
    
    Public Sub MoveWindow(ByVal hwnd As Long)
        Dim lngReturnValue As Long
        Call ReleaseCapture
        lngReturnValue = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    End Sub
    All the other methods below don't work - they move the form but not correctly
    '--------------------------------------------------------------------------------------------
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static a, b As Integer
    Select Case Button
    Case 0
    a = X
    b = Y
    Case 1
    Me.Left = Me.Left + (X - a)
    Me.Top = Me.Top + (Y - b)
    End Select
    End Sub
    '-------------------------------------------------------------------------------------------
    Code:
    'allow async events while moving borderless form
    'added support for Esc while moving
    'always shows form while moving
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lngXpos = X: lngYpos = Y
       lngXleft = Left: lngYtop = Top
       flgDown = True
     End If
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton And flgDown = True Then
       Move Left + X - lngXpos, Top + Y - lngYpos
     End If
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     flgDown = False
    End Sub
    '-------------------------------------------------------------------------------------------
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Static lx As Single, ly As Single
      'works with any scalemode in the picturebox
      If Button = vbRightButton Then
        Left = Left + ScaleX((X - lx), Me.ScaleMode, vbTwips)
        Top = Top + ScaleY((Y - ly), Me.ScaleMode, vbTwips)
      Else
        lx = X: ly = Y
      End If
    End Sub
    '-------------------------------------------------------------------------------------------
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Static lx  As Single, ly As Single
       
     If Button = 0 Then
       lx = X
       ly = Y
     ElseIf Button = vbRightButton Then
       Me.Left = Me.Left + X - lx 
       Me.Top = Me.Top + Y - ly
      End If
    End Sub
    '-------------------------------------------------------------------------------------------

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,308

    Re: Moving a borderless Form

    Hi Code Dummy,

    There are several methods for doing this, some that work better than others. Here's a link to a little class that I use to do it. The major problem that some of the methods have is that they won't let you drag the title-bar off the screen (or where the title-bar might be). This little class allows you to drag the form almost completely off the screen in any direction you like.

    If that doesn't meet your needs, I can post another method that will accomplish the same objectives, but where more code winds up in the form module.

    Good Luck,
    Elroy

    EDIT1: Also, if you find that the code in that link is working for you, I've got an updated version that allows exclusions. Let me know if you'd like that updated version and I'll post it.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width