[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 阅读(506) 评论(8) 编辑 收藏

评论

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

路过,学习  回复 引用 查看   

#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
兄台何出此言呢
 回复 引用 查看   

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

公告

These postings are provided "AS IS" with no warranties
and confer no rights.


Locations of visitors to this page

Blog Keywords
Embedded System,Visual Studio,.Net Framework,.Net Compact Framework,.Net Micro Framework,Windows Mobile,Windows Embedded CE,Emulator,WCF,CLR,Design & Pattern,C/C++,C#,Matlab,Algorithms
昵称:Freesc Huang
园龄:4年11个月
粉丝:32
关注:4

统计

  • 随笔 - 197
  • 文章 - 0
  • 评论 - 840

搜索

 

随笔分类(227)

随笔档案(197)

Blogs

Link

积分与排名

最新评论

阅读排行榜

评论排行榜

推荐排行榜