DEVBUZZ Homepage How to wrap a standard Windows control in .Net Compact Framework (C#)
 
Web www.devbuzz.com
  HOME PAGE
  All Articles
  Advertise
  Consulting

 Development
  Discuss - Forums
  Still in the box?
  .Compact Framework
  Code Snippets
  SQL Server CE
  Database
  MS Resources
 Stores
  Developer Controls
  Pocket PC Hardware
  Pocket PC Software
  Pocket PC Books
  .NET CF Books
  Book Reviews
  SPB SW Discounts
  RESCO SW Discounts
 DEVBUZZ Info
  About Us
  Help
  Join our email list
  Links & Ratings
  Press & Comments
  Pocket PC version
  Software Reviews
  Hardware Reviews
 Authors
  Authors
  Article Guide
  Competitions
 Resources
  Developers
  Register
  Login

  SPB Discounts!
 Columnists
  Rick Winscot
 Past Blast
  Personal Media Ctr
  Gizmobility
  eVB Legacy
  Old news
  Hosted Software
  Wireless
  Newsletters
  Carl Davis
  Upton Au

 Pocket PC Registry
  Join the registry
  View current list
 Current Poll
Are you converting to .NET Compact Framework?
Yes, it has changed my life!
No, I'm sticking with eVB
.NET CF what's that`?

Current results
3431 votes so far
 Recent Forum Threads [goto forums]

Get Computername
read... (67 hits)


Great aid to development productivity
read... (82 hits)


ThreadingTimer sample code
read... (143 hits)


Multithreading with .NET CF
read... (194 hits)


Moving from eMbedded Visual Basic to Visual Basic .NET
read... (166 hits)


.NET Compact Framework 2.0 Service Pack 2
read... (226 hits)


Transfer Data from SQL Server 2000 to SQL Server Compact Edition
read... (298 hits)


This protocol version is not supported
read... (236 hits)


Converting Lowercase to uppercase wont work
read... (203 hits)


Direct access to MS SQL Server 2000
read... (374 hits)


Creating SDF file in Desktop
read... (513 hits)


Winsock in CF.NET
read... (316 hits)


Using Pocket Outlook to submit HTML page form with MAILTO action
read... (420 hits)


Missing file "System.Data.PocketPC.asmmeta.dll"
read... (268 hits)


HP iPAQ hw6915 Serial Port Issue
read... (309 hits)


Info on the recent forum changes
read... (341 hits)


SqlServer tools from Redgate
read... (383 hits)


Arrow keys and Hardware navigation button
read... (393 hits)


O2 XDA lls pin sync cable to comport
read... (322 hits)


Creating dynamic folders on Pocket PC OS
read... (299 hits)

Custom Windows Mobile software development.
LBS Challenge 2007
LBS Challenge Eight previous NAVTEQ Global LBS Challenge® participants have received venture capital funding and nine past LBS Challenge winners have launched commercial applications on major wireless carriers. Register your non-commercial LBS application in the 2007 NAVTEQ Global LBS Challenge in one of three regions: Americas, Europe-Middle East-Africa (EMEA) or Asia-Pacific(APAC). You could win a share of $2 million in prizes. This could be your year.
Dream. Develop. Win.

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

Discuss this article   .NET Compact Framework   

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]

 

Back to the top of the page.
Chris De Herrera's Windows CE Website Windows CE News & Information Source
Copyright ©2000-2007 by DEVBUZZ.COM, Inc., NJ. USA.MSDEVELOP