Results 1 to 25 of 25

Thread: Find window question..

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Find window question..

    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?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    Why not search by Class?

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..


  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    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
    Last edited by |2eM!x; Jun 21st, 2005 at 01:08 AM.

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    This is easy. Here is it here: http://www.mentalis.org/apilist/FindWindow.shtml

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    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:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. 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
    3. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    4. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    5. Const SW_SHOWNORMAL = 1
    6. Const WM_CLOSE = &H10
    7. Const gcClassnameMSWord = "OpusApp"
    8. Const gcClassnameMSExcel = "XLMAIN"
    9. Const gcClassnameMSIExplorer = "IEFrame"
    10. Const gcClassnameMSVBasic = "wndclass_desked_gsk"
    11. Const gcClassnameNotePad = "Notepad"
    12. Const gcClassnameMyVBApp = "ThunderForm"
    13. Private Sub Form_Load()
    14.     'KPD-Team 1998
    15.     'URL: [url]http://www.allapi.net/[/url]
    16.     'E-Mail: [email][email protected][/email]
    17.     Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
    18.     'Ask for a Window title
    19.     Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
    20.     'Search the window
    21.     WinWnd = FindWindow(vbNullString, Ret)
    22.     If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
    23.     'Show the window
    24.     ShowWindow WinWnd, SW_SHOWNORMAL
    25.     'Create a buffer
    26.     lpClassName = Space(256)
    27.     'retrieve the class name
    28.     RetVal = GetClassName(WinWnd, lpClassName, 256)
    29.     'Show the classname
    30.     MsgBox "Classname: " + Left$(lpClassName, RetVal)
    31.     'Post a message to the window to close itself
    32.     PostMessage WinWnd, WM_CLOSE, 0&, 0&
    33. End Sub

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    thanks, i think i got it

    on a roll tonight arent we baja?

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    oops...how do i find more than one window with its class name?

  9. #9
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    Yeah

  10. #10
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    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.

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    thanks..im a bit puzzled at their examples

  12. #12
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    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

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    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

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    The code in the first link does just that. Lists them all in a ListView.

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find window question..



    Has someone helped you? Then you can Rate their helpful post.

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    the first link that baja works, im just stripping it to bare bones, then ill post what i have for reference : )

  17. #17
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    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.

  18. #18

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    yes, i now prefer classes more : )

  19. #19

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    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..?

  20. #20
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find window question..

    So don't search just for AOL, make it more specific 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)...


    Has someone helped you? Then you can Rate their helpful post.

  21. #21
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    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)
    Attached Files Attached Files

  22. #22

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    aha, str(hwnd)
    i wonder why :?

  23. #23

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    Totally stripped down code, searches for a Class and gives you its hwnd
    In form:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command2_Click()
    4. On Error Resume Next 'i was getting a bad dll error or something, dunno how to fix
    5.     Dim myLong As Long
    6.     myLong = EnumWindows(AddressOf WndEnumProc, vbNullString)
    7. End Sub
    8.  
    9. Private Sub Form_Unload(Cancel As Integer)
    10.     Dim i As Integer
    11.     For i = Forms.Count - 1 To 1 Step -1
    12.         Unload Forms(i)
    13.     Next
    14. End Sub


    VB Code:
    1. Option Explicit
    2. Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    3. Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    4. Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long
    5. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    6.  
    7. Public Function WndEnumProc(ByVal hwnd As Long) As Long
    8.     Dim WText As String * 512
    9.     Dim bRet As Long, WLen As Long
    10.     Dim WClass As String * 50
    11.     Dim hwnds As Long
    12.        
    13.     WLen = GetWindowTextLength(hwnd)
    14.     bRet = GetWindowText(hwnd, WText, WLen + 1)
    15.     GetClassName hwnd, WClass, 50
    16.             If InStr(WClass, "icoPMsgAIM") Then
    17.             Insert hwnd, WText, WClass
    18.             End If
    19.     WndEnumProc = 1
    20. End Function
    21. Private Sub Insert(iHwnd As Long, iText As String, iClass As String)
    22.     MsgBox Str(iHwnd) & " " & iClass
    23. End Sub

  24. #24
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Find window question..

    Here are the bare esentials of what you need.

    EDIT: Hmm... I should have refreshed before I posted. Anyway, another example.
    Attached Files Attached Files

  25. #25

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find window question..

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command2_Click()
    4. On Error Resume Next
    5.     Dim myLong As Long
    6.     myLong = EnumWindows(AddressOf WndEnumProc, vbNullString)
    7. End Sub
    8.  
    9. Private Sub Command23_Click()
    10. Dim i As Integer
    11.     For i = 0 To List.ListCount
    12.   SetWindowPos Val(List.List(i)), -1, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    13.   SetWindowPos Val(List.List(i)), -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    14.   DoEvents
    15.   Next
    16. End Sub

    VB Code:
    1. Option Explicit
    2. Public Const SWP_HIDEWINDOW = &H80
    3. Public Const SWP_SHOWWINDOW = &H40
    4. Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    5. Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    6. Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long
    7. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    8. Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    9. Public Declare Sub SetWindowPos Lib "user32" _
    10.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    11.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    12. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    13.  Type RECT
    14.         Left As Long
    15.         Top As Long
    16.         Right As Long
    17.         Bottom As Long
    18. End Type
    19. Public sizeofwin As RECT
    20. Public cat As Long: Public Oleft As Long: Public oTop As Long: Public oRight As Long: Public oBottom As Long
    21.  
    22. Public Function WndEnumProc(ByVal hWnd As Long) As Long
    23.     Dim WText As String * 512
    24.     Dim bRet As Long, WLen As Long
    25.     Dim WClass As String * 50
    26.     Dim hwnds As Long
    27.        
    28.     WLen = GetWindowTextLength(hWnd)
    29.     bRet = GetWindowText(hWnd, WText, WLen + 1)
    30.     GetClassName hWnd, WClass, 50
    31.             If InStr(WClass, "icoPMsgAIM") Then
    32.             GetWindowRect hWnd, sizeofwin    'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom
    33.                 Oleft = sizeofwin.Left: oTop = sizeofwin.Top: oRight = sizeofwin.Right: oBottom = sizeofwin.Bottom
    34.                 SetWindowPos hWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_HIDEWINDOW
    35.             ' MsgBox Str(hWnd) & " " & WClass
    36.             Form1.List.AddItem Str(hWnd)
    37.             End If
    38.     WndEnumProc = 1
    39. End Function

    THANKS for all the help, never used this type of API before so its new
    glad it all worked nice. I will be back tommorrow for some more help

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