|
-
Jul 27th, 2004, 02:25 PM
#1
Thread Starter
Hyperactive Member
Grab a window into a form.
Ive seen this around the boards but its not what I want that I have found.
I want my program, when a button is clicked, it looks for the process RunUO.exe and grabs it and puts it into my form.
The program RunUO.exe will be runnning before my program is started. And I just want to grab it and put it in my program so I can use its class's to setup an admin program for it.
If my post was helpful please rate it 
-
Jul 28th, 2004, 01:43 AM
#2
How exactly do you mean in your window? Is the SetParent API what you are looking for maybe?
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Load()
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
Dim Pid As Long
'Lock the window update
LockWindowUpdate GetDesktopWindow
'Execute notepad.Exe
Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
mWnd = InstanceToWnd(Pid)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on notepad
Putfocus mWnd
'Unlock windowupdate
LockWindowUpdate False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Unload notepad
DestroyWindow mWnd
'End this program
TerminateProcess GetCurrentProcess, 0
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Jul 28th, 2004, 05:47 AM
#3
Thread Starter
Hyperactive Member
Ive tried this and make it shell my program, but my program doesnt load into the form... its still out side of it in its own window...
If my post was helpful please rate it 
-
Jul 28th, 2004, 08:25 AM
#4
Thread Starter
Hyperactive Member
manavo11, you gave me an idea with this. I got what I needed so far but it wont grab my program.
Is there a way I can program a button to when hit, grab the window of the program thats already running any put it in the form?
That would be awsome if you could help me with that.
If my post was helpful please rate it 
-
Jul 29th, 2004, 03:38 AM
#5
Well, the difference would be that you won't need the InstanceToWnd function. You can just use the FindWindow API to get the hwnd of the window. You skip the starting the application part, and use the FindWindow API to get the hwnd by using either the classname of the window title (caption).
VB Code:
'The other declarations are in the other posts
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Form_Load()
mWnd = FindWindow("classname", "window caption")
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on the other application
Putfocus mWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Unload the other application
DestroyWindow mWnd
'End this program
TerminateProcess GetCurrentProcess, 0
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Jul 29th, 2004, 07:01 AM
#6
Thread Starter
Hyperactive Member
Thanks for the reply, but how would I use that as in the class name and the window caption. I understand about the window name just not the class name.
Could you elaberate for me?
If my post was helpful please rate it 
-
Jul 29th, 2004, 09:11 AM
#7
-
Jul 29th, 2004, 03:59 PM
#8
Thread Starter
Hyperactive Member
I get a type mismatch error when I try setting it up like this:
Private Sub Command2_Click()
Dim ServerPath As String
ServerPath = Text1.Text
mWnd = FindWindow(vbNullString, ServerPath)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on the other application
Putfocus mWnd
End Sub
I cant use the direct name of the window because the window name is setup like diffrent for everyone. The window name is:
Directory The Server Is In \ RunUO.exe
Mine is:
C:\Program Files\RunUO Software Team\RunUO 1.0 RC0\RunUO.exe
If I had it on my desktop it would change to:
C:\Documents and Settings\Administrator\Desktop\RunUO 1.0 RC0\RunUO.exe
Just wondering if what I did would be the right way. I thought it would be but its not working.
Yea... could ya help me again?
And thanks for the support so far!
If my post was helpful please rate it 
-
Jul 29th, 2004, 05:31 PM
#9
Frenzied Member
I don't think what you are trying to accomplish is possible. btw it would be
dim x as long
dim strwindtitle as string
strwindtitle = "Window Title"
x = findwindow("classnamehere",vbnullstring)
or if you know window caption then
dim x as long
dim strwindtitle as string
strwindtitle = "Window Title"
x = findwindow(vbnullstring, strwindTitle)
ice
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 29th, 2004, 05:40 PM
#10
Thread Starter
Hyperactive Member
Ice thanks for the reply but I get this error:
Type Mismatch and it highlights
x = findwindow(vbnullstring, strwindTitle)
Mind you that I use this line above it:
strwindtitle = Text1.Text
I have to use Text1.Text because the window title changes depending on installation point of the main program.
So Im not sure if thats making a problem with this...
If my post was helpful please rate it 
-
Jul 29th, 2004, 05:45 PM
#11
Frenzied Member
well i was just doing that as an example. but if you read my first sentence .." i dont think what your trying to do is possible" lol
but anyways... how are you finding the window caption? You can find handles based onpartial window caption such as..
VB Code:
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
' this is where it searches partial caption
If InStr(sSave, "partial cap here") Then
lngRhWnd = Str$(hwnd)
strClassname = GetClass(lngRhWnd)
strWindowTitle = GetWinTitle(lngRhWnd)
End If
EnumWindowsProc = True
End Function
the above is not all the code but simply an example... im sure if you look in the aPI forum you will find many examples or even the codebank. [ i cant get to my projects right now to post all of it lol ]
ice
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 30th, 2004, 03:17 AM
#12
Thread Starter
Hyperactive Member
Ok I put the function into my form...
But I changed the first api call to load my program, to not load on Forum load. It loads when I press a button.
How would I use what you gave me with another button to check if the sSave from that function is "RunUO.exe" (The partial window name) and if its true, then it will grab it?
If my post was helpful please rate it 
-
Jul 30th, 2004, 03:37 AM
#13
-
Jul 30th, 2004, 03:45 AM
#14
Thread Starter
Hyperactive Member
Ok I got that program, suprisingly I had it already, I forgot about it though lol. The window class name is ConsoleWIndowClass. So I should take the code you exampled and put that in and test it out.
If my post was helpful please rate it 
-
Jul 30th, 2004, 03:48 AM
#15
Frenzied Member
Yes if you use that classname with the code manavo posted. IM not sure your understanding me though, that will only allow you to FIND the window, just getting the handle by searching for the windows classname is not going to grab the window and place it onto your form. ( if thats what you are wanting to accomplish )
I don't believe this is possibe through VB....if it is i'd sure love to see the code
ice
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 30th, 2004, 04:00 AM
#16
Thread Starter
Hyperactive Member
Ice, it is possible to do this because I can grab anything else with this code. Its just not letting me grab this window for some reason.
I can grab the game client for this game and put it into the window, but I cant seem to get it to let me grab the server still even with the class name.
When I put this:
Code:
mWnd = FindWindow(ConsoleWindowClass, vbNullString)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on the other application
Putfocus mWnd
I get the Type Mismatch error. I then tried changing the vbNullString with Text1.Text sence the path for the exe is in Text1.Text and it also is the window title for the server. But I still get the Type Mismatch error.
If I try to use part of the server title I get object required sence its not the full name of the window. And I cant seem to get your code, ice, to get partial of the window name to work with this.
If my post was helpful please rate it 
-
Jul 30th, 2004, 04:02 AM
#17
Frenzied Member
what code you using to grab another window then?
id like to see it...the code you just posted would not grab a window and place it onto your form
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 30th, 2004, 04:06 AM
#18
Thread Starter
Hyperactive Member
Ok here is the code Im using so far, the entire thing...
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Then I changed around the usuall stuff to get it the way I want it:
Code:
Private Sub Form_Load()
On Error GoTo ErrorHandle
Dim a As Integer
Dim ff As Integer
a = 0
ff = FreeFile
Open App.Path & "\proxycfg.dat" For Input As ff
Do While Not EOF(ff)
Input #ff, Woot
Loop
Close ff
Text1.Text = Woot
Exit Sub
ErrorHandle:
MsgBox "Error", vbOKOnly, "Error!"
End Sub
Then I changed the form load to not start the program and put that code into a command button instead:
Code:
Private Sub Command1_Click()
Dim Pid As Long
'Lock the window update
LockWindowUpdate GetDesktopWindow
'Execute notepad.Exe
Pid = Shell(Text1.Text, vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
mWnd = InstanceToWnd(Pid)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on notepad
Putfocus mWnd
'Unlock windowupdate
LockWindowUpdate False
End Sub
That all works... just without grabbing my window...
Anything you guys are telling me right now is getting put into a second command button. So that you load the program with 1 button, and grab it with another.
If my post was helpful please rate it 
-
Jul 30th, 2004, 04:15 AM
#19
Frenzied Member
I meant did you have working code that will acually grab another window ( eg. aim instant message window ) and place it onto your form....
all of the code you posted is doing the searching for the window and finding it. but does not "grab" the window (which is what you want......) Im tryint to say i don't believe it is possible to acually grab the window and place it on your form....
im done with this now lolol i obviously am confussed at this point or you are misunderstanding me... i dunno but ill wait and see some others replies to the thread and maybe ill get back on track
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 30th, 2004, 04:30 AM
#20
Thread Starter
Hyperactive Member
The original code that was posted did grab notepad and any other programs, but I changed it around to do it when you press command buttons. It works with anything but the server program Im tryin to do it with thats why Im askin how to get it to grab the server window.
If my post was helpful please rate it 
-
Jul 30th, 2004, 04:59 AM
#21
-
Jul 30th, 2004, 05:10 AM
#22
Try this (You have to enclose the classname in quotes) :
VB Code:
Private Sub Command1_Click()
mWnd = FindWindow("ConsoleWindowClass", vbNullString)
SetParent mWnd, Me.hwnd
Putfocus mWnd
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Jul 30th, 2004, 05:12 AM
#23
-
Jul 30th, 2004, 05:23 AM
#24
Thread Starter
Hyperactive Member
ACtually I got it to work 
In my command1 button I had
Code:
Dim Pid As Long
'Lock the window update
LockWindowUpdate GetDesktopWindow
'Execute notepad.Exe
Pid = Shell(Text1.Text, vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
mWnd = InstanceToWnd(Pid)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on notepad
Putfocus mWnd
'Unlock windowupdate
LockWindowUpdate False
Which would launch the program but not grab it. After two days of you guys helping me out with this and me getting fustrated with it, I added this simple line of code to the command button:
Code:
If Pid = Pid Then
LockWindowUpdate GetDesktopWindow
mWnd = InstanceToWnd(Pid)
SetParent mWnd, Me.hwnd
Putfocus mWnd
LockWindowUpdate False
End If
And it works Thanks so much guys for your help and support. Couldnt of done this with out you guys
If my post was helpful please rate it 
-
Jul 30th, 2004, 05:33 AM
#25
Thread Starter
Hyperactive Member
Hmm wait I take that back its not working now.
If I set it up like this:
If Pid = Text1.Text Then
And start it and press command 1 it will start the server then get an error about Type Mismatch.
While in debug mode if I change it to
If Pid = Pid then
And press the play button again it will grab it.
Any idea's around that?
If my post was helpful please rate it 
-
Jul 30th, 2004, 05:34 AM
#26
Thread Starter
Hyperactive Member
Originally posted by manavo11
Try this (You have to enclose the classname in quotes) :
VB Code:
Private Sub Command1_Click()
mWnd = FindWindow("ConsoleWindowClass", vbNullString)
SetParent mWnd, Me.hwnd
Putfocus mWnd
End Sub
Still gets a type mismatch error.
If my post was helpful please rate it 
-
Jul 30th, 2004, 05:38 AM
#27
-
Jul 30th, 2004, 05:40 AM
#28
Thread Starter
Hyperactive Member
Well... Im trying to get it while its running, but if we can get it to grab it when it starts that will be perfect also.
And yes, the error is on the Findwindow line.
If my post was helpful please rate it 
-
Jul 30th, 2004, 05:44 AM
#29
Thread Starter
Hyperactive Member
If its already running then would I be able to grab it by the name that it is in Task manager?
Cause in task manager its only RunUO.exe
So I figured it would be easier to do it that way... but Im only suggesting.
If my post was helpful please rate it 
-
Jul 30th, 2004, 05:51 AM
#30
-
Jul 30th, 2004, 06:00 AM
#31
Thread Starter
Hyperactive Member
Originally posted by manavo11
Turns out to be something silly... The example I posted had a slightly different declaration for the FindWindow API than the usual. It had the classname and windowtitle parameters as Long instead of string Use this declaration instead :
VB Code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
This is for the Type Mismatch by the way
Manavo11, lemme just say, I LOVE YOU!!!! Lol. Anyway, that worked perfectly man. Thanks so much.
Thanks for your support and help also ice.
You guys rock!
One more question.
mWnd = FindWindow("ConsoleWindowClass", vbNullString)
SetParent mWnd, Me.hwnd
Putfocus mWnd
Is there anyway to remove the window that I captured into my program and put it back onto my desktop?
If my post was helpful please rate it 
-
Jul 30th, 2004, 06:47 AM
#32
Thread Starter
Hyperactive Member
Never mind figured out how to do that also.
Just made another command button and put in:
SetParent mWnd, GetDesktopWindow
Putfocus mWnd
If my post was helpful please rate it 
-
Jul 30th, 2004, 08:24 AM
#33
Thread Starter
Hyperactive Member
Along with the capture command is there a way to set it to a certain spot on my form and lock it at that spot so the user cant move it?
If my post was helpful please rate it 
-
Jul 30th, 2004, 08:39 AM
#34
-
Jul 30th, 2004, 09:45 AM
#35
Thread Starter
Hyperactive Member
Thanks again manavo.
Anyway, to lock it in that spot so the user can move the window?
If my post was helpful please rate it 
-
Jul 30th, 2004, 09:55 AM
#36
-
Jul 30th, 2004, 10:09 AM
#37
Thread Starter
Hyperactive Member
Yea, before you posted it I searched the forums and found that declair in another post you did in the API section and test it out. It took me about 5 minutes to set the window in the right spot and get it to the right size but it works perfect for me.
I would rather not put in a timer to not move the window. The user would be able to move it and it would jump back.
I would rather have it just stay there no matter what.
Thanks again.
If my post was helpful please rate it 
-
Jul 30th, 2004, 10:26 AM
#38
-
Jul 30th, 2004, 05:45 PM
#39
Thread Starter
Hyperactive Member
Could you give an example of this? Im pretty sure it would have to use a timer mostlikly but not sure.
Thanks again.
If my post was helpful please rate it 
-
Aug 2nd, 2004, 02:00 AM
#40
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
|