Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Very quick question. Userform control coordinates.

This is a discussion on Very quick question. Userform control coordinates. within the Excel Questions forums, part of the Question Forums category; Are in points? Correct? For example, the left coordinate of some control on a userform. Would be in points? I ...

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default Very quick question. Userform control coordinates.

    Are in points? Correct? For example, the left coordinate of some control on a userform. Would be in points? I am trying to get the window handle for a frame control that is drawn onto a userform. Because the class name is the same for all msform windowed controls, I figured that I would use the WindowFromPoint API function to get the handle by figuring out the xy coordinates of the frame.

    Thanks for any help!

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    Example VBA Code:
    **'The width of a pixel in Excel's userform coordinates
    **'thanks to Stephen Bullen
    **Public Property Get PointsPerPixelX() As Double
    ****** Dim hDC As Long
    ****** hDC = GetDC(0)
    ****** 'A point is defined as 1/72 of an inch and LOGPIXELSX returns
    ****** 'the number of pixels per logical inch, so divide them to give
    ****** 'the width of a pixel in Excel's userform coordinates
    ****** PointsPerPixelX = 72 / GetDeviceCaps(hDC, LOGPIXELSX)
    ****** ReleaseDC 0, hDC
    **
    **
    **End Property
    **
    **
    **'The width of a pixel in Excel's userform coordinates
    **'thanks to Stephen Bullen
    **Public Property Get PointsPerPixelY() As Double
    ****** Dim hDC As Long
    ****** hDC = GetDC(0)
    ****** 'A point is defined as 1/72 of an inch and LOGPIXELSX returns
    ****** 'the number of pixels per logical inch, so divide them to give
    ****** 'the width of a pixel in Excel's userform coordinates
    ****** PointsPerPixelY = 72 / GetDeviceCaps(hDC, LOGPIXELSY)
    ****** ReleaseDC 0, hDC
    **End Property
    **


    Example VBA Code:
    **Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    **Private Declare Function GetDeviceCaps Lib "Gdi32" (ByVal hDC As Long, ByVal _
    **nIndex As Long) As Long
    **Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal _
    **hDC As Long) As Long
    **Private Const LOGPIXELSX = 88
    **Private Const LOGPIXELSY = 90

  3. #3
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    5,772

    Default

    Userform Controls are windowless so I don't think you can get their handles.

    Regards.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  4. #4
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    The frame, listbox, and MultiPage controls are windows. The problem is the class name is the same for the client area and all of the above listed controls. So I have no way to distinguish one from the other or I could simply use the API FindWindowEx function. That is why I figured I would use the WindowFromPoint API function. Any other ideas?

  5. #5
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    71,422

    Default

    Tom

    I think Jaafar is right.

    What are you actually trying to achieve?
    If posting code please use code tags.

  6. #6
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    Hi Jaafar and Norie. The question relates to this post.

    It works, but you have to click on the frame. This way, I can use the cursor position to get frame1's handle by way of the frame1's click event. This may prove good enough for the OP. I don't know? My thought was to get the rectangle of the userform and then add the left position (converted from points to pixels) of the frame. What do you think?

    ScreenCaptureControlOnUserform.zip

    EDIT: See the code. There is a handle for the three controls listed.

  7. #7
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    71,422

    Default

    Tom

    What is the OP trying to do?

    I think there might be other solutions than the Windows API to place an image
    from a userform on a spreadsheet.

    I'm not going to try and check them out or look for them as it's rather late here, but I'll have a look in the morning/afternoon/whatever.
    If posting code please use code tags.

  8. #8
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824

    Default

    I don't know of any that would take a snapshot of a frame control and the child controls within? Maybe there is... I'm not even 100% sure that's what the OP wants. No matter, I do a lot of screenshots for various reasons but have been using Paint Shop Pro, which has a decent screen capture function, but I would like to create my own from Excel anyway. So this will be a good start.

    Am still looking for an alternative suggestion for getting the window handle for the frame control on a userform. I'm betting there is a better way than using the WindowFromPoint API function.

    Thanks guys!

  9. #9
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    71,422

    Default

    Tom

    Sorry, I just reread your post and the link.

    I think you might be on the right lines with the API, I can recall responding to
    a similar post and directing the OP to some similar code.

    I think that involved capturing some "freeform" drawing on a userform.
    If posting code please use code tags.

  10. #10
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    5,772

    Default

    I didn't know those particular controls had a hwnd !!

    Anyway, have you thought about using the ClientToScreen API ?

    Add a CommandButton1 to your form and add the following code in the userform module :

    I assume the frame is named frame1

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function WindowFromPoint Lib "user32" _
    (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    
    Private Declare Function ClientToScreen Lib "user32" _
    (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
     
    Private Declare Function GetDeviceCaps Lib "Gdi32" (ByVal hDC As Long, ByVal _
    nIndex As Long) As Long
    
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal _
    hDC As Long) As Long
    
    Private Const LOGPIXELSX = 88
    Private Const LOGPIXELSY = 90
     
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    
    'The width of a pixel in Excel's userform coordinates
      'thanks to Stephen Bullen
      Public Function PointsPerPixelX() As Double
           Dim hDC As Long
           hDC = GetDC(0)
           'A point is defined as 1/72 of an inch and LOGPIXELSX returns
           'the number of pixels per logical inch, so divide them to give
           'the width of a pixel in Excel's userform coordinates
           PointsPerPixelX = 72 / GetDeviceCaps(hDC, LOGPIXELSX)
           ReleaseDC 0, hDC
       
       
      End Function
    
    'The HEIGHT of a pixel in Excel's userform coordinates
      'thanks to Stephen Bullen
    
      Public Function PointsPerPixelY() As Double
           Dim hDC As Long
           hDC = GetDC(0)
           'A point is defined as 1/72 of an inch and LOGPIXELSY returns
           'the number of pixels per logical inch, so divide them to give
           'the width of a pixel in Excel's userform coordinates
           PointsPerPixelY = 72 / GetDeviceCaps(hDC, LOGPIXELSY)
           ReleaseDC 0, hDC
       
       
      End Function
    
    
    
    ' displays the frame hwnd
    Sub CommandButton1_Click()
    
        Dim P As POINTAPI
        Dim lngHwnd, lngFrameHwnd As Long
        Dim lngLeftInPixels, lngTopInPixels As Long
        
        lngHwnd = FindWindow(vbNullString, Me.Caption)
        lngLeftInPixels = Me.Frame1.Left / PointsPerPixelX
        lngTopInPixels = Me.Frame1.Top / PointsPerPixelY
        P.x = lngLeftInPixels
        P.y = lngTopInPixels
        ClientToScreen lngHwnd, P
        lngFrameHwnd = WindowFromPoint(P.x, P.y)
        MsgBox "The Frame handle is :  " & lngFrameHwnd
    
    End Sub

    Regards.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com