PDA

Click to See Complete Forum and Search --> : program running in background


funkheads
Dec 4th, 1999, 03:11 AM
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

Aaron Young
Dec 4th, 1999, 06:36 AM
You can use a few APIs for this.

In a Module, (No Forms in this Project)..

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
aarony@redwingsoftware.com
adyoung@win.bright.net

funkheads
Dec 4th, 1999, 02:22 PM
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).]

Aaron Young
Dec 5th, 1999, 08:45 PM
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..

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
aarony@redwingsoftware.com
adyoung@win.bright.net