none
请教一下,如何正确的使用HandleRef? RRS feed

答案

全部回复

  • 你好,

    你可以看一下链接里的例子。

    https://csharp.hotexamples.com/examples/-/HandleRef/-/php-handleref-class-examples.html

    https://msdn.microsoft.com/en-us/ie/hc662t8k(v=vs.94)

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年3月6日 7:48
    版主
  • 还是不太怎么理解,能简单的举下生活中的小例子吗?XD
    • 已标记为答案 Troy_Zhang 2019年3月26日 3:40
    2019年3月6日 8:38
  • 你好,

    这有个简单的例子用来获取应用程序的句柄。

     /// <summary>
            /// 获取应用程序窗口句柄
            /// </summary>
            /// <param name="processId"></param>
            /// <returns></returns>
            private IntPtr GetWindowHandle(int processId)
            {
                var windowHandle = IntPtr.Zero;
                EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
                EnumWindows(windowsCallback, IntPtr.Zero);
                //保持循环
                GC.KeepAlive(windowsCallback);
    
                bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
                {
                    int num;
                    GetWindowThreadProcessId(new HandleRef(this, handle), out num);
                    if (num == processId && IsWindowExist(handle))
                    {
                        windowHandle = handle;
                        return true;
                    }
                    return false;
                }
    
                return windowHandle;
            }
            public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年3月11日 7:12
    版主