Problems in setparent new cmd windows 10

1k views Asked by At

For some time, I have created a console application (cmd & powershell) by integrating the multi tab. I'm using the setparent function of user32. Everything works except when I move the parent window. It is impossible to access the child window. It is visible but impossible to click on it. To remedy this, the parent window must be replaced where it was. I noticed that this "bug" only appears with the new Windows 10 console.

I do not know how to do.

(Sorry for my bad English I'm French)

2

There are 2 answers

0
TheRake66 On

I will try to explain better. I start a child window at a position, if I move the parent window, the child window will be deactivated. Like this :

Start set parent to child window... enter image description here

Move parent window.. enter image description here

And the problem appears..

Here is the code:

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


[... Code ...]


Process process = new Process();
process.StartInfo.FileName = pProcess;
process.StartInfo.Arguments = pArgs;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler((s, e) =>
{
    FermerOnglet(tabpage);
});

process.Start();
while (process.MainWindowHandle == (IntPtr)0)
{
}
SetParent(process.MainWindowHandle, metroPanel1.Handle);
0
TheRake66 On

I finally found the solution, it was not that complicated but I did not understand why it did that.

Solution :

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

[DllImport("user32.dll")]
static extern long SetWindowLongA(IntPtr hWnd, int nIndex, long dwNewLong);

[DllImport("User32.Dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

[DllImport("user32.dll")]
private static extern void RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);


[... Code ...]


Process process = new Process();
process.StartInfo.FileName = pProcess;
process.StartInfo.Arguments = pArgs;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler((s, e) =>
{
    FermerOnglet(tabpage);
});

process.Start();
while (process.MainWindowHandle == (IntPtr)0)
{
}
SetParent(process.MainWindowHandle, metroPanel1.Handle);
SetWindowLongA(process.MainWindowHandle, -16, 0x80000000L);


[... Code ...]


RedrawWindow(CurrentOngletSelect.HandleProcess.MainWindowHandle, (IntPtr)0, (IntPtr)0,  0x0400/*RDW_FRAME*/ | 0x0100/*RDW_UPDATENOW*/ | 0x0001/*RDW_INVALIDATE*/);
MoveWindow(CurrentOngletSelect.HandleProcess.MainWindowHandle, 0, 0, metroPanel1.Width, metroPanel1.Height, true);
SetWindowPos(CurrentOngletSelect.HandleProcess.MainWindowHandle, (IntPtr)(-1), 0, 0, 0, 0, 0);
ShowWindow(CurrentOngletSelect.HandleProcess.MainWindowHandle, 5);
EnableWindow(CurrentOngletSelect.HandleProcess.MainWindowHandle, true);