mobile天地

mobile天地

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

问题:怎样获得.net控件的windows句柄
       我们想调用windows API函数并且需要获得控件的句柄。怎样才能获得.net控件的windows句柄呢?

 

解决办法:

     有一个简单的获得控件的句柄的窍门。你将窗体设置为鼠标可捕获,那么你的窗口句柄可以通过使用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);

 

 

 

posted on 2010-02-08 11:13  鳄鱼  阅读(1697)  评论(6编辑  收藏  举报