Results 1 to 4 of 4

Thread: program running in background

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98

    Post

    my question might be really simple, but i have no clue where to start with my idea. basically, if my program is running, when the user double-clicks (or right-clicks, based on the user's selection in the config menu of the prog) any window's titlebar i want to be able to run a proceedure. really all i need is a function that would allow me (that is, the program) to know if the user double- or right-clicks in any window's titlebar. (or an idea of where to start) any help would be appreciated. thank you

    --michael

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You can use a few APIs for this.

    In a Module, (No Forms in this Project)..
    Code:
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Sub Main()
        Dim lHwnd As Long
        Dim sBuff As String * 255
        Dim tPOINT As POINTAPI
        Dim tRECT As RECT
        Do
            'Wait for a Right Click Anywhere in the O/S
            While GetAsyncKeyState(vbRightButton) = 0
                DoEvents
            Wend
            'Get the Cursor,(Mouse), Position
            Call GetCursorPos(tPOINT)
            'Get the Window Handle the Mouse is Over
            lHwnd = WindowFromPoint(tPOINT.x, tPOINT.y)
            'Get the Windows Dimensions
            Call GetWindowRect(lHwnd, tRECT)
            If tPOINT.y < tRECT.Top + 27 Then
                'See if the Right Click Occurred in the Caption Area, (1st 27 Pixels)
                Debug.Print "Right Click in Caption of " & Left(sBuff, GetWindowText(lHwnd, ByVal sBuff, 255))
            End If
            'Wait for Right Button to be released
            While GetAsyncKeyState(vbRightButton)
            Wend
        Loop
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98

    Post

    aaron -

    thank you so very much. i think this help will allow me to get started on my idea. i truly appreciate your talents when it comes to VB programming. you are one of a kind. but i must ask for one more bit of information: is there a way to do this on a double-click or any key + any button single/double click? and also ONLY have it do something if the click is in the titlebar of a window...

    again, thank you.

    --michael

    ps - just out of curiosity, are you a teacher/professor? also, where in god's name did you learn all this?

    [This message has been edited by funkheads (edited 12-05-1999).]

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    To Activate the Code on More than a Mouse Click, simply specify more Keys with Or in the While Statement.
    You can also use the GetWindowLong API to check to see if the Form/Window has a Caption Bar..
    Code:
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    
    Private Const GWL_STYLE = (-16)
    Private Const WS_CAPTION = &HC00000
    
    Sub Main()
        Dim lHwnd As Long
        Dim sBuff As String * 255
        Dim tPOINT As POINTAPI
        Dim tRECT As RECT
        Do
            'Wait for a Right Click with Shift Held Down Anywhere in the O/S
            While GetAsyncKeyState(vbRightButton) = 0 Or GetAsyncKeyState(vbKeyShift) = 0
                DoEvents
            Wend
            'Get the Cursor,(Mouse), Position
            Call GetCursorPos(tPOINT)
            'Get the Window Handle the Mouse is Over
            lHwnd = WindowFromPoint(tPOINT.x, tPOINT.y)
            'See If Window Has a Caption bar..
            If (GetWindowLong(lHwnd, GWL_STYLE) And WS_CAPTION) = WS_CAPTION Then
                'Get the Windows Dimensions
                Call GetWindowRect(lHwnd, tRECT)
                If tPOINT.y < (tRECT.Top + 23) Then
                    'See if the Right Click Occurred in the Caption Area, (1st 23 Pixels)
                    Debug.Print "Right Click in Caption of " & Left(sBuff, GetWindowText(lHwnd, ByVal sBuff, 255))
                    Beep 'Audible Indicator
                End If
            End If
            'Wait for Right Button to be released
            While GetAsyncKeyState(vbRightButton)
            Wend
        Loop
    End Sub
    To answer your other questions..
    I'm not a Teacher/Professor, I'm a 22 year old Analyst Programmer.
    How do I know what I know?.. I love Programming and Problem Solving.
    I spent alot of time, when I first started professionaly, refining code and finding better ways to do things and the habit's just stuck with me.

    If I don't know the answer to something, I find it, then I make sure I understand it before I use it.

    You'd be suprised how much easier it is to program, when you actually understand how your code works, instead of just knowing what it does or is supposed to do.


    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

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