获得.net控件的windows句柄的方法

 更新时间:2013年04月15日 10:30:01   作者:  
有一个简单的获得控件的句柄的窍门。你将窗体设置为鼠标可捕获,那么你的窗口句柄可以通过使用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;
  }
}

    

相关文章

最新评论