Gets the current process (program) main form handle and sets the parent form of WPF for this handle

Source: Internet
Author: User

Source: Gets the current process (program) main form handle and sets the parent form of WPF for this handle

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.