Trap click on form title (caption)

Discussion in 'AutoCAD' started by AKS, Sep 23, 2004.

  1. AKS

    AKS Guest

    Using VBA, how does one capture a mouse click event on the form title? I've got a floating modeless form that I want to toggle its height so to affect a window shade effect.
     
    AKS, Sep 23, 2004
    #1
  2. AKS

    bcoward Guest

    AKS,

    I'm going to say "No" using this logic.

    I would need to grab the screen coordinates where the mouse was clicked. This could be done using the GetCursorPos API. You would then need to convert screen coordinates with client coordinates (ScreenToClient API).

    The problem, and crux of the issue, is that a userform does not extend a hWnd property nor does the userform click event extend to the form caption area. If you could grab a handle to the form you could achieve your needs. Thus without subclassing I don't think this can be done.

    Good luck,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Sep 23, 2004
    #2
  3. AKS

    Tom Roberts Guest

    Add the code below to your UserForm to give them a hWnd property

    '------------------------------------
    Private Declare Function FindWindow Lib "user32" _
    Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    Public Property Get hWnd() As Long

    Dim lngTemp As Long
    Dim strCaption As String

    strCaption = Me.Caption
    lngTemp = FindWindow(vbNullString, strCaption)
    If lngTemp <> 0 Then
    hWnd = lngTemp
    End If

    End Function
    '-----------------------------------

    Regards
    Tom Roberts
    __________________________
    MechWest Design & Drafting
    Perth, Western Australia


    This could be done using the GetCursorPos API. You would then need to
    convert screen coordinates with client coordinates (ScreenToClient API).
    hWnd property nor does the userform click event extend to the form caption
    area. If you could grab a handle to the form you could achieve your needs.
    Thus without subclassing I don't think this can be done.
     
    Tom Roberts, Sep 24, 2004
    #3
  4. AKS

    AKS Guest

    Ok, but I see the userform flash for an instant when clicking in the userform title area. Therefore the system is seeing the mouse click on the form caption area and deciding to ignore the event. Is it possible to change the clickable part of the form to include the caption area?
     
    AKS, Sep 24, 2004
    #4
  5. AKS

    bcoward Guest

    Thanks Tom,

    As has been found out, the userform click event doesn't extend to the caption area.

    You can paste the following code into a userform. It might give you enough to figure w work around.

    """""""""""""""""""

    Option Explicit

    Private Declare Function FindWindow Lib "user32" _
    Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    Private Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long

    Private Declare Function ScreenToClient Lib "user32" _
    (ByVal hWnd As Long, lpPoint As POINTAPI) As Long

    Private Type POINTAPI
    x As Long
    y As Long
    End Type

    Sub MouseClicked()

    Dim pt As POINTAPI
    Dim lRetVal As Long
    Dim lValRet As Long
    '
    ' See where the mouse is.
    Call GetCursorPos(pt)
    ' Convert into client coordinates.
    Call ScreenToClient(hWnd, pt)
    '
    MsgBox "My Position:" & vbLf & pt.x & "," & pt.y
    End Sub

    Public Function hWnd() As Long

    Dim lngTemp As Long
    Dim strCaption As String

    strCaption = Me.Caption
    lngTemp = FindWindow(vbNullString, strCaption)
    If lngTemp <> 0 Then
    hWnd = lngTemp
    End If

    End Function

    Private Sub UserForm_Click()

    MouseClicked

    MsgBox UserForm1.top & " Top"
    MsgBox UserForm1.Left & " Left"

    End Sub

    """""""""''''''''''''''''''''''

    Good luck,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Sep 24, 2004
    #5
  6. FWIW here is a simpler approach to "giving" a vb foorm hwnd property:

    Private Declare Function GetActiveWindow Lib "user32" () As Long

    Public Property Get hwnd() As Long
    hwnd = GetActiveWindow()
    End Property

    No messing with captions, etc., just straight API.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 24, 2004
    #6
  7. AKS

    AKS Guest

    Thank you folks.
     
    AKS, Sep 24, 2004
    #7
  8. AKS

    AKS Guest

    It almost works Bob. Nice try though. It is a good example. The problem is that userform_click does not apply to clicks on the caption area. That was the original problem. An alternative so far has been to turn off the visiblity of the form's controls and then reduce the form to include a click zone band that is about the same depth as the caption.
     
    AKS, Sep 27, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.