Using MDI Form perhaps?
Just wanna know before I start pouring energy into it and then find out it can't be done.
This post has been edited by papuccino1: 09 June 2009 - 05:33 PM
|
|
|
|
Is it possible to open a completely foreign software inside your Windo
Page 1 of 1
Is it possible to open a completely foreign software inside your Windo#1Posted 09 June 2009 - 05:32 PM
I'm having some ideas for my next project and I gotta strike while the iron is hot. Is it possible to have a program open inside your WindowsForm? To be clear, I mean literally a completely different program. Imagine a create a WinForm with two buttons, I click button1 and software X appears inside that WinForms as a child.
Using MDI Form perhaps? Just wanna know before I start pouring energy into it and then find out it can't be done. This post has been edited by papuccino1: 09 June 2009 - 05:33 PM #8Posted 09 June 2009 - 08:35 PM
create a form with a transparant background and size it based on the target programs coordinates.
To get a foreign window, need to use winAPI library so you have to import it you might have to do more research but these winApi functions should get you started
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
e
some c# code, you should get the idea
int hwnd=0;
IntPtr hwndChild=IntPtr.Zero;
//Get a handle for the Calculator Application main window
hwnd=FindWindow(null,"Calculator");
if(hwnd == 0)
{
if(MessageBox.Show("Couldn't find the calculator" +
" application. Do you want to start it?",
"TestWinAPI",
MessageBoxButtons.YesNo)== DialogResult.Yes)
{
System.Diagnostics.Process.Start("Calc");
}
}
else
{
//Call GetWindowPlacement() here, then place your window around it
}
This post has been edited by fremgenc: 09 June 2009 - 08:36 PM #10Posted 10 June 2009 - 07:14 AM Quote I'm having some ideas for my next project and I gotta strike while the iron is hot. Is it possible to have a program open inside your WindowsForm? To be clear, I mean literally a completely different program. Imagine a create a WinForm with two buttons, I click button1 and software X appears inside that WinForms as a child. Using MDI Form perhaps? Just wanna know before I start pouring energy into it and then find out it can't be done. There is actually a pretty easy way to do this. First, you need to add a panel to your form. This panel will be used to "host" the application. Next, you need to the "System.Runtime.InteropServices" and the "System.Diagnostics" namespace to your namespaces: using System.Diagnostics; using System.Runtime.InteropServices; Now, we need setup our WinAPI functions:
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam);
Now, inside the button click event, start the process, and set it's parent to the panel. In this example, I will be using notepad:
// Create a new process
Process proc;
// Start the process
proc = Process.Start("notepad.exe");
proc.WaitForInputIdle();
// Set the panel control as the application's parent
SetParent(proc.MainWindowHandle, this.panel1.Handle);
// Maximize application
SendMessage(proc.MainWindowHandle, 274, 61488, 0);
Hope this helps! **EDIT** In "SendMessage()", 274 and 61488 are commands used to maximize the application. 274 represetns the "WM_SYSCOMMAND" message, which is the message that is sent to a window when the user chooses a command from the window menu. 61488 represents the "SC_MAXIMIZE" wParam, or the type of system command to use. So, by calling "SendMessage()" with these parameters, you are telling the application to maximize itself. **EDIT** This post has been edited by lesPaul456: 10 June 2009 - 09:49 AM #15Posted 10 June 2009 - 08:35 AM
lesPaul, how the hell do you know this?! LOL! I've done a good amount of game programming, in both XNA and MDX. In order to host both of these in a windows forms application, you have do do something similar to this (although similar, the method to host XNA and MDX in a winform app is still very different). This code has proven particularly useful for hosting games created using the DarkGDK (which uses C++) in C# or VB.Net applications. This post has been edited by lesPaul456: 10 June 2009 - 08:36 AM #16Posted 10 June 2009 - 10:10 AM
lesPaul you're fucking kidding me!? This is exactly what I was looking for!
You're my new favorite person. Now, I just need a way so the window stays put. Using the code above I can move the window around (but only in my form). Who the hell knew C# was this powerful and didn't tell me?! Thanks Paul! #17Posted 10 June 2009 - 11:22 AM
Glad I could help!
I have yet to come up with a solution to prevent the application from moving. I'll look into it later today. I forgot to mention something earlier; It seems this code doesn't work right with applications created using the .Net Framework, only Win32 applications. I only really use this code, as I mentioned, for games created using the DarkGDK, since they are Win32 apps. I plan on looking into a solution to hosting .Net applications as well, but this may take some time. Anyway, if I find a way to keep the window stationary, I'll be sure to post back. #18Posted 10 June 2009 - 03:28 PM
I figured out how to prevent the user from moving the window.
First, you need to add two more native methods:
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
public static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
Next, you need to get the windows menu: IntPtr hWnd = GetSystemMenu(process.MainWindowHandle, false); And now we stop the user from moving the window: RemoveMenu(hWnd, 0xF010, 0x00000000 | 0x00001000); That's it! Hope this helps!
Page 1 of 1
Fast Reply
1 User(s) are reading this topic
|
||||||||
Forum Index:
Programming Help |
C and C++ |
VB6 |
Java |
VB.NET |
C# |
ASP.NET |
PHP |
ColdFusion |
Perl and Python |
Ruby |
Databases |
Other Languages |
Game Programming |
Mobile Development |
Software Development |
Computer Science |
Industry News |
52 Weeks Of Code |
Programming Tutorials |
C++ Tutorials |
Visual Basic Tutorials |
Java Tutorials |
VB.NET Tutorials |
C# Tutorials |
Linux Tutorials |
PHP Tutorials |
ColdFusion Tutorials |
Windows Tutorials |
HTML/JavaScript Tutorials |
CSS Tutorials |
Flash Tutorials |
Web Promotion Tutorials |
Graphic Design & Photoshop Tutorials |
Software Development Tutorials |
Database Tutorials |
Other Language Tutorials |
ASP.NET Tutorials |
Game Programming Tutorials |
WPF & Silverlight Tutorials |
Mobile Development Tutorials |
Python Tutorials |
Ruby Tutorials |
