Results 1 to 15 of 15

Thread: Help making a program that captures windows/programs into itself!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Thumbs up 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...

    What My Program Looks Like Now
    and...
    How That Program Should Work !!

    Code to my current program can be found here:
    CapturePrograms.ZIP with the code!

    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...


    My Unfinished Projects and My working Programs
    ***************

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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:
    1. Option Explicit
    2. '''FUNCTION TO RUN SHELL'ED PROGRAMS WITH'''
    3. 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
    4.  
    5. '''GETTING HWND FUNCTIONALITY'''
    6. Private Const GW_HWNDNEXT = 2
    7. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    8. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
    9. Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
    10. Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
    11.  
    12. '''DIM SOME STRING TO PICK UP THE WINDOW'S ID'''
    13. Dim PID As Long
    14. Private Const WM_CLOSE = &H10
    15. '----'
    16.  
    17.  
    18.  
    19.  
    20.  
    21. '''FUNCTION HWND'''
    22. Public Function GetHwnd(ByVal PID As Long) As Long
    23. ' Return the window handle for an instance handle.
    24.     Dim lHwnd As Long
    25.     Dim test_pid As Long
    26.     Dim Thread_ID As Long
    27.     ' Get the first window handle.
    28.     lHwnd = FindWindow(vbNullString, vbNullString)
    29.     ' Loop until we find the target or we run out
    30.     ' of windows.
    31.     Do While lHwnd <> 0
    32.         ' See if this window has a parent. If not,
    33.         ' it is a top-level window.
    34.         If GetParent(lHwnd) = 0 Then
    35.             ' This is a top-level window. See if
    36.             ' it has the target instance handle.
    37.             Thread_ID = GetWindowThreadProcessId(lHwnd, test_pid)
    38.             If test_pid = PID Then
    39.                 ' This is the target.
    40.                 GetHwnd = lHwnd
    41.                 Exit Do
    42.             End If
    43.         End If
    44.         ' Examine the next window.
    45.         lHwnd = GetWindow(lHwnd, GW_HWNDNEXT)
    46.     Loop
    47. End Function
    48.  
    49.  
    50.  
    51.  
    52.  
    53. Private Sub CommandX_Click()
    54.     'PID = Shell("notepad.exe", vbHide)
    55.     PID = Shell("notepad.exe", vbNormalFocus)
    56. End Sub
    57.  
    58.  
    59.  
    60. Private Sub Command2_Click()
    61.   ' Close the other application
    62.     Dim lngHWND As Long
    63.     lngHWND = GetHwnd(PID)
    64.     If lngHWND Then
    65.     MsgBox "OK lngHWND = " & lngHWND
    66.         SendMessage lngHWND, WM_CLOSE, 0, 0  '''I GET AN ERROR HERE'''
    67.     Else
    68.         MsgBox "Error: lngHWND = " & lngHWND
    69.     End If
    70.  
    71.  
    72.  
    73.  
    74. Private Sub Command1_Click()
    75. '''CHECKS IF YOU HAVE SELECTED TO END THE PROGAM'''
    76. If Combo1.Text = "CLOSE PROGRAM" Then
    77. Unload Form1 '''ENDS THE PROGRAM'''
    78. '''Also in the combo1's list I put, cmd.exe, notepad.exe and mspaint.exe'''
    79. Else
    80. '''IF NOT ENDING, THEN RUN THE CHOOSEN OR WRITTEN COMMAND'''
    81. PID = Shell(Combo1.Text, vbNormalFocus)
    82. 'PID = ShellExecute 0, "OPEN", ProgramText.Text, "", "", 1
    83. End If
    84. End Sub
    ***************
    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...


    My Unfinished Projects and My working Programs
    ***************

  4. #4
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    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
    IIF(Post.Rate > 0 , , )

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Re: Help making a program that captures windows/programs into itself!

    Quote 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...


    My Unfinished Projects and My working Programs
    ***************

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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??

    Code to my very close approach:
    Soure: VeryClose1.zip


    &#160;
    ***************
    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...


    My Unfinished Projects and My working Programs
    ***************

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Re: Help making a program that captures windows/programs into itself!

    Quote 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...


    My Unfinished Projects and My working Programs
    ***************

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Re: Help making a program that captures windows/programs into itself!

    I tested it fullscreen (as i replaced Explorer.exe as the OS shell)

    And that worked...
    PictureLink: Explorer as expected

    However, what seems more worrying is the fact that IE7 does not work
    while "VeryClose" replaces the Explorer.exe shell..

    PictureLink: IE7 doesnt work, IE6 (the backup for uninstall) works but is buggy

    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...


    My Unfinished Projects and My working Programs
    ***************

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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...

    Link: VeryClose3.zip


    The two previous versions that worked.. can be found here...
    Link: Previous Version VeryClose2.zip
    Link: Previous Version VeryClose1.zip
    ***************
    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...


    My Unfinished Projects and My working Programs
    ***************

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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).

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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...


    My Unfinished Projects and My working Programs
    ***************

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width