|
-
Dec 5th, 2001, 10:10 PM
#1
Thread Starter
Member
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:
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim sName As String
Length = GetWindowTextLength(hwnd) + 1
If Length > 1 Then
sName = Space(Length)
GetWindowText hwnd, sName, Length
sName = Left$(sName, Length - 1)
'Use the Like operator to search with wildcards
If sName Like "*The Wizard: Current Macro Set:*" Then
MsgBox "The handle is: " & sName
End If
End If
EnumWindowsProc = 1
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.
-
Dec 6th, 2001, 06:37 AM
#2
Thread Starter
Member
-
Dec 6th, 2001, 06:49 AM
#3
What is your code which opens the new window?
-
Dec 6th, 2001, 07:03 AM
#4
Thread Starter
Member
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.
-
Dec 6th, 2001, 07:14 AM
#5
Need-a-life Member
Try this:
VB Code:
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.
-
Dec 6th, 2001, 07:14 AM
#6
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
-
Dec 6th, 2001, 07:29 AM
#7
Thread Starter
Member
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:
Private Sub Form_Load()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub
In a Module i have this
VB Code:
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim sName As String
Length = GetWindowTextLength(hwnd) + 1
If Length > 1 Then
sName = Space(Length)
GetWindowText hwnd, sName, Length
sName = Left$(sName, Length - 1)
'Use the Like operator to search with wildcards
If sName Like "*The Wizard: Current Macro Set:*" Then
MsgBox "The handle is: " & sName
DRhwnd = hwnd
MsgBox DRhwnd
End If
End If
EnumWindowsProc = 1
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.
-
Dec 6th, 2001, 07:39 AM
#8
-
Dec 6th, 2001, 07:43 AM
#9
Thread Starter
Member
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.
-
Dec 6th, 2001, 07:54 AM
#10
Need-a-life Member
Here's part of a project I did once.
VB Code:
Option Explicit
Private Declare Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) 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 GetWindow Lib "user32" _
(ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Function GetHandle(Caption As String, TaskName As String) As Long
Dim CurrWnd As Long
Dim e0 As String
Dim Length As Integer
Dim tName As String
Dim Pos As Integer
CurrWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)
e0 = UCase$(Caption)
Do While CurrWnd <> 0
Length = GetWindowTextLength(CurrWnd)
tName = Space$(Length + 1)
Length = GetWindowText(CurrWnd, tName, Length + 1)
tName = Left$(tName, Len(tName) - 1)
If Length > 0 Then
If InStr(UCase$(Caption), "*") Then
e0 = Right$(UCase$(Caption), Len(Caption) - 1)
Pos = InStr(UCase$(tName), e0)
If Pos Then
'The wanted one
TaskName = tName
Exit Do
End If
Else
If UCase$(Caption) = UCase$(tName) Then
TaskName = tName
Exit Do
End If
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Loop
GetHandle = CurrWnd
End Function
Private Sub Form_Load()
Dim hWnd As Long
Dim TaskName As String
hWnd = GetHandle("* - Microsoft Outlook", TaskName)
MsgBox "The wanted window is " & TaskName
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.
-
Dec 6th, 2001, 07:56 AM
#11
Thread Starter
Member
Thanks, That looks REALLY complicated so it'll probably take me a while to mess around with.
-
Dec 6th, 2001, 06:21 PM
#12
Thread Starter
Member
Mc Brain your code didn't work, i even tried opening Outlook Express and pasting your code and running it like that.
-
Dec 6th, 2001, 07:56 PM
#13
Thread Starter
Member
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.
-
Dec 6th, 2001, 09:05 PM
#14
Need-a-life Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|