Results 1 to 14 of 14

Thread: A bit of Help please (solved)

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44

    A bit of Help please (solved)

    I'm trying to find the handle of a window but since it's class name changes everytime the window is opened i'm having some trouble.
    I've found a bit of code that allows me to use wildcards to find the handle but i can't seem to figure out how to call the handle from my form.
    Here is the bit of code i have in my module
    VB Code:
    1. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    2.     Dim Length As Long
    3.     Dim sName As String
    4.     Length = GetWindowTextLength(hwnd) + 1
    5.    
    6.     If Length > 1 Then
    7.       sName = Space(Length)
    8.       GetWindowText hwnd, sName, Length
    9.       sName = Left$(sName, Length - 1)
    10.       'Use the Like operator to search with wildcards
    11.       If sName Like "*The Wizard: Current Macro Set:*" Then
    12.            MsgBox "The handle is: " & sName
    13.         End If
    14.     End If
    15.  
    16.     EnumWindowsProc = 1
    17. End Function

    The Message box i have in there is just for me to know if the function worked Properly, and this part does.
    Anyone have any suggestions.
    Last edited by Irre; Dec 6th, 2001 at 07:54 PM.

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    Anyone have any clues ?

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    What is your code which opens the new window?

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    i'm not opening a new window just trying to get the handle to a
    window thats already open. Since it's class changes everytime
    it's opened i can't use findwindow(i think), so using that code
    allows me to use wildcards to find the caption. Once it has the
    caption it also has the handle which is proven by the msgbox i
    have in there. But like i said before my problem was getting other
    functions to be able to access the handle.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Try this:
    VB Code:
    1. If Instr(sName, "The Wizard: Current Macro Set:") Then
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Handle for class...

    Is the class created from within the application which is trying to hold of it's handle?
    What type of class is it?
    How does the name of the class change everytime it's created?

    Each class module has it's own handle, so if you place the following in the class then you can access it's handle from the object which created it:

    Public Property Get hWnd() As Long
    hWnd = Me.hWnd
    End Property

    Does that make sense?

    Although I am a tab puzzled...

    Could you post your code here so that I could have a look...Easier to find a solution then.


    Regards,

    Serious Beard

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    Mc Brain What is your code supposed to do ?


    The Window i'm trying to get the handle for is one for another
    program.


    in the form i have this
    VB Code:
    1. Private Sub Form_Load()
    2. EnumWindows AddressOf EnumWindowsProc, 0
    3. End Sub

    In a Module i have this

    VB Code:
    1. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    2.     Dim Length As Long
    3.     Dim sName As String
    4.     Length = GetWindowTextLength(hwnd) + 1
    5.    
    6.     If Length > 1 Then
    7.       sName = Space(Length)
    8.       GetWindowText hwnd, sName, Length
    9.       sName = Left$(sName, Length - 1)
    10.       'Use the Like operator to search with wildcards
    11.       If sName Like "*The Wizard: Current Macro Set:*" Then
    12.            MsgBox "The handle is: " & sName
    13.      DRhwnd = hwnd
    14.      MsgBox DRhwnd
    15.       End If
    16.     End If
    17.  
    18.     EnumWindowsProc = 1
    19. End Function


    If i try and put a message box in my form an have it display the
    handle of the window it just shows up blank, that is my main
    problem i can't seem to access the information in the Enumwindows function
    Last edited by Irre; Dec 6th, 2001 at 07:32 AM.

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I know what you're trying to do. I got the handle of program like DreamWeaver, or Teleport Pro. Those programs, for example, have a caption like "MyPage.htm - DreamWeaver", and that will depend on the name of the project or html you've opened.

    So, if in your sName, you have the string " - DreamWeaver" somewhere, there's a chance to be the window your looking for. I don't know what's the problem with that code. Have you tested?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    The reason why i used the wildcards in the caption was because
    that string is always there, and the class changes, which prevents
    me from useing findwindow,which i think would be alot easier to
    pass the handle onto other parts of the program. thats what the
    original post boils down to is that i can't figure out how to pass
    the handle to other parts of the function.
    Last edited by Irre; Dec 6th, 2001 at 07:49 AM.

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Here's part of a project I did once.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetWindowTextLength Lib _
    4.   "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    5. Private Declare Function GetWindowText Lib "user32" _
    6.   Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal _
    7.   lpString As String, ByVal cch As Long) As Long
    8. Private Declare Function GetWindow Lib "user32" _
    9. (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    10.  
    11. Private Const GW_HWNDFIRST = 0
    12. Private Const GW_HWNDNEXT = 2
    13.  
    14. Private Function GetHandle(Caption As String, TaskName As String) As Long
    15.     Dim CurrWnd As Long
    16.     Dim e0 As String
    17.     Dim Length As Integer
    18.     Dim tName As String
    19.     Dim Pos As Integer
    20.    
    21.     CurrWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)
    22.     e0 = UCase$(Caption)
    23.     Do While CurrWnd <> 0
    24.         Length = GetWindowTextLength(CurrWnd)
    25.         tName = Space$(Length + 1)
    26.         Length = GetWindowText(CurrWnd, tName, Length + 1)
    27.         tName = Left$(tName, Len(tName) - 1)
    28.         If Length > 0 Then
    29.             If InStr(UCase$(Caption), "*") Then
    30.                 e0 = Right$(UCase$(Caption), Len(Caption) - 1)
    31.                 Pos = InStr(UCase$(tName), e0)
    32.                 If Pos Then
    33.                     'The wanted one
    34.                     TaskName = tName
    35.                     Exit Do
    36.                 End If
    37.             Else
    38.                 If UCase$(Caption) = UCase$(tName) Then
    39.                     TaskName = tName
    40.                     Exit Do
    41.                 End If
    42.             End If
    43.         End If
    44.         CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    45.         DoEvents
    46.     Loop
    47.    
    48.     GetHandle = CurrWnd
    49. End Function
    50.  
    51. Private Sub Form_Load()
    52.     Dim hWnd As Long
    53.     Dim TaskName As String
    54.    
    55.     hWnd = GetHandle("* - Microsoft Outlook", TaskName)
    56.     MsgBox "The wanted window is " & TaskName
    57. End Sub

    In this example, it doesn't mind if your on the Inbox, Outbox or whatever... it will always find the Outlook's Window -if opened-
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    Thanks, That looks REALLY complicated so it'll probably take me a while to mess around with.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    Mc Brain your code didn't work, i even tried opening Outlook Express and pasting your code and running it like that.

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Ohio
    Posts
    44
    I feel so ashamed to have let this simple problem with such a
    simple answer cause such a problem all i had to do was to add
    in my module Public DRhwnd as long and then in my form add
    msgbox DRhwnd
    Damn do i feel dumb.

  14. #14
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Irre
    Mc Brain your code didn't work, i even tried opening Outlook Express and pasting your code and running it like that.
    I said Outlook, not Outlook Express.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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