Wednesday, February 09, 2005

There is a common technique of obtaining the hWnd of various controls in Compact Framework applications to use with native Win32 APIs. It involves setting a Capture property to true and then using GetCapture() to get the handle:

txtUser.Capture = true;
IntPtr hWndUser = GetCapture();
txtUser.Capture = false;

Suprisingly this does not work on Smartphone. It gets you a handle alright, but the handle seems to be wrong. It is wrong indeed. The reason is that on the Smartphone the native Edit control (wrapped by the TextBox) is hosted inside another child window. This has to do with the Smartphone navigation. When you set TextBox.Capture to true, the outer control gets Captrure and as a result, it's the outer control, of which you get the handle. Since we know that the outer control has just one child, we can see our way from this quandary.

txtUser.Capture = true;
IntPtr hWndUser = GetCapture();
hWndUser = GetWindow(hWndUser, GW_CHILD);
txtUser.Capture = false;

//GW_CHILD = 5;

The required PInvoke definitions are parts of Win32Window class in OpenNETCF SDF

Note: This is applicable to CF v1. I have a reason to believe that in v2 things are done differently

2/9/2005 12:24:05 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  |