Results 1 to 15 of 15

Thread: [RESOLVED] placing another window inside of form

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Resolved [RESOLVED] placing another window inside of form

    say I have shelled an instance of the command prompt and I want that to show on my form. Another programmer told me that I need to use a picture box on the form, get the handle of the shelled program, then place the program in the picture box.

    Not sure how to do this. I can't find how to get the handle of a particular window with .net and I'm unclear on how to "place" said window in the picture box. I've looked all over my msdn and here at this forum and couldn't find anything helpful. could someone give me a hand?
    Last edited by Andy; May 18th, 2005 at 12:18 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    take a look at the setparent api, eg:
    VB Code:
    1. Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Integer, ByVal hWndNewParent As IntPtr) As Integer
    2.  
    3. SetParent (" the other app's hwnd " , " your form's hwnd ")
    you dont have to use a picturebox btw.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    thats great! now, about getting the hwnd of the shelled app? do I need a loop to search all open windows running and search somehow for the string (for example) "cmd"?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    here's what i have so far

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Shell("cmd /k cd c:\")
    3.         Dim hMainWnd As Long
    4.         Dim hChildWnd As Long
    5.         hChildWnd = FindWindowEx(0, 0, "C:\", "c:\")
    6.         hMainWnd = FindWindowEx(0, 0, "form1", "Form1")
    7.         SetParent(hChildWnd, hMainWndl)
    8.     End Sub

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you should be able to find it with Process.GetProcesses() , eg:
    VB Code:
    1. [color=blue]Dim[/color] pr [color=blue]As[/color] Process
    2.         [color=blue]For Each[/color] pr [color=blue]In[/color] Process.GetProcesses
    3.             Console.WriteLine(pr.ProcessName & "  it's handle is: " & pr.MainWindowHandle.ToInt32)
    4.         [color=blue]Next[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Shell("cmd /k cd c:\")
    3.         Dim hMainWnd As System.IntPtr
    4.         Dim hChildWnd As Long
    5.         hChildWnd = FindWindowEx(0, 0, "cmd", "c:\")
    6.         hMainWnd = Process.GetProcesses("windowsapplication2")
    7.         SetParent(hChildWnd, hMainWnd)
    8.  
    9.         'Dim pr As Process
    10.         'For Each pr In Process.GetProcesses
    11.         '    Console.WriteLine(pr.ProcessName & "  it's handle is: " & pr.MainWindowHandle.ToInt32)
    12.         'Next
    13.     End Sub

    ok, so far, so good. the one thing that's stumping me is the setparentf() the last argument is of type system.intptr and there's not a lot of info on that. (I put '0' just to have something there)

    Thanks for the help by the way
    Last edited by Andy; Dec 8th, 2003 at 06:11 AM.

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    if you put the process's window handle with .ToInt32() and your form's handle like this .. Me.Handle
    for example , once you have your window's handle...
    VB Code:
    1. Dim hwnd As Integer = " your process's handle .ToInt32()
    2. SetParent(hwnd , Me.Handle)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Thumbs up

    ok, i'm beginning to see now... Here is what I get, however, when i run it.

    Cast from string "cmd.toint32()" to type 'Integer' is not valid


    here is what I've got coded:

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Shell("cmd /k cd c:\")
    3.         Dim hwnd As Integer = "cmd.toint32()"
    4.         SetParent(hwnd, Me.Handle)
    5. end sub

    I'm getting there!!! well, you are anyway

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    BTW, if I run that loop to get the numeric value and plug it in, here is what I get:

    'toint32' is not a member of 'Integer'.

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    like this....
    VB Code:
    1. Dim pr As Process
    2.         For Each pr In Process.GetProcesses
    3.             If pr.ProcessName = "cmd" Then
    4.                 Dim hwnd As Integer = pr.MainWindowHandle.ToInt32
    5.                 SetParent(hwnd, Me.Handle)
    6.             End If
    7.         Next
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    aw man!! i've been going at this the wrong way all along!!!! Thank you VERY much.

    I can't tell you how long I worked on this tonight!


  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I'd like to thank dynamic_sysop for his help on this project I've been working on. I've run up on anothe problem though. I've got the extra window to dock within a picture box (so I could seperate it from other things) and I would like to be able to maximize the window so It's nice and pretty. I'm attaching a picture so what I'm trying to get across is easier. Does anyone know where I should start looking in the msdn to solve this problem?
    Attached Images Attached Images  

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    i've got the shell command edited to do the maximize ,(winappstyle.maximizefocus) and it does maximize it but it's not exactly staying put. I'd like the shelled window to take up the entire allocated space and make it so the user can't move it. If anyone has any information as to where in the msdn I could look, that would be greate.

  14. #14
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    Not exactly the easiest thing to do.. I tried something similar before when i was trying to make a bittorrent download manager... You could look for an api to remove the titlebar from the window or something like that, but I've found all this messing around with setparent, and other api's is kind of an unreliable way of making things...

    instead, maybe try looking at this from a different perspective? How about emulating your own console? by this i mean, just put a textbox down and run the commands in a console behind the scenes, and put the results in the textbox..

    I don't know exactly how to, but i believe there is some way to get into a command line window and get the contents in there... you may want to look around for that...

    alternatively i think there is some way to 'bounce' the results from a command line that you shell into a text file... IO Redirect i think it may be referred to as... Basically it consists of adding an extra parameter to the command line... so you could shell this:

    "netstat -n > C:\NetstatResults.txt"

    That should print out the results into the specified file.. it works here, just tested when i'm in the cmd... so you should be able to get it working as shelling a new process...

    Just some options to consider Good luck!

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    that's exactly how this project started I thought it would be neat to have the command prompt available to the user for familiarity plus, if there are commands they want to use, they can simply use the prompt. I know a guy who has done something similar but did it in vb6. I may have to resort to either older commands or doing what you suggested. This is only a "for fun" project. Something to learn by, ya know? I'll post my code when I finally figure it out though!!!

    Thanks a bunch.

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