黄季冬=>fox23

Freesc Huang & Smart Devices
数据加载中……
[CF.Skills]WinCE互操作时传递托管控件句柄的小技巧
在和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了。
简单吧!

Enjoy it !

黄季冬

posted on 2008-05-03 10:29 Freesc Huang 阅读(412) 评论(8)  编辑 收藏 网摘 所属分类: Compact Framework

评论

#1楼 2008-05-03 13:04 李战      

http://www.cnblogs.com/Emoticons/yoyocici/223852199.gif" alt="" />路过,学习
    回复  引用  查看    

#2楼 2008-05-04 01:55 winifred[未注册用户]

Nothing replaces hard work--good.
    回复  引用    

#3楼 2008-08-22 09:50 Wuya      

.net cf 2.0好象直接有个Control.Handle属性获取HWND句柄呢。
    回复  引用  查看    

#4楼 2009-03-26 23:01 Special_Lynn[未注册用户]

IntPtr hWndButton = WinAPI.GetHWnd(button1);
IntPtr hWndButton2 = button.Handle;
if(hWndButton == hWndButton2 )
{
MessageBox.Show("博主代码纯属蒙人!");
}
    回复  引用    

#5楼 2009-03-26 23:02 Special_Lynn[未注册用户]

IntPtr hWndButton = WinAPI.GetHWnd(button1);
IntPtr hWndButton2 = button1.Handle;
if(hWndButton == hWndButton2 )
{
MessageBox.Show("博主代码纯属蒙人!");
}
    回复  引用    

#6楼[楼主] 2009-03-27 10:06 fox23      

@Wuya
yep~, 而且control.handle是一个线程安全的访问方法
    回复  引用  查看    

#7楼[楼主] 2009-03-27 10:14 fox23      

@Special_Lynn
兄台何出此言呢
    回复  引用  查看    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1179608




相关文章:

相关链接: