|
Development | .NET Compact Framework
How to wrap a standard Windows control in .Net Compact Framework (C#)
Written by Andrey Lebedev
[author's bio]
[read 33610 times]
Edited by Derek
Page 1
The main reason for wrapping Windows
controls in .Net is the fact that a lot of useful Windows
controls are not available in .Net CF, for example, a DateTimePicker,
a "hybrid" combo box (combo box with string editing), and
owner-draw list box and others. Some useful controls (for
example, owner-draw list boxes) may be easily implemented
in pure .Net, but unfortunately they will be not very much
reliable and too slow for serious applications.
In this article I'd like to explain
how to wrap a standard Windows control. This is somehow
a "one-direction" wrapping. Another article
will be devoted to how to handle messages and callbacks
from the created controls. In most cases you don't need
any callbacks from the control and "one-direction" wrapping
will be enough for you.
For example, it will be shown how to
wrap the DateTimePicker control which doesn't exist in .Net
Compact Framework. All other controls may be implemented
similarly. The language that we use is C#. The main idea
for the wrapping of a standard Windows control is to create
an instance of the control window via CreateWindowEx API
function (this will give us the handle of the created window)
and then to use the messages or other API functions to assign
the created object all the required properties.
A.
The first thing we should learn to do is wrap Windows API
function. The syntax is very easy:
namespace WinAPI
{
public class WinAPI {
[DllImport ("coredll.dll")]
public static extern IntPtr CreateWindowEx(
uint dwExStyle, string lpClassName, string lpWindowName,
uint dwStyle, int x, int y, int width, int height, IntPtr
hWndParent,
int hMenu, IntPtr hInstance, string lpParam);
[DllImport("coredll.dll",EntryPoint="SendMessage")]
public static extern int SendMessageStr(
IntPtr hWnd,
int message,
int data,
string s);
[DllImport("coredll.dll",EntryPoint="SendMessage")]
public static extern int SendMessageSystemTime(
IntPtr hWnd,
int message,
int data,
SystemTime d);
}
}
where SystemTime class is declared in
the same WinAPI namespace as follows:
public class SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort MilliSecond;
}
Of course, all standard Windows constants
(WS_XXXX, WM_XXXX and others) is useful to declare as C#
constants inside WinAPI class:
public class WinAPI
{
...
public const int DTM_SETFORMAT = 0x1032;
public const int DTM_GETSYSTEMTIME = 0x1001;
public const int DTM_SETSYSTEMTIME = 0x1002;
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
public const int WS_BORDER = 0x800000;
public const int DTS_TIMEFORMAT = 0x0009;
public const int GWL_STYLE = -16;
public const int WM_USER = 0x0400;
...
}
Later we will add to WinAPI class all
necessary Windows API functions: InitCommonControlsEx and
others. The rule for parameter types conversion is intuitive
and is shown in the following table:
| Native type
|
.Net CF type
|
| int |
int |
| DWORD |
uint |
| LPCTSTR |
string |
| LPTSTR |
char[] |
| HANDLE, HWND, HMENU, HICON, etc...
|
IntPtr |
Here is a useful function for handling
the parameters of type LPTSTR:
public static
string CharsToString(char[] c)
{
int nLength = 0;
while (nLength < c.Length &&
c[nLength] != '\0')
nLength++;
return new string(c, 0, nLength);
}
You create a large enough char[] buffer,
pass it to the imported function and then translate the
result into a normal .Net string.
B.
The hWndParent parameter to be passed to CreateWindowEx
function is a HWND window handle of a .Net window that will
become a parent for our new created DateTimePicker control.
C. We declare a .Net C#
class for our DateTimePicker control:
public class DateTimePicker {}
We will not wrap DateTimePicker into
a normal .Net control (a descendant of System.Windows.Forms.Control
class) but instead we declare a set of static functions
that provide all required operations on the control i.e.
the creation, the time format specification, the get/set
time/date operations. Therefore the DateTimePicker class
declaration stands for a namespace. If you want you can
easily create a normal .Net control over ours but I don't
see any reasons for doing this.
The creation function is like
this:
public class DateTimePicker
{
...
public const int DTP_HEIGHT = 22;
public static IntPtr DTP_Create(Formats.DateTimeSpec
nDTSpec, IntPtr hWndParent, int nX, int nY)
{
string strFormat = "";
int nWidth = 0;
switch (nDTSpec)
{
case Formats.DateTimeSpec.DTS_DATE:
nWidth = 90;
strFormat = "MM'/'dd'/'yyyy";
break;
case Formats.DateTimeSpec.DTS_TIME:
nWidth = 90;
strFormat = "hh':'mm':'ss";
break;
case Formats.DateTimeSpec.DTS_DATETIME:
nWidth = 150;
strFormat = "hh':'mm':'ss MM'/'dd'/'yyyy";
break;
}
uint nStyle = 0;
if (nDTSpec == Formats.DateTimeSpec.DTS_TIME)
nStyle |= WinAPI.WinAPI.DTS_TIMEFORMAT;
byte[] ics = {8, 0, 0, 0, 0, 1, 0, 0};
WinAPI.WinAPI.InitCommonControlsEx(ics);
IntPtr hWnd = WinAPI.WinAPI.CreateWindowEx(0,
"SysDateTimePick32", "",
WinAPI.WinAPI.WS_VISIBLE | WinAPI.WinAPI.WS_BORDER | WinAPI.WinAPI.WS_CHILD
| nStyle,
nX, nY,
nWidth, DTP_HEIGHT, hWndParent,
0, 0, null);
if (hWnd != IntPtr.Zero)
WinAPI.WinAPI.SendMessageStr(hWnd, WinAPI.WinAPI.DTM_SETFORMAT,
0, strFormat);
return hWnd;
}
...
}
D.
The C# code for creating the DateTimePicker control as a
child of the form form1 is as follows:
IntPtr hWndForm = WinAPI.WinAPI.GetHWnd(form1);
IntPtr hDTPicker = DateTimePicker.DTP_Create(Formats.DateTimeSpec.DTS_DATETIME,
hWndForm, 20, 10);
This will create the control and
insert it into given form and gives us its window handle
which can be used for getting and setting the control's
properties. For example, this function gets current date/time
value in the DateTimePicker editor:
public class DateTimePicker
{
...
public static WinAPI.SystemTime DTP_GetSystemTime(IntPtr
hWnd)
{
WinAPI.SystemTime st = new WinAPI.SystemTime();
WinAPI.WinAPI.SendMessageSystemTime(hWnd,
WinAPI.WinAPI.DTM_GETSYSTEMTIME, 0, st);
return st;
}
...
}
The usage of this method is as follows:
WinAPI.SystemTime st = DateTimePicker.DTP_GetSystemTime(hDTPicker);
Done!
Back to .NET Compact Framework | [Article Index]
|