|
-
Apr 27th, 2006, 02:46 AM
#1
Thread Starter
Fanatic Member
[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?
Last edited by Slyke; Apr 30th, 2006 at 10:14 AM.
-
Apr 27th, 2006, 02:54 AM
#2
Re: Executing & getting hwnd
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 27th, 2006, 03:03 AM
#3
Thread Starter
Fanatic Member
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).
-
Apr 27th, 2006, 03:17 AM
#4
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 27th, 2006, 03:33 AM
#5
Thread Starter
Fanatic Member
Re: Executing & getting hwnd
How? Where's the hWnd stored? How can i find that exact exe that i executed?
-
Apr 27th, 2006, 04:05 AM
#6
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 27th, 2006, 04:50 AM
#7
Thread Starter
Fanatic Member
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...
-
Apr 27th, 2006, 06:01 AM
#8
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
-
Apr 27th, 2006, 07:35 AM
#9
Thread Starter
Fanatic Member
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.
-
Apr 27th, 2006, 10:02 AM
#10
Re: Executing & getting hwnd
If you checking the window caption for verification then why not use code like the second link I posted earlier?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 29th, 2006, 09:54 PM
#11
Thread Starter
Fanatic Member
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.
-
Apr 29th, 2006, 10:49 PM
#12
Addicted Member
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
-
Apr 30th, 2006, 10:13 AM
#13
Thread Starter
Fanatic Member
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!
-
Apr 30th, 2006, 10:54 AM
#14
Re: Executing & getting hwnd
 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
http://vbforums.com/showthread.php?t...t=Shellandfind
-
Apr 30th, 2006, 02:50 PM
#15
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
Last edited by bushmobile; Apr 30th, 2006 at 02:54 PM.
-
May 1st, 2006, 08:56 PM
#16
Re: [Partly Resolved] Executing & getting hwnd
 Originally Posted by bushmobile
Man you know I was only kidding
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
|