dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] How to move a User Control at run time

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    340

    Resolved [RESOLVED] How to move a User Control at run time

    If I want to move a picturebox, for example, in my User Control at runtime I can use code like this:

    Code:
    Private Sub Picture1_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 = vbLeftButton Then
       Picture1.Left = Picture1.Left + X - lx
       Picture1.Top = Picture1.Top + Y - ly
      End If
    End Sub
    However, I see no way to do this with the control itself because a User Control does not appear to have a Left and Top property

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

    Re: How to move a User Control at run time

    Hi Code Dummy,

    A User Control (UC) always has a Left and Top property relative to its container (often just the form). You don't have to create these properties, as they're more a property of the container rather than the UC itself.

    There are several other intrinsic properties to controls (including UCs). If you want to see a complete list, just start a new UC with no code, throw it onto a form, and then call up the Properties Window with that new UC selected. You'll see the intrinsic properties.

    Good Luck,
    Elroy

    EDIT1: Also, just thinking about it some more, you may be trying to get to these intrinsic properties from within the UC's code. This is what the Extender object is about. To learn more about this Extender object, search for "Extender Object" (no quotes) in your VB6 MSDN, and find it under "Visual Basic Reference". There's a nice page about it.

    EDIT2: You must also keep in mind that your UC's code may be executing even while you're in development mode (non-run-mode) in the IDE. So, it's not really the best of ideas to make extensive use of these intrinsic properties within the UC's code. It's much better to just put this in the form's code instead, which would seem to get done what you're trying to do.
    Last edited by Elroy; Apr 15th, 2019 at 02:54 PM.
    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.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,192

    Re: How to move a User Control at run time

    One thing you can do is move it around by being able to resize it. Normally a UserControl only has None and Fixed Single for its border types, but if you use API to set a certain type of border, the whole thing becomes resizable and you can use that to get it anywhere in any size on your form:



    Here's the code to activate that kind of border:
    Code:
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Const GWL_STYLE As Long = (-16)
    Private Const GWL_EXSTYLE As Long = (-20)
    Private Const WS_BORDER = &H800000
    Private Const WS_THICKFRAME As Long = &H40000
    Private Const WS_EX_CLIENTEDGE As Long = &H200&
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal CX As Long, ByVal CY As Long, ByVal wFlags As SWP_Flags) As Long
    
    
    Private Enum SWP_Flags
        SWP_NOSIZE = &H1
        SWP_NOMOVE = &H2
        SWP_NOZORDER = &H4
        SWP_NOREDRAW = &H8
        SWP_NOACTIVATE = &H10
        SWP_FRAMECHANGED = &H20
        SWP_DRAWFRAME = &H20
        SWP_SHOWWINDOW = &H40
        SWP_HIDEWINDOW = &H80
        SWP_NOCOPYBITS = &H100
        SWP_NOREPOSITION = &H200
        SWP_NOSENDCHANGING = &H400
        
        SWP_DEFERERASE = &H2000
        SWP_ASYNCWINDOWPOS = &H4000
    End Enum
    
    Private Sub SetResizableBorder()
    
    Dim dwStyle As Long, dwExStyle As Long
    dwStyle = GetWindowLong(UserControl.hWnd, GWL_STYLE)
    dwExStyle = GetWindowLong(UserControl.hWnd, GWL_EXSTYLE)
    
    dwStyle = dwStyle Or WS_BORDER
    dwStyle = dwStyle Or WS_THICKFRAME
    dwExStyle = dwExStyle Or WS_EX_CLIENTEDGE
    
    Call SetWindowLong(UserControl.hWnd, GWL_STYLE, dwStyle)
    Call SetWindowLong(UserControl.hWnd, GWL_EXSTYLE, dwExStyle)
    SetWindowPos UserControl.hWnd, 0&, 0&, 0&, 0&, 0&, SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_FRAMECHANGED
    
    End Sub
    I know it's not exactly what you were looking for, but it accomplishes near the same thing and is pretty nifty.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,875

    Re: How to move a User Control at run time

    Quote Originally Posted by Elroy View Post
    EDIT1: Also, just thinking about it some more, you may be trying to get to these intrinsic properties from within the UC's code. This is what the Extender object is about. To learn more about this Extender object, search for "Extender Object" (no quotes) in your VB6 MSDN, and find it under "Visual Basic Reference". There's a nice page about it.
    Seems likely that breaking encapsulation rules like this is exactly what he might be after. Extender would be the way to do it.

    Otherwise why not do it in the container's code just as with a PictureBox?

  5. #5
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,540

    Re: How to move a User Control at run time

    Quote Originally Posted by dilettante View Post
    Seems likely that breaking encapsulation rules like this is exactly what he might be after. Extender would be the way to do it.

    Otherwise why not do it in the container's code just as with a PictureBox?
    Yes, he would have to send the MouseMove event, and that would be the correct way to do it.

    Code:
    Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseMove(Button, Shift, X, Y)
    End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    340

    Re: How to move a User Control at run time

    Thanks all. I decided to do it in the container's MouseMove event

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