您的位置:首页 > 其它

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

2012-10-20 11:38 183 查看
有一个简单的获得控件的句柄的窍门。你将窗体设置为鼠标可捕获,那么你的窗口句柄可以通过使用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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: