'DirectX game freeze problem using SetParent

The DirectX Game Freeze issue started after show the 'Run As Administrator' window.

I tried SetWindowPos(), SetWindowLong(), ShowWindow(), SetForegroundWindow(), SetActiveWindow() functions but don't fix the freeze problem.

All functions is working without using SetParent(). How to Active window handle using SetParent like VisualBasic.interaction.AppActivate() function?

NOT: this is not 'not responding'

enter image description here

Native:

using System.Runtime.InteropServices;
 using System.Diagnostics.Process;

  //SetWindowPos Function.
 [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr handle, IntPtr HandleInsertAfter, int Xkoordanit, int Ykoordanit, int cxboyut, int cyboyut, uint uFlags);
 public const int SWP_SHOWWINDOW = 0x0040;
 public const int SWP_NOSIZE = 0x0001;
 public const int SWP_NOMOVE = 0x0002;
 // SetWindowLong Function.
 [DllImport("user32.dll")]
 public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
 public const int GWL_STYLE = (-16);
 public abstract class WindowStyles
 {
     public const uint WS_OVERLAPPED = 0x00000000;
     public const uint WS_CAPTION = 0x00C00000;
     public const uint WS_SYSMENU = 0x00080000;
     public const uint WS_THICKFRAME = 0x00040000;
     public const uint WS_MINIMIZEBOX = 0x00020000;
     public const uint WS_MAXIMIZEBOX = 0x00010000;
 }
 // ShowWindow Function
 [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr handle, int nCmdShow);
 // SetForegroundWindow Function.
 [DllImport("user32.dll")]
 public static extern bool SetForegroundWindow(IntPtr hWnd);
 // SetActiveWindow
 [DllImport("user32.dll", SetLastError = true)]
 public static extern IntPtr SetActiveWindow(IntPtr hWnd);
 // SetParent function.
 [DllImport("user32.dll", SetLastError = true)]
 public static extern IntPtr SetParent(IntPtr MainWindowHandle, IntPtr Panel);

i tried code like focus window:

// form2
Form2 form2 = new Form2(); 
// dock window to form2.panel
IntPtr Handle = gameProcess.MainWindowHandle;
IntPtr Docked = SetParent(Handle, form2.panel1.handle);
// i tried all functions.
SetWindowPos(Handle, IntPtr.Zero, 0, 0, 1024, 768, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(Handle, IntPtr.Zero, 0,0, 1024,768, SWP_SHOWWINDOW);
SetWindowLong(Handle, GWL_STYLE, (WindowStyles.WS_OVERLAPPED | WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU | WindowStyles.WS_THICKFRAME | WindowStyles.WS_MINIMIZEBOX | WindowStyles.WS_MAXIMIZEBOX));
ShowWindow(Handle, 9); // ShowWindow = 9
SetForegroundWindow(handle);
Microsoft.VisualBasic.interaction.AppActivate(gameProcess.Id); // Need Microsoft.VisualBasic library DLL
SetActiveWindow(Handle);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source