[Partly Resolved] Executing & getting hwnd
Hey i know how to manipulate windows, but my problem is getting the hWnd for a window. I'm working on a program that executes other outside programs and then manipulates the size & the window state.
I need to know how to launch a program using the Shell command (with out knowing the caption, or at least find it when it's opened) so that i can manipulate the window. The program can either launch ontop or behind, minimized, normal, maximized or full screen. But my only problem is finding the hWnd.
I've checked for code on here for ages and i can't find any that works. Can some one please shed some light for me?
Re: Executing & getting hwnd
Re: Executing & getting hwnd
But there's no way to get the hWnd by executing it, or the caption after it's been executed (since it may go behind or ontop).
Re: Executing & getting hwnd
It doesnt matter what the ZOrder of the window is or even if its visible or not. Using the FindWindow API you can get the windows handle after its created.
Re: Executing & getting hwnd
How? Where's the hWnd stored? How can i find that exact exe that i executed?
Re: Executing & getting hwnd
Use FindWindow and pass the window classname of your desired window (does not change and can be found with Spy++) and the window caption if you want. If you have multiple windows of the same class name then you will need to enumerate them and choose the desired one.
Re: Executing & getting hwnd
umm, Is it possible to get the hWnd automatically? I'm remaking a program that stops games going full screen (like CS or what ever other games do) by grabbing the hWnd of it & stopping it. The only problem is that i don't know the caption of every game & moving the mouse onto it with that Spy++ thing won't work because i'd need to give it out & tell people how to use it. I know it is possible DxWnd is where i got the idea from, but i'm making my own version of it. Is somthing like this even possible in VB? I wouldn't know...
Re: Executing & getting hwnd
here's a Shell2hWnd function:
VB Code:
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" ( _
ByVal hwnd As Long) As Long
Private Const GW_HWNDNEXT = 2
Public Function Shell2hWnd(ByVal sFileName As String, Optional ByVal Mode As VbAppWinStyle) As Long
Dim lTaskID As Long, lProcID As Long
lTaskID = Shell(sFileName, Mode)
If lTaskID = 0 Then Exit Function
Shell2hWnd = FindWindow(vbNullString, vbNullString)
Do While Shell2hWnd
If GetParent(Shell2hWnd) = 0 Then
GetWindowThreadProcessId Shell2hWnd, lProcID
If lProcID = lTaskID Then Exit Function
End If
Shell2hWnd = GetWindow(Shell2hWnd, GW_HWNDNEXT)
Loop
Shell2hWnd = 0
End Function
Private Sub Form_Load()
Dim lhWnd As Long
lhWnd = Shell2hWnd("notepad.exe", vbNormalFocus)
End Sub
Re: Executing & getting hwnd
Yes, this works, but only somtimes! I used that code on a dud program i made for testing (just nothing in the program) and somtimes it gets the correct hWnd, but other times it doesn't. I have code checking the hWnd of the window from it's caption (because i'm testing) sometimes the hWnds match, other times they don't. Sometimes that code works with that hWnd, somtimes the other hWnd works, sometimes none of them work. The code is still in early development so there's nothing screwing around with any processes or memory.
Why does this happen?
Thank you for your help by the way.
Re: Executing & getting hwnd
If you checking the window caption for verification then why not use code like the second link I posted earlier?
Re: Executing & getting hwnd
Because i was testing it (sometimes it worked & sometimes it didn't) so i used a different code so check what wasn't working, which needed a window caption.
I don't know the window captoin, but while i'm testing it i do it in controled enviroment so that i can find out what's wrong.
Re: Executing & getting hwnd
You could do it like this:, Use a timer and get the mouse position then get the hWnd with API Function WindowFromPoint.
ex:
Code:
Private Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursor Lib "user32" Alias "GetCursor" () As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Timer1_Timer()
Dim lp As POINTAPI, value As Long
GetCursorPos lp
value& = WindowFromPoint(lp.x,lp.y)
Label1.Caption = value&
End Sub
Re: Executing & getting hwnd
So like make the mouse jump onto the program once it's started? ok, that will have to do, thank you!
Re: Executing & getting hwnd
Quote:
Originally Posted by bushmobile
here's a Shell2hWnd function:
VB Code:
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" ( _
ByVal hwnd As Long) As Long
Private Const GW_HWNDNEXT = 2
Public Function Shell2hWnd(ByVal sFileName As String, Optional ByVal Mode As VbAppWinStyle) As Long
Dim lTaskID As Long, lProcID As Long
lTaskID = Shell(sFileName, Mode)
If lTaskID = 0 Then Exit Function
Shell2hWnd = FindWindow(vbNullString, vbNullString)
Do While Shell2hWnd
If GetParent(Shell2hWnd) = 0 Then
GetWindowThreadProcessId Shell2hWnd, lProcID
If lProcID = lTaskID Then Exit Function
End If
Shell2hWnd = GetWindow(Shell2hWnd, GW_HWNDNEXT)
Loop
Shell2hWnd = 0
End Function
Private Sub Form_Load()
Dim lhWnd As Long
lhWnd = Shell2hWnd("notepad.exe", vbNormalFocus)
End Sub
Its cool to see code you write, appear months later with a different name :lol:
http://vbforums.com/showthread.php?t...t=Shellandfind :ehh:
Re: [Partly Resolved] Executing & getting hwnd
man, people can be bitchy, this was where i modified it from: http://www.digital-inn.de/visual-bas...s-steuern.html
Re: [Partly Resolved] Executing & getting hwnd
Quote:
Originally Posted by bushmobile
Man you know I was only kidding :afrog: