如何获得.net控件的windows句柄

来源:互联网 发布:linux查看进程占用端口 编辑:程序博客网 时间:2024/06/17 05:48
有一个简单的获得控件的句柄的窍门。你将窗体设置为鼠标可捕获,那么你的窗口句柄可以通过使用Windows API捕获。 这个想法在以下的GetHWnd函数中实现:

 

class WinAPI
{
  [DllImport("coredll.dll")]
  private static extern IntPtr SetCapture(IntPtr hWnd);

  [DllImport("coredll.dll")]
  private static extern IntPtr GetCapture();

 

  public static IntPtr GetHWnd(Control ctrl)
  {
    IntPtr hOldWnd = GetCapture();

    ctrl.Capture = true;

    IntPtr hWnd = GetCapture();

    ctrl.Capture = false;

    SetCapture(hOldWnd);

    return hWnd;
  }
}


这是GetHWnd函数的简单用法:

  IntPtr hWndButton = WinAPI.GetHWnd(button1);

原创粉丝点击