Sometimes WPF controls want to display the form themselves, but need to set the Owner property when calling WPF controls in C + +. The roundabout workaround is to set the parent form of the WPF window form to the handle of the process.
1. Get the current process ID
int id = process.getcurrentprocess (). Id;
2. Get the process master handle based on the process ID
public static class Processhelper {private static class Win32 {internal con St UINT Gwowner = 4; Internal delegate bool Enumwindowsproc (IntPtr hWnd, IntPtr lParam); [DllImport ("User32.dll", CharSet = CharSet.Auto)] internal static extern bool EnumWindows (Enumwindowsproc l Penumfunc, IntPtr LParam); [DllImport ("User32.dll", CharSet = CharSet.Auto)] internal static extern int GetWindowThreadProcessId (INTPT R HWnd, out IntPtr lpdwprocessid); [DllImport ("User32.dll", CharSet = CharSet.Auto)] internal static extern IntPtr GetWindow (IntPtr hWnd, uint Ucmd); [DllImport ("User32.dll", CharSet = CharSet.Auto)] internal static extern bool IsWindowVisible (IntPtr hWnd); } public static IntPtr getprocesshandle (int processId) {IntPtr PROCESSPT R = IntPtr.Zero; WiN32. EnumWindows (hWnd, LParam) = {IntPtr pid; Win32.getwindowthreadprocessid (hWnd, out PID); if (pid = = LParam && win32.iswindowvisible (hWnd) && win32.ge Twindow (hwnd, win32.gwowner) = = IntPtr.Zero) {processptr = hwnd; return false; } return true; }, New IntPtr (processId)); return processptr; } }
3. Set WPF window's parent form to the current Process main window handle, complete with the following code:
int id = process.getcurrentprocess (). Id; IntPtr mainptr = processhelper.getprocesshandle (ID); var win = new Window (); New Windowinterophelper (Win) {Owner = mainptr}; Win. Show ();
Thanks for reading.
Gets the current process (program) main form handle and sets the parent form of WPF for this handle