Help making a program that captures windows/programs into itself!
Hi all...
Long time, no see... hehe
I've gotten rusty in coding and need some help making the following...
A program that can open any program (this part is easy)
and then capture the opened window into a Picturebox (harder)
and then capture the name/icon of the opened program into a second Picturebox,
so that when you click the icon/image, you bring the captured window/program to the front inside the first picturebox...
I have two pictures that explains all this a little better...
So... is there anyone out there, with the knowlegde of how to go about this?
I know we have to do some .hwnd code, preferrably catching the PID and not the WINDOW title, as that differs from system to system, and from language to language...
And yes, this program will act as a Explorer Shell Replacement...
This may ofcourse not be the END-DESIGN of it... but it will have to do for now until the code is working better...
This is to be ussed in locked down kiosk machines...
And the drop-down list, will be replaced by bigger icons at the very "desktop" (this programs main surface... hehe)
More of that later, when working code has been archieved...
Last edited by alexdata; May 31st, 2008 at 06:49 PM.
*************** Please use [highlight=vb]..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
Re: Help making a program that captures windows/programs into itself!
Well i have tested the following code, giving me an error
You need a Command1 and a Command2 button, and a combo with names of a few programs in its list/text (cmd.exe, notepad.exe, mspaint.exe)
But this code trows me an error..
I've commented in the code where it gives me the error!!
vb Code:
Option Explicit
'''FUNCTION TO RUN SHELL'ED PROGRAMS WITH'''
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal Hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'''GETTING HWND FUNCTIONALITY'''
Private Const GW_HWNDNEXT = 2
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
'''DIM SOME STRING TO PICK UP THE WINDOW'S ID'''
Dim PID As Long
Private Const WM_CLOSE = &H10
'----'
'''FUNCTION HWND'''
Public Function GetHwnd(ByVal PID As Long) As Long
' Return the window handle for an instance handle.
Re: Help making a program that captures windows/programs into itself!
Code:
SendMessage lngHWND, WM_CLOSE, 0, 0 '''I GET AN ERROR HERE'''
you should get an error here . You havent declared SendMessage
Code:
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Re: Help making a program that captures windows/programs into itself!
Originally Posted by RhinoBull
@alexdata:
sample code that you found doesn't look like what you're after - as I said most likely you only need two functions: SetParent and FindWindow.
Yes.. I know I need setparent.. but as I said in the beginning of the post, i havent been coding in a long time, and have forgotten about it... So I kinda have to figure out one thing, ahead of the other...
And with SetParent, i want to use the PID and not the "window title" that is different from system to system, and from OS language to language...
So thats why im digging into the PID stuff...
Do you have any code examples by the way?
I need to be able to...
- open the program from mine (writing its name (and maybe path))
- get its PID, so that i dont have to use window titles
- use setparent to bind it to the picturebox or the form...
- Somehow get the icon/name and also put into a pic.box
This is going to act like a shell replacement after all...
*************** Please use [highlight=vb]..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
Re: Help making a program that captures windows/programs into itself!
Well..
Here is some code that opens a program and captures it into the form itself
strange thing is, I can only make it capture NOTEPAD, all other apps "escape"
or doesnt get captured... why is that?? anyone??
Re: Help making a program that captures windows/programs into itself!
Included is a simple hack I came up with for an older personal program, as far as I remember it worked fine for regular programs but doesn't seem to be working on explorer.exe. I have some other API code that's supposed to return the correct handle but IIRC even that wasn't perfect, though I can't seem to find it to test it out, oh well if you do find the absolute perfect solution please share.
Last edited by Edgemeal; Jun 22nd, 2008 at 06:12 AM.
Reason: OLD
Re: Help making a program that captures windows/programs into itself!
Originally Posted by Edgemeal
Included is a simple hack I came up with for an older personal program, as far as I remember it worked fine for regular programs but doesn't seem to be working on explorer.exe. I have some other API code that's supposed to return the correct handle but IIRC even that wasn't perfect, though I can't seem to find it to test it out, oh well if you do find the absolute perfect solution please share.
I will take a look...
I guess the EXPLORER.EXE is heavily tied to the WINDOWS DESKTOP so unless
you REPLACE your WindowsDesktop (the explorer.exe) then you cannot fully free it from the WINDOWS CORE... the rest of the system....
But ofcourse, i'll take a look, and share my results along the way!
*************** Please use [highlight=vb]..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
Re: Help making a program that captures windows/programs into itself!
I figured a way to get explorer.exe in there too. Another hack.... I had to change your FindWindow API,
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Code:
''''Lock the window update
LockWindowUpdate GetDesktopWindow
If LCase$(Combo1.Text) = "explorer.exe" Then
PID = Shell(Combo1.Text, vbNormalFocus)
Sleep 100
If PID > 0 Then mWnd = FindWindow("ExploreWClass", "My Documents")
Else
'Launch and retrieve the handle to the selected program..
mWnd = Launch(Combo1.Text)
End If
If mWnd = 0 Then
MsgBox "Error getting handle to the program"
Exit Sub
End If
'Set the selected programs new parentwindow
SetParent mWnd, Me.hwnd
Last edited by Edgemeal; Jun 2nd, 2008 at 05:06 AM.
Reason: FIX - ELSE END IF order
I think maybe I should try to implement your code there, and then we should find something similar for IE7, as it seems dependent on Explorer.exe as a shell... I dont know why.. But it does not load, and it freezes its window, if i kill it in taskMGR and restart it, then it manages to get the title of your homepage, but displays nothing, and then freezes...
(Ohh if the MS CORE was not so dependent on explorer.exe)
*************** Please use [highlight=vb]..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
Re: Help making a program that captures windows/programs into itself!
Okey... I added your latest code.. but this gives me the error
"Error getting handle to the program" all the time...
It starts the different programs in the list, but does not manage to capture them...
Re: Help making a program that captures windows/programs into itself!
I tested Notepad and MsPaint and they seem to work fine, but for explorer you probably need to do a similar type of loop as in my module, wait till the PID and handle are not = 0, and if not found in 1 or 2 secs then exit the loop and show a not found message. This is really a hack, and a really busy or really slow PC may not always work as expected, you may want to look at other APIs to find out when the window is ready instead of just waiting in a loop.
I see one possible problem here, if no handle is found the LockWindowUpdate wasn't reset.
Code:
If mWnd = 0 Then
LockWindowUpdate False
MsgBox "Error getting handle to the program"
Exit Sub
End If
One thing you really need to consider is the closing of the programs, you really shouldn't assume the handle you are passing is the one you need or a valid one, and if more then one program was open then you need to close all of them, you probably want to store the Handles or the PIDs in an array after a program opens successfully, then when your program is about to exit you would loop through the array and if the Handle/PID is found running then destroy it or whatever, tho if it were me I'd probably send the program a WM_Close or WM_Quit message instead of trying to kill the process or destroy the window.
You may also need to check how many programs are running every so often and if the count changes check your PID array against those PIDs, cause what happens if I close an app inside your program then open 100 different programs outside your program and it just so happens to have the same handle or PID in your array?, you could end up closing the wrong program(s).
Re: Help making a program that captures windows/programs into itself!
[QUOTE=Edgemeal]One thing you really need to consider is the closing of the programs, you really shouldn't assume the handle you are passing is the one you need or a valid one, and if more then one program was open then you need to close all of them, you probably want to store the Handles or the PIDs in an array after a program opens successfully, then when your program is about to exit you would loop through the array and if the Handle/PID is found running then destroy it or whatever, tho if it were me I'd probably send the program a WM_Close or WM_Quit message instead of trying to kill the process or destroy the window.
QUOTE]
Could this array that you're talking about, be used also to make a list of each running program, so that we could make one button/icon for each, and if we click that, then we could bring that app to the front/set focus on it?
Same as the explorer.exe's start bar does to make the buttons we click to show the programs that we've minimized...
*************** Please use [highlight=vb]..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
Re: Help making a program that captures windows/programs into itself!
Sounds like you got your work cut out for you!
Maybe this will help somewhat in your journey, I slapped together a simple example on how to get a launched programs titlebar text and its icon and add it to a VB command button. Clicking the button will maximize the program. Note this example is only for one program (just one button) and the programs don't get killed/destroyed on exit, (i removed code) so close em before you exit your app.
The button is a control array so as you load programs you'd have to load the next button, position it, etc,etc. Sounds like a lot of stuff to keep track of and not as simple as it looks, I've done a similar program and its almost impossible to follow now even with all the code commented.