Im looking to find a window, but the caption will never be exactly the same..it will always have the word AIM in it..could anyone tell me how id find it?
Printable View
Im looking to find a window, but the caption will never be exactly the same..it will always have the word AIM in it..could anyone tell me how id find it?
Why not search by Class?
Look here for details http://msdn.microsoft.com/library/de...findwindow.asp
uh oh..i dont like msdn to much..ill do my best to find it..
okay, how do i search by class? its class is :icoPMsgAIM
This is easy. Here is it here: http://www.mentalis.org/apilist/FindWindow.shtml
And you can use this code to find out the class name for a window (if you know it's title). Only use this once to find out the class name for what you need, then just hardcode it in the FindWindow
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Const SW_SHOWNORMAL = 1 Const WM_CLOSE = &H10 Const gcClassnameMSWord = "OpusApp" Const gcClassnameMSExcel = "XLMAIN" Const gcClassnameMSIExplorer = "IEFrame" Const gcClassnameMSVBasic = "wndclass_desked_gsk" Const gcClassnameNotePad = "Notepad" Const gcClassnameMyVBApp = "ThunderForm" Private Sub Form_Load() 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String 'Ask for a Window title Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match") 'Search the window WinWnd = FindWindow(vbNullString, Ret) If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub 'Show the window ShowWindow WinWnd, SW_SHOWNORMAL 'Create a buffer lpClassName = Space(256) 'retrieve the class name RetVal = GetClassName(WinWnd, lpClassName, 256) 'Show the classname MsgBox "Classname: " + Left$(lpClassName, RetVal) 'Post a message to the window to close itself PostMessage WinWnd, WM_CLOSE, 0&, 0& End Sub
thanks, i think i got it :wave:
on a roll tonight arent we baja?
oops...how do i find more than one window with its class name?
Yeah :)
You can use this. Enumerate all windows to a list, then loop through it and check for a given class:
http://www.mentalis.org/apilist/EnumWindows.shtml
http://www.mentalis.org/apilist/EnumThreadWindows.shtml
two examples are avilable.
You should download the API Guide from their site. It has all that, code examples etc.
thanks..im a bit puzzled at their examples
I remembered I saw a complete application that implements it. Here is the link:
http://www.planetsourcecode.com/vb/s...tp%2FC92098362
http://www.planetsourcecode.com/vb/s...tp%2FC3196UPLO
well, since ive posted ive gotten both to work. But they dont do as i want..The enumwindows will tell me the forms name, not classname. The 2nd one just shows children..
ill check out those two examples. Just to clarify, i want to search thru all the classes open, and if a class is icoPMAim or whatever- then i want to find its hwnd
The code in the first link does just that. Lists them all in a ListView.
To find a window based on a partial caption look here :
http://www.vbforums.com/showthread.php?t=316924
http://www.vbforums.com/showpost.php...7&postcount=36
the first link that baja works, im just stripping it to bare bones, then ill post what i have for reference : )
Yeah, but then you increase the chances of mistaking. If you search for partial (example AOL) it is easy that some other window will have 'aol' in it's name.
yes, i now prefer classes more : )
baja, im not sure if you have the download still, but on the api page it says
Insert hwnd, lParam, WText, WClass
how come i cannot do a messagebox of the hwnd? it just stays blank even though it contains a number..?
So don't search just for AOL, make it more specific :D You can't really avoid getting a wrong window. You can reduce the chances by checking the classname (I don't know if that is done in the examples)...
Try the code I modified. When you click refresh, it lists the objects, then loops through them and searches for a class (BaseBar in my example), and diplays their hwnds (if any found)
aha, str(hwnd)
i wonder why :?
Totally stripped down code, searches for a Class and gives you its hwnd
In form:
VB Code:
Option Explicit Private Sub Command2_Click() On Error Resume Next 'i was getting a bad dll error or something, dunno how to fix Dim myLong As Long myLong = EnumWindows(AddressOf WndEnumProc, vbNullString) End Sub Private Sub Form_Unload(Cancel As Integer) Dim i As Integer For i = Forms.Count - 1 To 1 Step -1 Unload Forms(i) Next End Sub
VB Code:
Option Explicit Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Function WndEnumProc(ByVal hwnd As Long) As Long Dim WText As String * 512 Dim bRet As Long, WLen As Long Dim WClass As String * 50 Dim hwnds As Long WLen = GetWindowTextLength(hwnd) bRet = GetWindowText(hwnd, WText, WLen + 1) GetClassName hwnd, WClass, 50 If InStr(WClass, "icoPMsgAIM") Then Insert hwnd, WText, WClass End If WndEnumProc = 1 End Function Private Sub Insert(iHwnd As Long, iText As String, iClass As String) MsgBox Str(iHwnd) & " " & iClass End Sub
Here are the bare esentials of what you need.
EDIT: Hmm... I should have refreshed before I posted. Anyway, another example.
and one last thing, for others.
This works for my trillian(It finds all the class windows with AIM and shrinks them , then brings them back to original size!)
add a listbox name list, add 2 command buttons.
VB Code:
Option Explicit Private Sub Command2_Click() On Error Resume Next Dim myLong As Long myLong = EnumWindows(AddressOf WndEnumProc, vbNullString) End Sub Private Sub Command23_Click() Dim i As Integer For i = 0 To List.ListCount SetWindowPos Val(List.List(i)), -1, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW SetWindowPos Val(List.List(i)), -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW DoEvents Next End Sub
VB Code:
Option Explicit Public Const SWP_HIDEWINDOW = &H80 Public Const SWP_SHOWWINDOW = &H40 Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long Public Declare Sub 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 Long) Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public sizeofwin As RECT Public cat As Long: Public Oleft As Long: Public oTop As Long: Public oRight As Long: Public oBottom As Long Public Function WndEnumProc(ByVal hWnd As Long) As Long Dim WText As String * 512 Dim bRet As Long, WLen As Long Dim WClass As String * 50 Dim hwnds As Long WLen = GetWindowTextLength(hWnd) bRet = GetWindowText(hWnd, WText, WLen + 1) GetClassName hWnd, WClass, 50 If InStr(WClass, "icoPMsgAIM") Then GetWindowRect hWnd, sizeofwin 'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom Oleft = sizeofwin.Left: oTop = sizeofwin.Top: oRight = sizeofwin.Right: oBottom = sizeofwin.Bottom SetWindowPos hWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_HIDEWINDOW ' MsgBox Str(hWnd) & " " & WClass Form1.List.AddItem Str(hWnd) End If WndEnumProc = 1 End Function
THANKS for all the help, never used this type of API before so its new :wave:
glad it all worked nice. I will be back tommorrow for some more help :)