Capturing Applications in a Form with API Calls

This is a programming tutorial showing you how to capture an application in a form using C# and API calls to unmanaged user32.dll calls.

Ever get your desktop cluttered with applications, ever wish you could group them together but not in the silly way the taskbar does? Here’s a method. (only works with NT based like XP, 2000, Vista, etc.; sidenote added for Windows 98):
Step 1:
Create a new solution in the IDE (win32 forms example);

Step 2:
Add a Picturebox to the form and dock it in the center of the screen;

Step 3:
Add a Timer to the form and set its interval to 10;

Step 4:
Double click the Timer (could manually add event for use but why bother);

Step 5:
Input this code:

System.IntPtr winParent;

System.IntPtr x;

System.IntPtr winHandle = Usr32.FindWin("ConsoleWindowClass", "Command Prompt");

if(winHandle != System.IntPtr.Zero)

{

    winParent = Usr32.GetParent(winHandle);

    x = Usr32.SetParent(winHandle, this.pictureBox1.Handle);

    x = Usr32.SetWindowPos(winHandle, 1, 10, 10, 500, 300, 0);

    this.timer1.Enabled = false;

}

Step 6:
Double click the PictureBox to add in the onclick code;

Step 7:
Input this code:

System.Diagnostics.Process bob = new System.Diagnostics.Process();

        bob.StartInfo.UseShellExecute = true;

        bob.StartInfo.Arguments += " /K TITLE Command Prompt";

        bob.StartInfo.FileName = "CMD";

        bob.Start();

this.timer1.Enabled = true;

As a side note bob is an arbitrary name, a nice short one.

Step 8:
Near the top of the code file underneath:

using System.Windows.Forms;

Paste in the code:

using System.Runtime.InteropServices;

Step 9:
After the class for the form finishes add the following code:

public class Usr32

{

    [DllImport("user32.dll", EntryPoint="FindWindow")]

    public static extern IntPtr FindWin(string lpClassName, string lpWindowName);

 

    [DllImport("user32.dll", EntryPoint="SetParent")]

    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

 

    [DllImport("user32.dll", EntryPoint="GetParent")]

    public static extern IntPtr GetParent(IntPtr hWnd);

 

    [DllImport("user32.dll", EntryPoint="SetWindowPos")]

    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter , int x , int Y , int cx , int cy , int wFlags);

 

    [DllImport("user32.dll", EntryPoint="ShowWindow")]

    public static extern IntPtr ShowWindow(IntPtr hWnd, long nCmdShow);

 

    [DllImport("user32.dll", EntryPoint="CloseWindow")]

    public static extern IntPtr CloseWindow(IntPtr hWnd);

 

    [DllImport("user32.dll", EntryPoint="DestroyWindow")]

    public static extern IntPtr DestroyWindow(IntPtr hWnd);

}

Now anybody thinking to put this in a separate file for a larger project would be right, but this is just to quickly lay down the principles to make the application work (it’s actually a port of an earlier application I made from VB 6.0, lovely)
You’re all done and this application is too however there are other controls that have the same functionality such as the tabbed view control of which there is a sister project attached as an example of a quick expansion of the project.

Sidenotes:
The Picturebox control takes up quite a bit of memory considering all it’s doing is housing the program so dont go too crazy with their use but it is good for making a large project from smaller applications.

To use this for Windows 98 and such, the code for launching a command prompt changes a wee bit:

WinNT version:

System.Diagnostics.Process bob = new System.Diagnostics.Process();

bob.StartInfo.UseShellExecute = true;

bob.StartInfo.Arguments += " /K TITLE Command Prompt";

bob.StartInfo.FileName = "CMD";

bob.Start();

this.timer1.Enabled = true;

Win9x version:

System.Diagnostics.Process bob = new System.Diagnostics.Process();

bob.StartInfo.UseShellExecute = true;

bob.StartInfo.Arguments += " ";

bob.StartInfo.FileName = "COMMAND";

bob.Start();

this.timer1.Enabled = true;

And the code for finding the window changes from version to version of Windows too:

WinNT version:

System.IntPtr winHandle = Usr32.FindWin("ConsoleWindowClass", "Command Prompt");

Win9x version:

System.IntPtr winHandle = Usr32.FindWin("tty", "MS-DOS Prompt");

It’s also useful to know that the windowclass will change from application to application

Internet Explorer = IEFrame
Outlook Express = Outlook Express Browser Class
Notepad = Notepad
Messenger Live = MSBLWindowClass
Explorer = CabinetWClass
#32770 = Display Properties in control panel and also magnifier(very useful for coding accessible apps for visually impaired)
MMCMainFrame = Services (from control Panel)

Hope you had fun,
Lewis 🙂

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top