Skip navigation

.NET Compact Framework中,把原有的Form.Handle属性去掉了。表面上看似乎无法获得一个窗口的句柄了。实际上我们可以变通一下来解决这个问题。首先,通过Form.Capture让窗口获得焦点,接下来使用GetCapture API来取得焦点窗口的句柄。类似的方法还可以获得其他一些有焦点控件的句柄

[DllImport("coredll.dll")]
private static extern IntPtr SetCapture(IntPtr hWnd);

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

public static IntPtr GetHandle()
{
 IntPtr hOldWnd = GetCapture();
 this.Capture = true;
 IntPtr ret = GetCapture();
 this.Capture = false;
 SetCapture(hOldWnd);
 return ret;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>