技术开发 频道

WinCE互操作时传递托管控件句柄的小技巧


【IT168技术文档】

  在和Native Code打交道的时候,我们经常要传递某个控件(如form)的句柄(handle)给本地代码。下面的代码演示了如何使用Control的Copture属性和SetCapture,GetCapture方法来实现这个过程:
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;//设置ctrl为窗体焦点 IntPtr hWnd = GetCapture();获取当前焦点句柄(即ctrl) ctrl.Capture = false; SetCapture(hOldWnd);//还原Capture状态 return hWnd; } }
  调用的时候:
  IntPtr hWndButton = WinAPI.GetHWnd(button1);
  再把这个intPtr传给本地需要的函数中就Ok了。
0
相关文章