Niloo
Niloo

Reputation: 1215

Call program windows in my application like a child

I have an application and i wan to call another program into it, like notepad.so when i minimize the notepad, or maximize, it must rest in my application.

I open application maximized, and want open notepad like a child.

I use this code

 [DllImport("user32.dll")]
 private static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

 [DllImport("User32")]
 private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

 private const int SW_MAXIMIZE = 3;

 Process p = new Process();


 private void frmMain_Load(object sender, EventArgs e)
  {
   p.StartInfo.FileName = "NOTEPAD.EXE";
   p.StartInfo.UseShellExecute = true;
   p.Start();
   // change parent window and maximize inside the form
   SetParent(p.MainWindowHandle, this.Handle);
   ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);
  }

But don't work!!! ()

Upvotes: 0

Views: 210

Answers (1)

David Heffernan
David Heffernan

Reputation: 613612

It is possible to do this, but it is fiendishly difficult to get it right. Raymond Chen covered the topic in some detail: Is it legal to call have a cross-process parent/child or owner/owned window relationship?

A customer liaison asked whether it was legal to use Set­Parent to create a parent/child relationship between windows which belong to different processes.

......

So yes, it is technically legal, but if you create a cross-process parent/child or owner/owned relationship, the consequences can be very difficult to manage. And they become near-impossible to manage if one or both of the windows involved is unaware that it is participating in a cross-process window tree. (I often see this question in the context of somebody who wants to grab a window belonging to another process and forcibly graft it into their own process. That other process was totally unprepared for its window being manipulated in this way, and things may stop working. Indeed, things will definitely stop working if you change that other window from a top-level window to a child window.)

So, you can do this. But it is not easy, and absolutely not recommended. It's hard enough to do in C++, but from the managed world you are just asking for pain. The most sane advice we can give you is to find a different solution to your problem.

Upvotes: 3

Related Questions