Windows CE程序设计read.pudn.com/downloads150/ebook/647850/Windows+CE 程序... · Web...

Click here to load reader

Transcript of Windows CE程序设计read.pudn.com/downloads150/ebook/647850/Windows+CE 程序... · Web...

Windows CE

Windows CE

WindowsWindows CE.NETWindowsPocket PCSmartphone

- Windows

1 - "Hello Windows CE"

2 -

3 -

4 -

5 - Windows CE

6 -

- Windows CE

7 -

8 -

9 - Windows CE

10 -

11 -

-

12 -

13 - Windows CE

14 -

15 -

16 - Shell

17 - Pocket PC

18 - Pocket PC

19 - Smartphone

20 - API-GAPI

- Windows CE

21 -

22 -

23 - .Net CF

Windows CE.NET3Windows CE.NET 4.2.NET CFDouglas BolingWindowsWindows CE.NETWindowsPocket PCSmartphoneDouglas Boling

PC

--Smartphone

--Pocket PC PhoneSmartphoneSMS

--

API

Douglas BolingWindows CETechEdBoling ConsultingDougWindows CE

TechEdBoling ConsultingDougWindows CE

Windows CEWindowsWindows

Windows CEPC

Windows CEWin32 APIWindows

Windows CEWindows

Windows CE

Windows CEWindows Windows CEROMWin32 API

Windows CEWindows APIXP

Windows 95/98/Me MS-DOSWindows 2.x3.x

Windows MeWindows NT/2000/XP

Windows XP Windows XP --

2

Windows CEMS-DOSWindows.Windows CE

Win32 API

Windows CE .NET CF.NET CF.NET

.NET

Win CE

Windows CEPocket PCWindows CE--

Windows CE480*240640*240ChicletPC1996Fall Comdex 97Windows CE 2.0640*240

19981Palm-size PCPocket PCWindows CE 2.xPalm-size PC,Windows CE 3.xPocket PCPCAuto PCWindows CEPalm 0s240*320Windows CE

Windows CEVGA/Super VGAH/PC10Pocket OfficeModemthenstandard IBM

20004Pocket PCPalm-size PCPocket PCWindows CE 3.0Pocket PC2-TodayPocket PCWindows CEWindows CECPUPocket Pocket PCWindows CEWindows CE

PC2000Windows CE 3.0PC2000

2001Pocket PCPocket PC 2002Windows CE3.0Pocket PC PhonePocket PCPocket PC

Smart Display(,802.11bWindows XP ProfessionalRemote Desktop)Windows CE.NET 4.1PC2PC

2003Pocket PCPocket PCPocket PC 2003Windows CE.NET 4.2Pocket PC 2003OEM

OEMWindows CESmartphone2002Windows CE 3.02003Windows CE4.2 .NET runtime

Media to GoWindows CE

Windows CE1.0Windows CE 2.0Windows CEPocket PCPocket PCSmartphone

Fall Comdex 1997Windows CE 2.0PC 2.0Windows CE 2.0Windows NDIS miniportNE2000COMWindows CE 1.02Windows CE 2.0Windows CEH/PCWindows CE (ETK),Windows CEETK

Palm-size PC1998Windows CEWindows CE 2.01ETKWindows CE 2.01CEXEDLL

19988H/PC-2.11Windows CE 2.11Windows CE 2.1Windows CE 2.11Windows CE2.114MMS-DOSCMD.exeWindows CEWindows CE2.11Fast IRIrDA4MIP

Windows CE 2.122.1-Windows CE 2.1BuilderBuilderPCInternet IE 4.0Windows CEGenie-IEHTMLPocket InternetMMQWindows CE 2.11/go/no gogo,but don't trust

Windows CE 3.020004Pocket PCWindows CE 3.0Windows CE 3.02568

Windows CE 3.0COM2.0COMCOMDCOM256M RAM32M/Platform Builder 3.0media playerPPTPICSDirectX API

Windows CE2001Windows CE.NET 4.01Windows CE.NET 4.0(Services)802.111394Windows CE.NET 4.0.NET.NET

2001Windows CE 4.1Windows CE 4.0IP v6Winsock2appletsPower ManagerWindows CE 4.1.NET .NET (QFE)

20032Windows CE.NET 4.2OEM-Pocket PCPocket PCAPIShellCPU

Windows CEWindows CE www.bolingconsulting.com

Windows CEWindows CE.NETWindows CEWindows Windows

Windows Win32 API1 Windows Windows(Charles Petzold)Windows CE

Windows Windows CE Windows XPWin32 APIWindows CEWin32APIWin32 APIWindows CEWindows CEWin32 API

.NET CFCFWindows CE

Windows CEWindows CE

APIPetzoldWindows CEMFCWindows CEWindows CEMFCWindows CE APIWindows CE APIMFC

33SmartphoneWindows CE.NET 4.2Smartphone.NET CFOBEX

SmartphonePocket PC PhoneSmartphone 2003SmartphonePocket PC PhoneSMS

.NET CFWindows CE.NET CFWindows Form

OBEXOBEXOBEXOBEX

Pocket PCPocket PC 20032Pocket PCWindows CEPocket PC API

Windows CEWindows CEWindows CE .NET 4.0Windows CEIPWindows CE

12Pocket PCWindows CE1

.NET

.NET.NETWindows CEPocket PCWindows CE.NET 4.1

/

MFC

MFCWindows CEwindows CECPUMFCWindows CEMFCMFCROMMFC

MFCMFC.NETMFCMFCMFC

Windows CE

CWindowsXPVisual C++Visual C++ 4.0Windows CESDK

WCEPocket PCPocket PC

Pocket PC 20002002Visual C++ 3.0eVC3eVC4eVC3Pocket PCSDKOBEX

Visual Studio.NET 2003.NETVisual Studio.NET 2003Pocket PCWindows CE 4.1

Windows CESDKWindows CEWindows XPWindows CEPCWindows CEPCPCWindows CE

Windows CECPUCPUCPU

CD

MS eVCWindows CE 4.2Pocket PC 2003Smartphone 2003

eVCPocket PC 2003SDK

StartCDCDWindowsCDStartCD.exeReadme.txtCDSDK

MS eVCeVCWindows 2000, Windows XP, Windows Server 2003

*Pentium PCPentium 150 MHz*Microsoft Windows XP, Windows 2000 Service Pack 2 (or later) or Windows Server 2003*32 MB48MB*360MB720MB**VGAS VGA*

Windows CEWindows--Charles PetzoldWindows1998WindowsCharlesWindowsWin32 APIJeff RichterWindows Programming Applications for Microsoft Windows (Microsoft Press, 1999)JeffMFCJeff ProsiseMFC Programming Windows with MFC (Microsoft Press, 1999). MFCPetzoldMFC

.NETCharles PetzoldC# Windows Programming Windows with C# (Microsoft Press, 2002)Charles .NETWindows Form.NET

1 Hello Windows CE

The C Programming Language"Hello,world"

"Hello,world"Windows CEWindows Windows CEXPPCWindows CE

WindowsWindows Windows CEWindows CE

1.1 Windows CE

Windows CEWindows Windows CEIntel x86Windows CE4CPUSHx, MIPS, ARM, x86CPU

Windows CEPocket PC240*320480*240,640*240,640*480Windows CEWindows AltWindows CE

Windows CE

Windows CEWindows IBM256800*600

Windows CEWindows CEWindows CERAMWindows CEWindowsWindows CE

Unicode

Windows CEUnicode. Unicode16ASCII8Unicode 65,536Unicode UnicodeASCII

Unicode22602590charTCHARTCHARMS Windows 9598charWindows 2000,XP,Windows CEUnicodeTCHARunsigned shortASCIIUnicode

Windows CEWindows CEWindows CEWindows CEedit controlPDAWindows Windows CEH/PCPocket PCWindowsWindows CE

Windows CEWindows CEOEMWindowsWindows Windows 95/98Me2000XPAPIWindows CE

Windows CEPocket PCWindows CEPocket PCWindows CEOEM--PDAPocket PCPocket PCAPIOEMSDKOEMSDK

Win32

Windows CEXPXPWindows CEWindows CEXPWindows CEWindows CEWindows CEAPI

1.2 Windows

Windows CEWindowsWindows CEWindows

Charles PetzoldWindows WindowsCharles Simonyi12NameszNamesz01-1

1-1

Integeri or n

Word (16-bit) w or s

Double word (32-bit unsigned) Dw

Long (32-bit signed) L

Char C

String Sz

Pointer P

Long pointer lp

Handle h

Window handle hwnd

Struct sizecb

WindowslpIntel 1616hbrlpsz0Windows API

1.3 Windows CE

Windows CE1-1Hello1--Windows CEHello World

Listing 1-1: Hello1, Windows

Hello1.cpp

//======================================================================// Hello1 - A simple application for Windows CE// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include "windows.h"// Program entry pointint WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPWSTR lpCmdLine, int nCmdShow) {

printf ("Hello World\n"); return 0;

}

KernighanRitchie

#include "windows.h"

Windows API

CCint main (char **argv, int argc)Windows CEWindows[1]int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow);

Windows CEWindows

1hInstanceEXEWindows API

hPrevInstanceWin16 APIWindows 3.1Windows CEWin32 hPrevInstance 0

lpCmdLineUnicodeWindows CElpCmdLine Windows CEXPWindows CEUnicodeWindows ASCII

nCmdShowSW_SHOWMINIMIZESW_SHOWMAXIMIZEDSW_RESTOREWindows CE3SW_SHOWSW_HIDESW_SHOWNOACTIVATEShowWindow

printf ("Hello World\n");

Windows CECprintf,getcharWindows CEUnicodeANSIANSICprintfstrcpyANSIWindows CEUnicode wprintf, getwchar, wcscpy

return 0;

Win32 APIGetExitCodeProcess

Hello1MS eVC++[File][]WinCEWin321-1MFCATLWCE Pocket PCPocket PCWCEWindows CECPUWin32(WCE MIPI)MIPS CPUWindows CEWCEXP

1-1 eVC++Windows CE

eVC++Hello World [File][New]Hello1.cpp

Hello 1CDWin32(WCE x86em) DebugbuildEXEHello1Windows 98MeWin2000XP

Windows CEPocket PC(PPC)PPCPCPCPPCActiveSyncPPCPCWindows CEActiveSyncWindows CE

PCWindows CEeVC++iPaq Pocket PCWin32 [WCE ARM] DebugrebuildeVC++\windows\start

Winodows CEH/PCHello1PCMy Computer

Pocket PCStarteVC++\windows\startStart

Pocket PCHello1PCWindows CEHello1printfHello1

Pocket PCPocket PCprintfconsole.dllPocket PCWindows Pocket PCPCWindows CE

1C

1.4 Hello2

Hello1Windows CEHello2printfHello CEHello21-2

1-2: Hello2,MessageBox

Hello2.cpp

//======================================================================// Hello2 - A simple application for Windows CE// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include "windows.h"int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPWSTR lpCmdLine, int nCmdShow) {

MessageBox (NULL, TEXT ("Hello World"), TEXT ("Hello2"), MB_OK);

return 0;

}

Hello21-2

1-2

Windows CEHello2

printfMessageBoxHello22"Hello World"MessageBoxHello2

MessageBox

int MessageBox (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

1NULLHello22TEXTUnicode3lpCaptionuType1-2Windows CE

1-2:

MB_OK

OK

MB_OKCANCEL

OK and Cancel

MB_RETRYCANCEL

Retry and Cancel

MB_YESNO

Yes and No

MB_YESNOCANCEL

Yes, No, and Cancel

MB_ABORTRETRYIGNORE

Abort, Retry, and Ignore

MB_ICONEXCLAMATION, MB_ICONWARNING

Exclamation point

MB_ICONINFORMATION, MB_ICONASTERISK

Lower case i within a circle

MB_ICONQUESTION

Question mark

MB_YESNO

Yes and No

MB_ICONSTOP, MB_ICONERROR, MB_ICONHAND

Stop sign

MB_DEFBUTTON1

First button

MB_DEFBUTTON2

Second button

MB_DEFBUTTON3

Third button

For Window Styles:

MB_SETFOREGROUND

Bring the message box to the foreground.

MB_TOPMOST

Make the message box the topmost window.

MessageBox

IDOK

OK button pressed

IDYES

Yes button pressed

IDNO

No button pressed

IDCANCEL

Cancel button pressed or Esc key pressed

IDABORT

Abort button pressed

IDRETRY

Retry button pressed

IDIGNORE

Ignore button pressed

eVC++buildHello2eVC++[Update Remote File]

Hello2MessageBoxWindows

1.5 Windows

WindowsMS-DOSUnixMS-DOSUnixgetc-putcMS-DOSUnix"Pull"Windows"Push"

Windowsmessages

--x,yz----

WindowsWM_CREATE WM_MOVEWindows

WindowsWindowsWindowsWindows

1WindowsXPWindows CE[1] Windows3.1WindowsWindows

2WindowsWindowsWindowsWindows

3WindowsWindowsWindowsWindowsWindows

4WM_MOVE

Windows CE

1.6 Hello3

Windows --Hello3Hello3CDWin32Windows CE1-3Hello3

1-3Hello3

Hello3.cpp

//======================================================================// Hello3 - A simple application for Windows CE// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include // For all that Windows stuffLRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; HWND hWnd; MSG msg; // Register application main window class. wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = TEXT("MyClass"); // Window class name if (RegisterClass (&wc) == 0) return -1; // Create main window. hWnd = CreateWindowEx(WS_EX_NODRAG, // Ex style flags TEXT("MyClass"), // Window class TEXT("Hello"), // Window title // Style flags WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, // x position CW_USEDEFAULT, // y position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height NULL, // Parent NULL, // Menu, must be null hInstance, // Application instance NULL); // Pointer to create // parameters if (!IsWindow (hWnd)) return -2; // Fail code if not created. // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return msg.wParam;}

// MainWndProc - Callback function for application windowLRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect; HDC hdc; switch (wMsg) { case WM_PAINT: // Get the size of the client rectangle GetClientRect (hWnd, &rect);

hdc = BeginPaint (hWnd, &ps); DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

EndPaint (hWnd, &ps); return 0;

case WM_DESTROY: PostQuitMessage (0); break; }

return DefWindowProc (hWnd, wMsg, wParam, lParam);}

Hello3WindowsHello3--WinMainHello3

WinMainHello3RegisterClassRegisterClassWNDCLASS

ATOM RegisterClass (const WNDCLASS *lpWndClass);

typedef struct _WNDCLASS {

UINT style;

//

WNDPROC lpfnWndProc;//

int cbClsExtra;

//

int cbWndExtra;

//Windows

HANDLE hInstance;

//

HICON hIcon;

//Windows CENULL

HCURSOR hCursor;

//NULL

HBRUSH hbrBackground;//

LPCTSTR lpszMenuName;//NULLWindows CE

LPCTSTR lpszClassName;//

} WNDCLASS;

WNDCLASSHello3styleWindows CECS_GLOBALCLASS Windows CE

CS_HREDRAW

CS_VREDRAW

CS_NOCLOSE []

CS_PARENTDC

CS_DBLCLKS [](Windows CE)

lpfnWndProc

cbClsExracbWndExtraWindowsWindows CEcbClsExtra cbWndExtra4

hInstance

hIconWindows CENULLWindows CEHello3WindowsWindows CE

Windows CEhCursorNULLLoadCursor (IDC_ARROW) NULL

hbrBackgroundWindows CEWindowsbrushWindows CEGetStockObjecthbrBackgroundNULLWM_ERASEBKGND

lpszMenuNameNULLWindows CEWindows CE

lpszClassNameWindowsHello3MyClass

WNDCLASSRegisterClassWNDCLASS0

WindowsWindowsCreateWindowCreateWindowExCreateWindowEx

HWND CreateWindow(

LPCTSTR lpClassName,

LPCTSTR lpWindowName,

DWORD dwStyle,

int x, int y, int nWidth, int nHeight,

HWND hWndParent,

HMENU hMenu,

HANDLE hInstance,

PVOID lpParam

);

HWND CreateWindowEx (DWORD dwExStyle,//, CreateWindow

LPCTSTR lpClassName,//

LPCTSTR lpWindowName,////

DWORD dwStyle,

//

int x, int y, int nWidth, int nHeight,//

HWND hWndParent

//

HMENU hMenu,

//Windows CE

HINSTANCE hInstance,

LPVOID lpParam

);

1Windows CE

WS_EX_TOPMOST

WS_EX_WINDOWEDGE

WS_EX_CLIENTEDGE

WS_EX_STATICEDGE 3D

WS_EX_OVERLAPPEDWINDOW WS_EX_WINDOWEDGE WS_EX_CLIENTEDGE

WS_EX_CAPTIONOKBUTTON OK

WS_EX_CONTEXTHELP

WS_EX_NOACTIVATE

WS_EX_NOANIMATION

WS_EX_NODRAG

dwExStyleCreateWindowExCreateWindowWindows CECreateWindowCreateWindowdwExStyle0CreateWindowEx

2Hello3MyClassRegisterClass

3WindowsH/PCPocket PCTEXTWindows CEUnicode

WS_VISIBLEWin32CreateWindowWindows CE

Wimdows CECW_USEDEFAULTWindows CEWindows CE

NULLNULLWindows CE

hInstanceWM_CREATECreateWindowNULL

CreateWindow0--ifShowWindow UpdateWindowShowWindowWinMainnCmdShowUpdateWindowWindowsWM_PAINT

WinMainWindows Hello3

while (GetMessage (&msg, NULL, 0, 0)) {

TranslateMessage (&msg);

DispatchMessage (&msg);

}

GetMessageMSGMSGWin32XPtappedWindows CE

TranslateMessage IsDialogMsgDispatchMessage Windows

GetMessage WM_QUITGetMessage0whileGetMessage0

Hello3WinMainWinMainWM_QUITwParamPostQuitMessageWM_QUITWM_QUITwParam

send postHello3MainWndProcMainWndProc

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);

LRESULT longWindowslong32Windowswindows.h

CALLBACK EXEWindowsCALLBACK PascalCPascalWindowsPascalWindowsWindows CEC

wMsgWinMainMSGunsignedwParam lParam, Win16wParam16lParam32Win32Windows CE32

Hello3switchwMsgIDswitch2caseWM_PAINTWM_DESTROY

WM_PAINT

WM_PAINTWindows WM_PAINTWindowsHello3WM_PAINT

PAINTSTRUCT ps;

HDC hdc;

case WM_PAINT:

GetClientRect (hWnd, &rect);

hdc = BeginPaint (hWnd, &ps);

DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect,

DT_CENTER | DT_VCENTER | DT_SINGLELINE);

EndPaint (hWnd, &ps);

return 0;

Windows--WindowsGetClientRect RECT

WindowsWM_NCxxxWindows CEWindows CE

WM_PAINTBeginPaint EndPaintBeginPaint HDCWindowsWindowsBeginPaint PAINTSTRUCT

typedef struct tagPAINTSTRUCT {

HDC hdc;

//

BOOL fErase;

//

RECT rcPaint;

//

BOOL fRestore;

//Windows

BOOL fIncUpdate;//Windows

BYTE rgbReserved[32]; //Windows

} PAINTSTRUCT;

hdcBeginPaintfErasercPaintRECTHello3WM_PAINTrcPaintWindowsfRestore, fIncUpdate, rgbReservedWindows

Hello3Hello3DrawText2DrawTextHello Windows CEDrawTextEndPaintWindows

EndPaintWindowsBeginPaintEndPaintWindowsBeginPaintEndPaintWindowsWM_PAINT

WM_DESTROY

Hello3WM_DESTROYWM_DESTROYPostQuitMessagePostQuitMessageWM_QUITWM_QUITwParam

WM_QUITWinMain TermInstanceHello3WinMain

Hello3WindowsPetzoldWindowsCharles PetzoldWindowsCharlesWindows Programming Microsoft Windows 5Windows

WindowsWindowsWindows CE

1.7 HelloCE

Windows SDKswitchswitchWindows

WindowsWinMainWinProcWindowsWinMainWindowsWinMain

Ray DuncanPCMS-DOSOS/2RayRay

HelloCE1-4

1-4 HelloCEHelloCE.h

// Returns number of elements

#define dim(x) (sizeof(x) / sizeof(x[0]))

// Generic defines and data types

struct decodeUINT { // Structure associates

UINT Code; // messages

LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM); // with a function.

};

struct decodeCMD { // Structure associates

UINT Code; // menu IDs with a function

LRESULT (*Fxn)(HWND, WORD, HWND, WORD);

};

// Function prototypes

HWND InitInstance (HINSTANCE, LPWSTR, int);

int TermInstance (HINSTANCE, int);

// Window procedures

LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

// Message handlers

LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);

LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

#include

#include "helloce.h"

const TCHAR szAppName[] = TEXT("HelloCE");

HINSTANCE hInst; // Program instance handle

// Message dispatch table for MainWindowProc

const struct decodeUINT MainMessages[] = {

WM_PAINT, DoPaintMain,

WM_DESTROY, DoDestroyMain,

};

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; int rc = 0; HWND hwndMain; // Initialize this instance. hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam);}

HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {

WNDCLASS wc;

HWND hWnd;

// Save program instance handle in global variable.

hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, only allow one instance of the application hWnd = FindWindow (szAppName, NULL);

if (hWnd) {

SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));

return 0;

}

#endif

// Register application main window class.

wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass (&wc) == 0) return 0; // Create main window. hWnd = CreateWindow (szAppName, // Window class TEXT("HelloCE"), // Window title // Style flags WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, // x position CW_USEDEFAULT, // y position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height NULL, // Parent NULL, // Menu, must be null hInstance, // Application instance NULL); // Pointer to create // parameters if (!IsWindow (hWnd)) return 0; // Fail code if not created. // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd;}

int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC;}

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i;

// Search message list to see if we need to handle this // message. If in list, call procedure.

for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam);}

LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect; HDC hdc; // Get the size of the client rectangle GetClientRect (hWnd, &rect); hdc = BeginPaint (hWnd, &ps); DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint (hWnd, &ps); return 0;}

LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}

HelloCEUnicodeszAppNamehInst InitInstance

HelloCEWinMainInitInstanceTerminateInstanceWinMainWinMain

InitInstance WinMainnCmdShow Pocket PC

InitInstancehInstance hInstWindows

Pocket PCHelloCEFindWindowSetFroegroundWindow0WinMain17Pocket PC

Pocket PC#if and #endif#ifWIN32_PLATFORM_PSPC[][] C/C++ $CePlatform-Ce$Key [HKEY_LOCAL_MACHINE]\Software\Microsoft\Windows CE Tools\Platform Manager keyeVC++CePlatformCePocket PC Palm-size PC CePlatformWIN32_PLATFORM_PSPC

Hello3szAppNameszAppName FindWindow

InitInstance

Windows

MFCWindowsHelloCEWindows CEWindowsWindows 3.1MeXPCE

CMainMessagesDefWindowProcDefWindowProcWindowsWindows

// Message dispatch table for MainWindowProc

const struct decodeUINT MainMessages[] = {

WM_PAINT, DoPaintMain,

WM_DESTROY, DoDestroyMain,

};

Windows CEROMWindows CEROMRAMRAM

2DoDoPaintMainWm_PAINT

DoPaintMain DoDestroyMain

HelloCEPaintMain DoDestroyMainHello3caseHello3

HelloCE

eVC++VC++ [Build]->[Execute] HelloCE.exeCtrl+F5"Hello Windows CE"1-31-4HelloCEPocketPC[Close]Windwos CEWM_CLOSEHelloCEWM_CLOSEDefWindowProcWM_DESTORYPostQuitMessage

1-3

Windows CEHelloCE

HelloCEWindows CEHelloCE.exeHelloCEHelloCEDrawText

1-4

Pocket PCHelloCE

1-4HelloCEPocket PCHelloCESIPHelloCEPocket PC

Pocket PCWindows CEPocket PCWindows CE

2

1HelloCEDrawTextWindows CE

WindowsWindowsWindowsWindows CEWindowsCEWin32 APIWindwos CEAPI

Win32 APIWin32 APIWindows CEAPIWindows CEWindows CEWindowsWindows CEMM_TEXTWindows CEWindows CEGetDeviceCapsGetDeviceCapsGetDeviceCaps

Windows CEWindows CEWin32 APIWin32--Windows CE

Windows

Windows

1Windows

Windows123

2

MM_TEXT,XY

3

MM_LOMETRICMM_HIMETRICMM_LOENGLISHMM_HIENGLISHMM_TWIPS0.10.010.010.0011/14400.0007

4

MM_ISOTROPICMM_ANISOTROPICXY

Windows

1

XY

2

XY

3

MM_TEXT

void CMapModeView::OnPaint() {CPaintDC dc(this);

//CPoint ptOrgView,ptOrgWindow;CSize sizeView,sizeWindow;CString strMsg;

ptOrgView=dc.GetViewportOrg();//ptOrgWindow=dc.GetWindowOrg();//sizeView=dc.GetViewportExt();//sizeWindow=dc.GetWindowExt();//

strMsg.Format(_T("Viewport Extent:(%d,%d),\tViewport Org:(%d,%d)\tWindow Extent:(%d,%d)\tWindow Org(%d,%d)"),sizeView.cx,sizeView.cy,ptOrgView.x,ptOrgView.y,sizeWindow.cx,sizeWindow.cy,ptOrgWindow.x,ptOrgWindow.y);TRACE("%s\n",strMsg);

//dc.SetMapMode(MM_TEXT);//dc.SetWindowOrg(100,100);//dc.SetViewportOrg(200,200);//

dc.SetWindowExt(5,10);//dc.SetViewportExt(1,1);//

ptOrgView=dc.GetViewportOrg();ptOrgWindow=dc.GetWindowOrg();sizeView=dc.GetViewportExt();sizeWindow=dc.GetWindowExt();strMsg.Format(_T("Viewport Extent:(%d,%d),\tViewport Org:(%d,%d)\tWindow Extent:(%d,%d)\tWindow Org(%d,%d)"),sizeView.cx,sizeView.cy,ptOrgView.x,ptOrgView.y,sizeWindow.cx,sizeWindow.cy,ptOrgWindow.x,ptOrgWindow.y);TRACE("%s\n",strMsg);

//(300,400)CPoint ptMap;ptMap=CPoint(300,400);dc.LPtoDP(&ptMap);strMsg.Format(_T("The Orginal Point(In LP):CPoint(300,400),Convert to DP is:CPoint(%d,%d)"),ptMap.x,ptMap.y);TRACE("%s\n",strMsg);

//(300,400)ptMap=CPoint(300,400);dc.DPtoLP(&ptMap); strMsg.Format(_T("The Orginal Point(In DP):CPoint(300,400),Convert to LP is:CPoint(%d,%d)"),ptMap.x,ptMap.y);TRACE("%s\n",strMsg);}

Viewport Extent:(1,1), Viewport Org:(0,0) Window Extent:(1,1) Window Org(0,0)Viewport Extent:(1,1), Viewport Org:(200,200) Window Extent:(1,1) Window Org(100,100)The Orginal Point(In LP):CPoint(300,400),Convert to DP is:CPoint(400,500)The Orginal Point(In DP):CPoint(300,400),Convert to LP is:CPoint(200,300)MSDNSetWindowOrg(x,y)xySetVieportOrg(x,y)xy

MM_TEXT,

0,0DxLx(0,0)

(100,100)Lx

(200,200)Dx

.(300,400)

Dy,LyLyDy

VC300400

4Y100Y10011X100100200Y300

300400300400Y200Y20011X200200400Y500

2.1

WindowsGDIWindows CEGDIGWEWindows CEGWEGWEWindowsGWEGDIWindows

Windows CE2000XPWM_PAINTWM_PAINT

WindowsWindowsWM_PAINTWM_PAINTBeginPaintEndPaintBeginPaintBeginPaintWM_ERASEBACKGROUNDBeginPaintEndPaintWM_PAINTBeginPaint EndPaint

ValidateRectpost or sendWM_PAINT

BOOL InvalidateRect (HWND hWnd, const RECT *lpRect, BOOL bErase);

InvalidateRectlpRectNULLbEraseBeginPaintWindowsWindows CEhWnd

DCWindowsWindowsWindows CE

WindowsWindowsDC

WM_PAINTBeginPaintDCWM_PAINTWindowsWM_PAINT

WM_PAINTGetDCDCGetDC

HDC GetDC (HWND hWnd);

GetDCDCWM_PAINT

Windows CEDC

HDC GetDCEx (HWND hWnd, HRGN hrgnClip, DWORD flags);

GetDCExhrgnClipflagsDCDCWindows CEDCX_PARENTCLIP, DCX_NORESETATTRS, DCX_LOCKWINDOWUPDATE, DCX_VALIDATE

ReleaseDCReleaseDC

int ReleaseDC (HWND hWnd, HDC hDC);

GetDCDCHDC GetWindowDC (HWND hWnd);ReleaseDC

Windows CEDCXPDCWindows APIWindows CE

2.2

HelloCEDrawText

DrawText (hdc,

TEXT ("Hello Windows CE!"),

-1,

//-1NULL

&rect,//

DT_CENTER | DT_VCENTER | DT_SINGLELINE);

DrawTextWindowsDrawTextTEXTUnicodeWindows CE

-1NULLWindows

rectDrawText--DT_CALCRECTDrawTextDrawText(line break)HelloCEDT_CENTERDT_VCENTERDT_VCENTERDT_SINGLELINE

BOOL ExtTextOut (HDC hdc, int X, int Y,//

UINT fuOptions,

const RECT *lprc,

//

LPCTSTR lpString, UINT cbCount,

//

const int *lpDx);

//

ExtTextOutDrawTextExtTextOutxyDCrectopaqueNULLopaquing

Windows CEWindowsDrawTextExTextOutTextOut TabbedTextOutWindowsWindows CEWindows

HelloCeDrawTextDCWindows

SetTextColor GetTextColorWindwos CE

GetDeviceCaps

int GetDeviceCaps (HDC hdc, int nIndex);

DCDCDCDC256NUMCOLORS256NUMCOLORS-1BITSPIXELbitBITSPIXEL

nNumColors = GetDeviceCaps (hdc, NUMCOLORS);

if (nNumColors == -1)

nNumColors = 1 = FAMILYMAX) return 0; // Copy face name of font. lstrcpy (ffs[sFamilyCnt++].szFontFamily, lplf->lfFaceName); return rc;}

// EnumSingleFontFamily - Callback function that enumerates fontsint CALLBACK EnumSingleFontFamily (CONST LOGFONT *lplf, CONST TEXTMETRIC *lpntm, DWORD nFontType, LPARAM lParam) { PFONTFAMSTRUCT pffs;

pffs = (PFONTFAMSTRUCT) lParam; pffs->nNumFonts++; // Increment count of fonts in family return 1;}

// PaintSingleFontFamily - Callback function that draws a fontint CALLBACK PaintSingleFontFamily (CONST LOGFONT *lplf, CONST TEXTMETRIC *lpntm, DWORD nFontType, LPARAM lParam) { PPAINTFONTINFO ppfi; TCHAR szOut[256]; INT nFontHeight, nPointSize; HFONT hFont, hOldFont; ppfi = (PPAINTFONTINFO) lParam; //Translate lParam into struct pointer.

// Create the font from the LOGFONT structure passed. hFont = CreateFontIndirect (lplf);

hOldFont = (HFONT)SelectObject (ppfi->hdc, hFont); // Compute font size. nPointSize = (lplf->lfHeight * 72) / GetDeviceCaps(ppfi->hdc,LOGPIXELSY);

// Format string and paint on display. wsprintf (szOut, TEXT ("%s Point:%d"), lplf->lfFaceName, nPointSize); ExtTextOut (ppfi->hdc, 25, ppfi->yCurrent, 0, NULL, szOut, lstrlen (szOut), NULL); // Compute the height of the default font. nFontHeight = lpntm->tmHeight + lpntm->tmExternalLeading; // Update new draw point. ppfi->yCurrent += nFontHeight; // Deselect font and delete. SelectObject (ppfi->hdc, hOldFont); DeleteObject (hFont); return 1;}

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam);}

LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; INT i, rc; //Enumerate the available fonts. hdc = GetDC (hWnd); rc = EnumFontFamilies ((HDC)hdc, (LPTSTR)NULL, FontFamilyCallback, 0); for (i = 0; i < sFamilyCnt; i++) { ffs[i].nNumFonts = 0; rc = EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily, EnumSingleFontFamily, (LPARAM)(PFONTFAMSTRUCT)&ffs[i]); } ReleaseDC (hWnd, hdc); return 0;}

LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

PAINTSTRUCT ps; RECT rect; HDC hdc; TEXTMETRIC tm; INT nFontHeight, i; TCHAR szOut[256]; PAINTFONTINFO pfi;

GetClientRect (hWnd, &rect);

hdc = BeginPaint (hWnd, &ps);

// Get the height of the default font. GetTextMetrics (hdc, &tm); nFontHeight = tm.tmHeight + tm.tmExternalLeading;

// Initialize struct that is passed to enumerate function. pfi.yCurrent = rect.top; pfi.hdc = hdc; for (i = 0; i < sFamilyCnt; i++) {

// Format output string, and paint font family name. wsprintf (szOut, TEXT("Family: %s "), ffs[i].szFontFamily); ExtTextOut (hdc, 5, pfi.yCurrent, 0, NULL, szOut, lstrlen (szOut), NULL); pfi.yCurrent += nFontHeight;

// Enumerate each family to draw a sample of that font. EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily, PaintSingleFontFamily, (LPARAM)&pfi); } EndPaint (hWnd, &ps); return 0;}

LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}

OnCreateMainWM_CREATEEnumFontFamiliesFontFamilyNULLFontFamilyCallback

WM_PAINTOnPaintBeginPaintGetTextMetricsOnCreateMainEnumFontFamilies

PaintSingleFontFamilylParamPAINTFONTINFOlParamFontList

LOGFONThOldFontDrawTextExtTextOut

TEXTMETRICtmHeighttmExternalLeadingSelectObjectDeleteObjectWindows2-4

2-4PC

2-4Font

2.3

[]Winodws[]Windowsbits

WindowsDDBsDIBsDDBsDCDCDIBs

Windwos CEWindowsWindows CEDIBs

CreateBitmap

HBITMAP CreateBitmap (int nWidth, int nHeight,//

UINT cPlanes,

//1

UINT cBitsPerPel,

//

CONST VOID *lpvBits);//

nWidthnHeightcPlanesWindows CE1cBitspPerPelcBitspPerPel2Windows CE124816244Windows CEWindows

Windows CE402-5126*648

2-5

CreateCompatibleBitmap

HBITMAP CreateCompatibleBitmap (HDC hdc, int nWidth, int nHeight);

DC

DIBsBMPWindows 3.0WindowsDIB

WindowsDIBWindows CESHLoadDIBitmap

HBITMAP SHLoadDIBitmap (LPCTSTR szFileName);

Windows XPLR_LOADFROMFILELoadImageWindows CELoadImage

DIB

Windows CEDIBsDDBsDIBDIB

Windows 3.0DIBWindows CEXPDIBCreateDIBSection

HBITMAP CreateDIBSection (HDC hdc,

const BITMAPINFO *pbmi,

UINT iUsage,

//

void *ppvBits,

//

HANDLE hSection,//0

DWORD dwOffset); //0

Win32 APIDIBDIBWinodows NTDIBDIBDIBDCDCDCDCDIBNTWindow CE

BITMAPINFOBITMAPINFOHEADERRGBQUADBITMAPINFOHEADER

typedef struct tagBITMAPINFOHEADER{

DWORD biSize;

//

LONG biWidth;

//

LONG biHeight;

WORD biPlanes;

//1

WORD biBitCount;//

DWORD biCompression;//

DWORD biSizeImage;//

LONG biXPelsPerMeter;//

LONG biYPelsPerMeter;

DWORD biClrUsed;//

DWORD biClrImportant;//

} BITMAPINFOHEADER;

CreateBitmapOS/2BITMAPCOREINFOHEADERbiWidth, biHeight, biPlanes,biBitCountCreateBitmapbiHeightbiHeightCreateBitmapbiHeightCreateBitmapbiPlanes1

biCompressionWindows CEBI_RGBBI_BITFIELDSbiSizeImageBI_RGBbiSizeImage0BITMAPINFOHEADERdimensions

biXPelsPerMeterbiYPelsPerMeterCreateBIBSection0biClrUsed256256100--Windows--biClrImportant0

BITMAPINFOHEADERRGBQUAD

typedef struct tagRGBQUAD { /* rgbq */

BYTE rgbBlue;

BYTE rgbGreen;

BYTE rgbRed;

BYTE rgbReserved;

} RGBQUAD

256shade

RGBQUADDIB256242562562416322432RGBQUADQUAD

CreateDIBSectionWindows CEIUsageDIB_RGB_COLORSRGBDIB_PAL_COLORSDCPpvBits hSectiondwOffsetWindows CE0WindowsWindows CECreateDIBSection

GetDIBColorTableSetDIBColorTableDIB

UINT GetDIBColorTable (HDC hdc,

UINT uStartIndex,//

UINT cEntries,

//

RGBQUAD *pColors);//

UINT SetDIBColorTable (HDC hdc, UINT uStartIndex,UINT cEntries, RGBQUAD *pColors);

uStartIndexCEntriesRGBQUADSetDIBColorTableGetDIBColorTable

DCDC

WindowsWindowsDC

CreateCompatibleDC

HDC CreateCompatibleDC (HDC hdc);

DCDCSelectObjectDCBitBlt StretchBltDCDC

BOOL BitBlt (HDC hdcDest,

int nXDest, int nYDest, int nWidth, int nHeight,//

HDC hdcSrc,

//

int nXSrc, int nYSrc,//DC

DWORD dwRop);

//

BitBltbit blit43DC

dwRopROPROPSRCOPYROPSRCPAINTROPSRCINVERTROPbrushROPWindows CE

// Create a DC that matches the device.

hdcMem = CreateCompatibleDC (hdc);

// Select the bitmap into the compatible device context.

hOldSel = SelectObject (hdcMem, hBitmap);

// Get the bitmap dimensions from the bitmap.

GetObject (hBitmap, sizeof (BITMAP), &bmp);

// Copy the bitmap image from the memory DC to the screen DC.

BitBlt (hdc, rect.left, rect.top, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY);

// Restore original bitmap selection and destroy the memory DC.

SelectObject (hdcMem, hOldSel);

DeleteDC (hdcMem);

DCGetObjectGetObjectBitBlitDCDeleteDCDCDeleteDCReleaseDCReleaseDCDCDeleteDCCreateCompatibleDCReleaseDCGetDCGetWindowsDC

StretchBlt

BOOL StretchBlt (HDC hdcDest,

int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,

HDC hdcSrc,

int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,

DWORD dwRop);

StretchBltBitBltROP

Windows CETransparentImage

BOOL TransparentImage (HDC hdcDest, LONG DstX, LONG DstY, LONG DstCx, LONG DstCy,

HANDLE hSrc, LONG SrcX, LONG SrcY, LONG SrcCx, LONG SrcCy,

COLORREF TransparentColor);

StretchBlthSrcTransparentImageWindows 2000TransparentBltTransparetBlt

WindowsWindows CE2blitPatBltMaskBltPatBltDCMaskBltBitBltDC

2.4

WindowsWindows CEChord, Arc, Pie"[current point]"MoveToEx, LineToGetCurrentPositionExGDIWindows CE ArcToPolyBezierToWindows CE

Polyline

BOOL Polyline (HDC hdc,

const POINT *lppt,//POINT

int cPoints);

//point

2POINT

typedef struct tagPOINT {

LONG x;

LONG y;

} POINT

XYpoint0,050,100

POINTS pts[2];

pts[0].x = 0; pts[0].y = 0;

pts[1].x = 50;pts[1].y = 100;

PolyLine (hdc, &pts, 2);

MoveToExLineTo

BOOL WINAPI MoveToEx (HDC hdc, int X, int Y, LPPOINT lpPoint);

BOOL WINAPI LineTo (HDC hdc, int X, int Y);

MoveToExLineTo

MoveToEx (hdc, 0, 0, NULL);

LineTo (hdc, 50, 100);

GetCurrentPositionEx

WINGDIAPI BOOL WINAPI GetCurrentPositionEx (HDC hdc, LPPOINT pPoint);

(0, 0)(50, 100)WindowsWindows CE

penGDIGDI GDIGetStockObject

HGDIOBJ GetStockObject (int fnObject);

(HPEN)GetStockObject (WHITE_PEN);//WHITE_PEN, BLACK_PENNULL_PEN

(HFONT)GetStockObject (SYSTEM_FONT);

(HBRUSH)GetStockObject (WHITE_BRUSH);// BLACK_BRUSHLTGRAY_BRUSH

Windows13NULLGetStockObjectWHITE_PEN, BLACK_PENNULL_PEN

Windows

HPEN CreatePen (int fnPenStyle, int nWidth, COLORREF crColor);

fnPenStylePS_DASHWindows CEPS_SOLIDPS_DASHPS_NULLnWidthcrColorcrColorCOLORREFRGBRGB

COLORREF RGB (BYTE bRed, BYTE bGreen, BYTE bBlue);

hPen = CreatePen (PS_SOLID, 1, RGB (0xff, 0, 0));

HPEN CreatePenIndirect (const LOGPEN *lplgpn);

LOGPEN

typedef struct tagLOGPEN {

UINT lopnStyle;

POINT lopnWidth;

COLORREF lopnColor;

} LOGPEN;

CreatePenIndirectWindowsCreatePenIndirect1

LOGPEN lp;

HPEN hPen;

lp.lopnStyle = PS_SOLID;

lp.lopnWidth.x = 1;

lp.lopnWidth.y = 1;

lp.lopnColor = RGB (0xff, 0, 0);

hPen = CreatePenIndirect (&lp);

Windows CE1GetDeviceCaps2LINECAPSWindows CE

GDI--brush8*8WindowsWindows CEGetStockObject

HBRUSH CreateSolidBrush (COLORREF crColor);

crColorRGB

Windows CEWin32 CreateDIBPatternBrushPt

HBRUSH CreateDIBPatternBrushPt (const void *lpPackedDIB, UINT iUsage);

DIBBITMAPINFOBITMAPINFOBITMAPINFOHEADERRGBQUADDIBDIB_RGB_COLORSRGBQUAD8DIB_PAL_COLORSWindows CE

Windows CECreateDIBPatternBrushPtWindows CEWindowsCreateHatchBrushCreateDIBPatternBrushPtWindows CE

100,100125,22088*8

BOOL SetBrushOrgEx (HDC hdc, int nXOrg, int nYOrg, LPPOINT lppt);

nXOrgnYOrg078*8lppt

WindowsWindows CEWindowsRectangle, RoundRect, EllipsePolygon

BOOL Rectangle (HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);Rectangle

0,05*7Rectangle(0,0,5,7);12-62-6Rectangele46WindowsRectangle1WindowsPS_INSIDEFRAME

EllipseBOOL Ellipse (HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);bounding rectangle2-7Rectangle2-7Ellipse

RoundRectBOOL RoundRect (HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);2-82-8RoundRect

PolygonBOOL Polygon (HDC hdc, const POINT *lpPoints, int nCount);2Point

int FillRect (HDC hDC, CONST RECT* lprc, HBRUSH hbr);FillRectFillRectFillRectGradientFillGradientFill2-9GradientFill2-9GradientFillGradientFillBOOL GradientFill (HDC hdc, PTRIVERTEX pVertex, ULONG dwNumVertex, PVOID pMesh, ULONG dwNumMesh, ULONG dwMode);pVertexTRIVERTEXdwNumVertexTRIVERTEXTRIVERTEXstruct _TRIVERTEX { LONG x; Long y; COLOR16 Red; COLOR16 Green; COLOR16 Blue; COLOR16 Alpha;s} TRIVERTEX;TRIVERTEXRGBpMeshGRADIENT_RECTstruct _GRADIENT_RECT{ ULONG UpperLeft; ULONG LowerRight;} GRADIENT_RECT;GRADIENT_RECTTRIVERTEXdwNumMeshGRADIENT_RECTdwModeGRADIENT_FILL_RECT_HGRADIENT_FILL_RECT_VGradientFillWindows CE2-9TRIVERTEX vert[2];GRADIENT_RECT gRect; vert [0] .x = prect->left;vert [0] .y = prect->top;vert [0] .Red = 0x0000;vert [0] .Green = 0x0000;vert [0] .Blue = 0xff00;vert [0] .Alpha = 0x0000; vert [1] .x = prect->right;vert [1] .y = prect->bottom;vert [1] .Red = 0x0000;vert [1] .Green = 0xff00;vert [1] .Blue = 0x0000;vert [1] .Alpha = 0x0000; gRect.UpperLeft = 0;gRect.LowerRight = 1; GradientFill(hdc,vert,2,&gRect,1,GRADIENT_FILL_RECT_H);

Shapes

2-3ShapesShapes4Listing 2-3: ShapesShapes.h//================================================================// Header file//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================// Returns number of elements#define dim(x) (sizeof(x) / sizeof(x[0])) //----------------------------------------------------------------------// Generic defines and data types//struct decodeUINT { // Structure associates UINT Code; // messages // with a function. LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);}; struct decodeCMD { // Structure associates UINT Code; // menu IDs with a LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.}; //----------------------------------------------------------------------// Defines used by MyCreateHatchBrush//typedef struct { BITMAPINFOHEADER bmi; COLORREF dwPal[2]; BYTE bBits[64];} BRUSHBMP; #define HS_HORIZONTAL 0 /* ----- */#define HS_VERTICAL 1 /* ||||| */#define HS_FDIAGONAL 2 /* \\\\\ */#define HS_BDIAGONAL 3 /* ///// */#define HS_CROSS 4 /* +++++ */#define HS_DIAGCROSS 5 /* xxxxx */ //----------------------------------------------------------------------// Function prototypes//HWND InitInstance (HINSTANCE, LPWSTR, int);int TermInstance (HINSTANCE, int); // Window proceduresLRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);// Message handlersLRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

Shapes.cpp//======================================================================// Shapes- Brush and shapes demo for Windows CE//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include // For all that Windows stuff#include "shapes.h" // Program-specific stuff //----------------------------------------------------------------// Global data//const TCHAR szAppName[] = TEXT ("Shapes");HINSTANCE hInst; // Program instance handle // Message dispatch table for MainWindowProcconst struct decodeUINT MainMessages[] = { WM_PAINT, DoPaintMain, WM_DESTROY, DoDestroyMain,}; //======================================================================//// Program entry point//int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwndMain; // Initialize this instance. hwndMain = InitInstance(hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam);}//----------------------------------------------------------------------// InitInstance - Instance initialization//HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){ WNDCLASS wc; HWND hWnd; // Save program instance handle in global variable. hInst = hInstance; #if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, allow only one instance of the application. hWnd = FindWindow (szAppName, NULL); if (hWnd) { SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01)); return 0; }#endif // Register application main window class. wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass (&wc) == 0) return 0; // Create main window. hWnd = CreateWindowEx (WS_EX_NODRAG, // Ex Style szAppName, // Window class TEXT("Shapes"), // Window title WS_VISIBLE, // Style flags CW_USEDEFAULT, // x position CW_USEDEFAULT, // y position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height NULL, // Parent NULL, // Menu, must be null hInstance, // Application instance NULL); // Pointer to create // parameters // Return fail code if window not created. if (!IsWindow (hWnd)) return 0; // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd;}//----------------------------------------------------------------------// TermInstance - Program cleanup//int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC;}//======================================================================// Message handling procedures for MainWindow// //----------------------------------------------------------------------// MainWndProc - Callback function for application window//LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam);}//----------------------------------------------------------------// MyCreateHatchBrush - Creates hatched brushes//HBRUSH MyCreateHatchBrush (INT fnStyle, COLORREF clrref) { BRUSHBMP brbmp; BYTE *pBytes; int i; DWORD dwBits[6][2] = { {0x000000ff,0x00000000}, {0x10101010,0x10101010}, {0x01020408,0x10204080}, {0x80402010,0x08040201}, {0x101010ff,0x10101010}, {0x81422418,0x18244281}, }; if ((fnStyle < 0) || (fnStyle > dim(dwBits))) return 0; memset (&brbmp, 0, sizeof (brbmp)); brbmp.bmi.biSize = sizeof (BITMAPINFOHEADER); brbmp.bmi.biWidth = 8; brbmp.bmi.biHeight = 8; brbmp.bmi.biPlanes = 1; brbmp.bmi.biBitCount = 1; brbmp.bmi.biClrUsed = 2; brbmp.bmi.biClrImportant = 2; // Initialize the palette of the bitmap. brbmp.dwPal[0] = PALETTERGB(0xff,0xff,0xff); brbmp.dwPal[1] = PALETTERGB((BYTE)((clrref >> 16) & 0xff), (BYTE)((clrref >> 8) & 0xff), (BYTE)(clrref & 0xff)); // Write the hatch data to the bitmap. pBytes = (BYTE *)&dwBits[fnStyle]; for (i = 0; i < 8; i++) brbmp.bBits[i*4] = *pBytes++; // Return the handle of the brush created. return CreateDIBPatternBrushPt (&brbmp, DIB_RGB_COLORS);}//----------------------------------------------------------------------// DoPaintMain - Process WM_PAINT message for window.//LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect; HDC hdc; POINT ptArray[6]; HBRUSH hBr, hOldBr; TCHAR szText[128]; GetClientRect (hWnd, &rect); hdc = BeginPaint (hWnd, &ps); // Draw ellipse. hBr = (HBRUSH) GetStockObject (DKGRAY_BRUSH); hOldBr = (HBRUSH) SelectObject (hdc, hBr); Ellipse (hdc, 10, 50, 90, 130); SelectObject (hdc, hOldBr); // Draw round rectangle. hBr = (HBRUSH) GetStockObject (LTGRAY_BRUSH); hOldBr = (HBRUSH) SelectObject (hdc, hBr); RoundRect (hdc, 95, 50, 150, 130, 30, 30); SelectObject (hdc, hOldBr); // Draw hexagon using Polygon. hBr = (HBRUSH) GetStockObject (WHITE_BRUSH); hOldBr = (HBRUSH) SelectObject (hdc, hBr); ptArray[0].x = 192; ptArray[0].y = 50; ptArray[1].x = 155; ptArray[1].y = 75; ptArray[2].x = 155; ptArray[2].y = 105; ptArray[3].x = 192; ptArray[3].y = 130; ptArray[4].x = 230; ptArray[4].y = 105; ptArray[5].x = 230; ptArray[5].y = 75; Polygon (hdc, ptArray, 6); SelectObject (hdc, hOldBr); hBr = (HBRUSH) MyCreateHatchBrush (HS_DIAGCROSS, RGB (0, 0, 0)); hOldBr = (HBRUSH) SelectObject (hdc, hBr); Rectangle (hdc, 10, 145, 225, 210); SelectObject (hdc, hOldBr); DeleteObject (hBr); SetBkMode (hdc, OPAQUE); lstrcpy (szText, TEXT ("Opaque background")); ExtTextOut (hdc, 20, 160, 0, NULL, szText, lstrlen (szText), NULL); SetBkMode (hdc, TRANSPARENT); lstrcpy (szText, TEXT ("Transparent background")); ExtTextOut (hdc, 20, 185, 0, NULL, szText, lstrlen (szText), NULL); EndPaint (hWnd, &ps); return 0;}//----------------------------------------------------------------------// DoDestroyMain - Process WM_DESTROY message for window//LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}

ShapesOnPaintMainGetStockObjectCreateDIBPatternBrushPtMyCreateHatchBrushWindows CECreateHatchBrushCreateDIBPatternBrushPta8*81003

opaquetransparentopaquetransparent2-10Shapes

2-10

Shapes240ShapesPCPocket PCWindows CEGWEGDIGDIWindows CEWindows Programming Windows, 5th edition, by Charles Petzold(Microsoft Press, 1998)Windows GDI

--

3

WindowsWindows CEWindowsPCWindowsWindows CEWindows CE

3.1

Windows CEPocket PC--Windows CE

WindowsZExplorerAlt-Esc

WindowsGetFocusHWND GetFocus (void);SetFocusHWND SetFocus (HWND hWnd);

Windows CESetFocusSetFocusSetFocus

WindowsWM_KILLFOCUSwParamWM_SETFOCUSwParam

SetFocusSetFocusWM_SETFOCUS

Windows CEWindowsWindowsWM_KEYDOWNWindowsWM_KEYDOWNWM_CHARWM_CHARWM_KEYDOWNWindowsWM_KEYUPWM_KEYDOWNWM_CHARWM_KEYUPAltWM_SYSKEYDOWNWM_SYSKEYCHARWM_SYSKEYUP

wParamlParamWM_KEYxxWM_SYSKEYxxwParamWindows3-1VK_xxWindows CEPCWindows CEPCWindows CE3-1Windows CEWindows CE

3-1

3-1PCWindows CE

WM_CHARWM_SYSCHARwParamUnicodeWM_CHARWM_KEYDOWNWM_KEYUPWM_CHAR2

lParam3-2lParam

015Windows CEWindows CE29Alt3003116-28Windows CEWindows CE

3-2lParam

WM_DEADCHARWM_DEADCHAR2

WM_DEADCHARWindowsWindows CEWindows CEWM_DEADCHAR

WindowsGetKeyStateGetAsyncKeyStateGetKeyState

short GetKeyState (int nVirtKey);

CtrlAltShift/ShiftShifeCtrlAlt

nVirKeyAltVK_MENU

GetKeyStateWindows CEWindowsGetKeyState

short GetAsyncKeyState(int vKey);GetKeyStateGetAsyncKeyStateGetKeyStateGetKeyStateGetAsyncKeyStateGetKeyStateGetAsyncKeyStateShiftCtrlAltVK_LBUTTONGetAsyncKeyState

keybd_event

void keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, DWORD dwExtraInfo);

bScanWindows CENULLdwFlagsKEYEVENTF_KEYUPKEYEVENTF_SILENTkeydb_event2KEYEVENTF_KEYUPKEYEVENTF_KEYUPVKVK_LSHIFVK_RCONTROL

Windows CE

BOOL PostKeybdMessage (HWND hwnd, UINT VKey, //0

KEY_STATE_FLAGS KeyStateFlags, //

UINT cCharacters,

//

UINT *pShiftStateBuffer, //shift

UINT *pCharacterBuffer );//VK

hwndVKey0KeyStateFlagscCharacterspShiftStateBuffershiftpCharacterBufferVKkeybd_event

MapVirtualKeyWindows CEMapVirtualKeyWindows

UINT MapVirtualKey (UINT uCode, UINT uMapType);

Windows CEuMapTypeMapVirtualKeyOEMMapVirtualKey

DWORD GetKeyboardStatus (VOID);KBDI_KEYBOARD_PRESENTKBDI_KEYBOARD_ENABLEDbEnableFALSEBOOL EnableHardwareKeyboard (BOOL bEnable)

KeyTrac

KeyTracKeyTracWM_PAINTwParamlParam3-3KeyTrac

3-3Shift-AaKeyTrac

KeyTracaWM_KEYDOWN,WM_CHARWM_KEYUPaShiftShiftShiftaShiftShiftWM_CHARaWM_CHARwParam0x41Aa3-1KeyTrac3-1KeyTracKeyTrac.h//======================================================================// Returns number of elements#define dim(x) (sizeof(x) / sizeof(x[0]))

struct decodeUINT { // Structure associates UINT Code; // messages // with a function. LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);}; struct decodeCMD { // Structure associates UINT Code; // menu IDs with a LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.};

// Program-specific defines and structures

typedef struct { UINT wKeyMsg; INT wParam; INT lParam; LPCTSTR pszMsgTxt; TCHAR szShift[20];} MYKEYARRAY, *PMYKEYARRAY;// Structure to associate messages with text name of messagetypedef struct { UINT wMsg; LPCTSTR pName;} KEYNAMESTRUCT;

// Function prototypes

HWND InitInstance (HINSTANCE, LPWSTR, int);int TermInstance (HINSTANCE, int); // Window proceduresLRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);// Message handlersLRESULT DoCreateMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoKeysMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

KeyTrac.cpp//======================================================================// KeyTrac - displays keyboard messages//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include // For all that Windows stuff#include // Command bar includes#include "keytrac.h" // Program-specific stuff // The include and lib files for the Pocket PC are conditionally// included so that this example can share the same project file. This// is necessary since this example must have a menu bar on the Pocket// PC to have a SIP button.#if defined(WIN32_PLATFORM_PSPC)#include // Add Pocket PC includes.#pragma comment( lib, "aygshell" ) // Link Pocket PC lib for menu bar.#endif

// Global data

const TCHAR szAppName[] = TEXT ("KeyTrac");HINSTANCE hInst; // Program instance handle // Program-specific global dataMYKEYARRAY ka[16];int nKeyCnt = 0;int nFontHeight; // Array associates key messages with text tagsKEYNAMESTRUCT knArray[] = {{WM_KEYDOWN, TEXT ("WM_KEYDOWN")}, {WM_KEYUP, TEXT ("WM_KEYUP")}, {WM_CHAR, TEXT ("WM_CHAR")}, {WM_SYSCHAR, TEXT ("WM_SYSCHAR")}, {WM_SYSKEYUP, TEXT ("WM_SYSKEYUP")}, {WM_SYSKEYDOWN, TEXT ("WM_SYSKEYDOWN")}, {WM_DEADCHAR, TEXT ("WM_DEADCHAR")}, {WM_SYSDEADCHAR, TEXT ("WM_SYSDEADCHAR")}};// Message dispatch table for MainWindowProcconst struct decodeUINT MainMessages[] = { WM_CREATE, DoCreateMain, WM_PAINT, DoPaintMain, WM_KEYUP, DoKeysMain, WM_KEYDOWN, DoKeysMain, WM_CHAR, DoKeysMain, WM_DEADCHAR, DoKeysMain, WM_SYSCHAR, DoKeysMain, WM_SYSDEADCHAR, DoKeysMain, WM_SYSKEYDOWN, DoKeysMain, WM_SYSKEYUP, DoKeysMain, WM_DESTROY, DoDestroyMain,}; //======================================================================// Program entry point

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; int rc = 0; HWND hwndMain; // Initialize this instance. hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam);}

// InitInstance - Instance initialization

HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; HWND hWnd; #if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, allow only one instance of the application hWnd = FindWindow (szAppName, NULL); if (hWnd) { SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01)); return 0; }#endif hInst = hInstance; // Save program instance handle // Register application main window class. wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass(&wc) == 0) return 0; // Create main window. hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName, TEXT ("KeyTrac"), WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Fail if window not created if (!IsWindow (hWnd)) return 0; // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd;}

// TermInstance - Program cleanup

int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC;}//======================================================================// Message handling procedures for MainWindow////----------------------------------------------------------------------// MainWndProc - Callback function for application window//LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam);}//----------------------------------------------------------------------// DoCreateMain - Process WM_CREATE message for window.//LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; TEXTMETRIC tm; #if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300) SHMENUBARINFO mbi; // For Pocket PC, create memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we mbi.cbSize = sizeof(SHMENUBARINFO); // have a sip button mbi.hwndParent = hWnd; mbi.dwFlags = SHCMBF_EMPTYBAR; // No menu SHCreateMenuBar(&mbi);#endif // Get the height of the default font. hdc = GetDC (hWnd); GetTextMetrics (hdc, &tm); nFontHeight = tm.tmHeight + tm.tmExternalLeading; ReleaseDC (hWnd, hdc); return 0;}//----------------------------------------------------------------------// DoPaintMain - Process WM_PAINT message for window.//LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect, rectOut; TCHAR szOut[256]; HDC hdc; INT i, j; LPCTSTR pKeyText; GetClientRect (hWnd, &rect); // Create a drawing rectangle for the top line of the window. rectOut = rect; rectOut.bottom = rectOut.top + nFontHeight; hdc = BeginPaint (hWnd, &ps); if (nKeyCnt) { for (i = 0; i < nKeyCnt; i++) { // Create string containing wParam, lParam, and shift data. wsprintf (szOut, TEXT ("wP:%08x lP:%08x shift: %s"), ka[i].wParam, ka[i].lParam, ka[i].szShift); // Look up name of key message. for (j = 0; j < dim (knArray); j++) if (knArray[j].wMsg == ka[i].wKeyMsg) break; // See if we found the message. if (j < dim (knArray)) pKeyText = knArray[j].pName; else pKeyText = TEXT ("Unknown"); // Scroll the window one line. ScrollDC (hdc, 0, nFontHeight, &rect, &rect, NULL, NULL); // See if wide or narrow screen. if (GetSystemMetrics (SM_CXSCREEN) < 480) { // If Pocket PC, display info on 2 lines ExtTextOut (hdc, 10, rect.top, ETO_OPAQUE, &rectOut, szOut, lstrlen (szOut), NULL); // Scroll the window another line. ScrollDC(hdc, 0, nFontHeight, &rect, &rect, NULL, NULL); ExtTextOut (hdc, 5, rect.top, ETO_OPAQUE, &rectOut, pKeyText, lstrlen (pKeyText), NULL); } else { // Wide screen, print all on one line. ExtTextOut (hdc, 5, rect.top, ETO_OPAQUE, &rectOut, pKeyText, lstrlen (pKeyText), NULL); ExtTextOut (hdc, 100, rect.top, 0, NULL, szOut, lstrlen (szOut), NULL); } } nKeyCnt = 0; } EndPaint (hWnd, &ps); return 0;}//----------------------------------------------------------------------// DoKeysMain - Process all keyboard messages for window.//LRESULT DoKeysMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { if (nKeyCnt >= 16) return 0; ka[nKeyCnt].wKeyMsg = wMsg; ka[nKeyCnt].wParam = wParam; ka[nKeyCnt].lParam = lParam; // Capture the state of the shift flags. ka[nKeyCnt].szShift[0] = TEXT ('\0'); if (GetKeyState (VK_LMENU)) lstrcat (ka[nKeyCnt].szShift, TEXT ("lA ")); if (GetKeyState (VK_RMENU)) lstrcat (ka[nKeyCnt].szShift, TEXT ("rA ")); if (GetKeyState (VK_MENU)) lstrcat (ka[nKeyCnt].szShift, TEXT ("A ")); if (GetKeyState (VK_LCONTROL)) lstrcat (ka[nKeyCnt].szShift, TEXT ("lC ")); if (GetKeyState (VK_RCONTROL)) lstrcat (ka[nKeyCnt].szShift, TEXT ("rC ")); if (GetKeyState (VK_CONTROL)) lstrcat (ka[nKeyCnt].szShift, TEXT ("C ")); if (GetKeyState (VK_LSHIFT)) lstrcat (ka[nKeyCnt].szShift, TEXT ("lS ")); if (GetKeyState (VK_RSHIFT)) lstrcat (ka[nKeyCnt].szShift, TEXT ("rS ")); if (GetKeyState (VK_SHIFT)) lstrcat (ka[nKeyCnt].szShift, TEXT ("S ")); nKeyCnt++; InvalidateRect (hWnd, NULL, FALSE); return 0;}//----------------------------------------------------------------------// DoDestroyMain - Process WM_DESTROY message for window.//LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}

KeyTracInvalidateRectWM_PAINTsend or postWM_PAINTWindowsWM_PAINT

KeyTracBOOL ScrollDC (HDC hDC, int dx, int dy, const RECT *lprcScroll,

const RECT *lprcClip, HRGN hrgnUpdate,LPRECT lprcUpdate);

Windows CE ScrollDCScrollDC

KeyTracKeyTrac

Windows 256

VK_LBUTTON=&H1VK_RBUTTON=&H2VK_CANCEL=&H3VK_MBUTTON=&H4VK_BACK=&H8VK_TAB=&H9VK_CLEAR=&HCVK_RETURN=&HDVK_SHIFT=&H10VK_CONTROL=&H11VK_MENU=&H12VK_PAUSE=&H13VK_CAPITAL=&H14VK_ESCAPE=&H1BVK_SPACE=&H20VK_PRIOR=&H21VK_NEXT=&H22VK_END=&H23VK_HOME=&H24VK_LEFT=&H25VK_UP=&H26VK_RIGHT=&H27VK_DOWN=&H28VK_Select=&H29VK_PRINT=&H2AVK_EXECUTE=&H2BVK_SNAPSHOT=&H2CVK_Insert=&H2DVK_Delete=&H2EVK_HELP=&H2FVK_0=&H30VK_1=&H31VK_2=&H32VK_3=&H33VK_4=&H34VK_5=&H35VK_6=&H36VK_7=&H37VK_8=&H38VK_9=&H39VK_A=&H41VK_B=&H42VK_C=&H43VK_D=&H44VK_E=&H45VK_F=&H46VK_G=&H47VK_H=&H48VK_I=&H49VK_J=&H4AVK_K=&H4BVK_L=&H4CVK_M=&H4DVK_N=&H4EVK_O=&H4FVK_P=&H50VK_Q=&H51VK_R=&H52VK_S=&H53VK_T=&H54VK_U=&H55VK_V=&H56VK_W=&H57VK_X=&H58VK_Y=&H59VK_Z=&H5AVK_STARTKEY=&H5BVK_CONTEXTKEY=&H5DVK_NUMPAD0=&H60VK_NUMPAD1=&H61VK_NUMPAD2=&H62VK_NUMPAD3=&H63VK_NUMPAD4=&H64VK_NUMPAD5=&H65VK_NUMPAD6=&H66VK_NUMPAD7=&H67VK_NUMPAD8=&H68VK_NUMPAD9=&H69VK_MULTIPLY=&H6AVK_ADD=&H6BVK_SEPARATOR=&H6CVK_SUBTRACT=&H6DVK_DECIMAL=&H6EVK_DIVIDE=&H6FVK_F1=&H70VK_F2=&H71VK_F3=&H72VK_F4=&H73VK_F5=&H74VK_F6=&H75VK_F7=&H76VK_F8=&H77VK_F9=&H78VK_F10=&H79VK_F11=&H7AVK_F12=&H7BVK_F13=&H7CVK_F14=&H7DVK_F15=&H7EVK_F16=&H7FVK_F17=&H80VK_F18=&H81VK_F19=&H82VK_F20=&H83VK_F21=&H84VK_F22=&H85VK_F23=&H86VK_F24=&H87VK_NUMLOCK=&H90VK_OEM_SCROLL=&H91VK_OEM_1=&HBAVK_OEM_PLUS=&HBBVK_OEM_COMMA=&HBCVK_OEM_MINUS=&HBDVK_OEM_PERIOD=&HBEVK_OEM_2=&HBFVK_OEM_3=&HC0VK_OEM_4=&HDBVK_OEM_5=&HDCVK_OEM_6=&HDDVK_OEM_7=&HDEVK_OEM_8=&HDFVK_ICO_F17=&HE0VK_ICO_F18=&HE1VK_OEM102=&HE2VK_ICO_HELP=&HE3VK_ICO_00=&HE4VK_ICO_CLEAR=&HE6VK_OEM_RESET=&HE9VK_OEM_JUMP=&HEAVK_OEM_PA1=&HEBVK_OEM_PA2=&HECVK_OEM_PA3=&HEDVK_OEM_WSCTRL=&HEEVK_OEM_CUSEL=&HEFVK_OEM_ATTN=&HF0VK_OEM_FINNISH=&HF1VK_OEM_COPY=&HF2VK_OEM_AUTO=&HF3VK_OEM_ENLW=&HF4VK_OEM_BACKTAB=&HF5VK_ATTN=&HF6VK_CRSEL=&HF7VK_EXSEL=&HF8VK_EREOF=&HF9VK_PLAY=&HFAVK_ZOOM=&HFBVK_NONAME=&HFCVK_PA1=&HFDVK_OEM_CLEAR=&HFE

3.2

PCWindows CEWindows CEWindows CE

WM_MOUSEMOVEWM_LBUTTONDOWNWM_RBUTTONDOWNWM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONDOWNWM_MBUTTONUP

wParamlParamwParamCtrlShiftWindows AltAltGetKeyState

lParam1616x16y

WindowsWM_LBUTTONDBLCLKCS_DBLCLKSRegisterClass

WM_LBUTTONDOWNWM_LBUTTONUP

WM_LBUTTONDBLCLKWM_LBUTTONUPWM_LBUTTONDOWNWM_LBUTTONDBLCLK

WM_MOUSEWHEELlParamwParamwParamWHEEL_DELTA

WindowsWindows CEWindowsWindows CE

Windows CEWindowsWindows CESetCursorWindows

WM_LBUTTONDOWNWM_LBUTTONUPWM_MOUSEMOVE

WM_MOUSEMOVECEWindows CEBOOL GetMouseMovePoints (PPOINT pptBuf, UINT nBufPoints, UINT *pnPointsRetrieved);GetMouseMovePointsWM_MOUSEMOVEWM_MOUSEMOVEWM_MOUSEMOVE

GetMouseMovePoints4GetMouseMovePoints4

PenTracGetMouseMovePoints3-4PenTracWM_MOUSEMOVEGetMouseMovePointsWM_MOUSEMOVEGetMouseMovePoints

3-4PenTrac

3-2PenTracWM_MOUSEMOVEWM_LBUTTONDOWNShiftPenTracGetMouseMovePoints

GetMouseMovePointsPenTracWM_MOUSEMOVEWM_LBUTTONDOWNDoMouseMainsleep

3-2PenTrac

PenTrac.h//======================================================================// Header file//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================// Returns number of elements.#define dim(x) (sizeof(x) / sizeof(x[0])) //----------------------------------------------------------------------// Generic defines and data types//struct decodeUINT { // Structure associates UINT Code; // messages // with a function. LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);}; struct decodeCMD { // Structure associates UINT Code; // menu IDs with a LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.}; //----------------------------------------------------------------------// Function prototypes//HWND InitInstance (HINSTANCE, LPWSTR, int);int TermInstance (HINSTANCE, int); // Window proceduresLRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM); // Message handlersLRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoMouseMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

PenTrac.cpp//======================================================================// PenTrac - Tracks stylus movement//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================#include // For all that Windows stuff#include "pentrac.h" // Program-specific stuff //----------------------------------------------------------------------// Global data//const TCHAR szAppName[] = TEXT ("PenTrac");HINSTANCE hInst; // Program instance handle // Message dispatch table for MainWindowProcconst struct decodeUINT MainMessages[] = { WM_LBUTTONDOWN, DoMouseMain, WM_MOUSEMOVE, DoMouseMain, WM_DESTROY, DoDestroyMain,}; //======================================================================// Program entry point//int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; int rc = 0; HWND hwndMain; // Initialize this instance. hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam);}//----------------------------------------------------------------------// InitApp - Application initialization//HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; HWND hWnd; #if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, allow only one instance of the application hWnd = FindWindow (szAppName, NULL); if (hWnd) { SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01)); return 0; }#endif // Save program instance handle in global variable. hInst = hInstance; // Register application main window class. wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass (&wc) == 0) return 0; // Create main window. hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName, TEXT ("PenTrac"), WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Return fail code if window not created. if (!IsWindow (hWnd)) return 0; // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd;}//----------------------------------------------------------------------// TermInstance - Program cleanup//int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC;}//======================================================================// Message handling procedures for MainWindow// //----------------------------------------------------------------------// MainWndProc - Callback function for application window//LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam);}//----------------------------------------------------------------------// DoMouseMain - Process WM_LBUTTONDOWN and WM_MOUSEMOVE messages// for window.//LRESULT DoMouseMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { POINT pt[64]; POINT ptM; UINT i, uPoints = 0; HDC hdc; ptM.x = LOWORD (lParam); ptM.y = HIWORD (lParam); hdc = GetDC (hWnd); // If shift and mouse move, see if any lost points. if (wMsg == WM_MOUSEMOVE) { if (wParam & MK_SHIFT) GetMouseMovePoints (pt, 64, &uPoints); for (i = 0; i < uPoints; i++) { pt[i].x /= 4; // Convert move pts to screen coords pt[i].y /= 4; // Covert screen coordinates to window coordinates MapWindowPoints (HWND_DESKTOP, hWnd, &pt[i], 1); SetPixel (hdc, pt[i].x, pt[i].y, RGB (255, 0, 0)); SetPixel (hdc, pt[i].x+1, pt[i].y, RGB (255, 0, 0)); SetPixel (hdc, pt[i].x, pt[i].y+1, RGB (255, 0, 0)); SetPixel (hdc, pt[i].x+1, pt[i].y+1, RGB (255, 0, 0)); } } // The original point is drawn last in case one of the points // returned by GetMouseMovePoints overlaps it. SetPixel (hdc, ptM.x, ptM.y, RGB (0, 0, 0)); SetPixel (hdc, ptM.x+1, ptM.y, RGB (0, 0, 0)); SetPixel (hdc, ptM.x, ptM.y+1, RGB (0, 0, 0)); SetPixel (hdc, ptM.x+1, ptM.y+1, RGB (0, 0, 0)); ReleaseDC (hWnd, hdc); // Kill time to make believe we are busy. Sleep(25); return 0;}//----------------------------------------------------------------------// DoDestroyMain - Process WM_DESTROY message for window.//LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}

WM_MOUSEMOVEWM_MOUSEMOVE

HWND SetCapture (HWND hWnd)NULLBOOL ReleaseCapture(void)HWND GetCapture(void)00GetCapture0

GetCaptureWM_CAPTURECHANGEDlParamGetCapture

WindowsWindowsWM_RBUTTONDOWNWM_RBUTTONUPWindows CEAltWM_LBUTTONDOWNwParamMK_ALTAltVK_MENUGetKeyStateGetKeyState

SHRecognizeGesturePocket PCShellWindows CEWINSHELLAPI DWORD SHRecongnizeGesture(SHRGINFO * shrg);SHRGINFOtypedef struct tagSHRGI{DWORD cbSize;HWND hwndClient;POINT ptDown;DWORD dwFlags;}SHRGINFO,*PSHRGINFO;

cbSizehwndClientptDowndwFlagsSHRG_RETURNCMDGN_CONTEXTMENU0SHRG_NOTIFYPARENTWM_NOTIFYSHRG_LONDELAY

TicTac1

tic-tac-toe3-5TicTac13-3XO

3-5TicTac1

3-3TicTac1TicTac1.h//======================================================================// Header file//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling//======================================================================// Returns number of elements#define dim(x) (sizeof(x) / sizeof(x[0]))//----------------------------------------------------------------------// Generic defines and data types//struct decodeUINT { // Structure associates UINT Code; // messages // with a function. LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);}; struct decodeCMD { // Structure associates UINT Code; // menu IDs with a LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.}; //----------------------------------------------------------------------// Function prototypes//HWND InitInstance (HINSTANCE, LPWSTR, int);int TermInstance (HINSTANCE, int); // Window proceduresLRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM); // Message handlersLRESULT DoSizeMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoLButtonDownMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoLButtonUpMain (HWND, UINT, WPARAM, LPARAM);LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM); // Game function prototypesvoid DrawXO (HDC hdc, HPEN hPen, RECT *prect, INT nCell, INT nType);void DrawBoard (HDC hdc, RECT *prect);

TicTac1.cpp//======================================================================// TicTac1 - Simple tic-tac-toe game//// Written for the book Programming Windows CE// Copyright (C) 2003 Douglas Boling////======================================================================#include // For all that Windows stuff#include // Command bar includes#include "tictac1.h" // Program-specific stuff //----------------------------------------------------------------------// Global data//const TCHAR szAppName[] = TEXT ("TicTac1");HINSTANCE hInst; // Program instance handle // State data for gameRECT rectBoard = {0, 0, 0, 0}; // Used to place game board.RECT rectPrompt; // Used to place prompt.BYTE bBoard[9]; // Keeps track of X's and O's.BYTE bTurn = 0; // Keeps track of the turn. // Message dispatch table for MainWindowProcconst struct decodeUINT MainMessages[] = { WM_SIZE, DoSizeMain, WM_PAINT, DoPaintMain, WM_LBUTTONUP, DoLButtonUpMain, WM_DESTROY, DoDestroyMain,}; //======================================================================//// Program entry point//int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwndMain; // Initialize this instance. hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam);}//----------------------------------------------------------------------// InitInstance - Instance initialization//HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; HWND hWnd; // Save program instance handle in global variable. hInst = hInstance; #if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, allow only one instance of the application. hWnd = FindWindow (szAppName, NULL); if (hWnd) { SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01)); return 0; }#endif // Register application main window class. wc.style = 0; // Window style wc.lpfnWndProc = MainWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass (&wc) == 0) return 0; // Create main window. hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName, TEXT ("TicTac1"), WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Return fail code if window not created. if (!IsWindow (hWnd)) return 0; // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd;}//----------------------------------------------------------------------// TermInstance - Program cleanup//int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC;}//======================================================================// Message handling procedures for MainWindow////----------------------------------------------------------------------// MainWndProc - Callback function for application window//LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc(hWnd, wMsg, wParam, lParam);}//----------------------------------------------------------------------// DoSizeMain - Process WM_SIZE message for window.//LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { RECT rect; INT i; // Adjust the size of the client rect to take into account // the command bar height. GetClientRect (hWnd, &rect); // Initialize the board rectangle if not yet initialized. if (rectBoard.right == 0) { // Initialize the board. for (i = 0; i < dim(bBoard); i++) bBoard[i] = 0; } // Define the playing board rect. rectBoard = rect; rectPrompt = rect; // Layout depends on portrait or landscape screen. if (rect.right - rect.left > rect.bottom - rect.top) { rectBoard.left += 20; rectBoard.top += 10; rectBoard.bottom -= 10; rectBoard.right = rectBoard.bottom - rectBoard.top + 10; rectPrompt.left = rectBoard.right + 10; } else { rectBoard.left += 20; rectBoard.right -= 20; rectBoard.top += 10; rectBoard.bottom = rectBoard.right - rectBoard.left + 10; rectPrompt.top = rectBoard.bottom + 10; } return 0;} //----------------------------------------------------------------------// DoPaintMain - Process WM_PAINT message for window.//LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect; HFONT hFont, hOldFont; HDC hdc; GetClientRect (hWnd, &rect); hdc = BeginPaint (hWnd, &ps); // Draw the board. DrawBoard (hdc, &rectBoard); // Write the prompt to the screen. hFont = (HFONT)GetStockObject (SYSTEM_FONT); hOldFont = (HFONT)SelectObject (hdc, hFont); if (bTurn == 0) DrawText (hdc, TEXT (" X's turn"), -1, &rectPrompt, DT_CENTER | DT_VCENTER | DT_SINGLELINE); else DrawText (hdc, TEXT (" O's turn"), -1, &rectPrompt, DT_CENTER | DT_VCENTER | DT_SINGLELINE); SelectObject (hdc, hOldFont); EndPaint (hWnd, &ps); return 0;}//----------------------------------------------------------------------// DoLButtonUpMain - Process WM_LBUTTONUP message for window.//LRESULT DoLButtonUpMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { POINT pt; INT cx, cy, nCell = 0; pt.x = LOWORD (lParam); pt.y = HIWORD (lParam); // See if pen on board. If so, determine which cell. if (PtInRect (&rectBoard, pt)){ // Normalize point to upper left corner of board. pt.x -= rectBoard.left; pt.y -= rectBoard.top; // Compute size of each cell. cx = (rectBoard.right - rectBoard.left)/3; cy = (rectBoard.bottom - rectBoard.top)/3; // Find column. nCell = (pt.x / cx); // Find row. nCell += (pt.y / cy) * 3; // If cell empty, fill it with mark. if (bBoard[nCell] == 0) { if (bTurn) { bBoard[nCell] = 2; bTurn = 0; } else { bBoard[nCell] = 1; bTurn = 1; } InvalidateRect (hWnd, NULL, FALSE); } else { // Inform the user of the filled cell. MessageBeep (0); return 0; } } return 0;}//----------------------------------------------------------------------// DoDestroyMain - Process WM_DESTROY message for window.//LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0;}//======================================================================// Game-specific routines////----------------------------------------------------------------------// DrawXO - Draw a single X or O in a square.//void DrawXO (HDC hdc, HPEN hPen, RECT *prect, INT nCell, INT nType) { POINT pt[2]; INT cx, cy; RECT rect; cx = (prect->right - prect->left)/3; cy = (prect->bottom - prect->top)/3; // Compute the dimensions of the target cell. rect.left = (cx * (nCell % 3) + prect->left) + 10; rect.right = rect.right = rect.left + cx - 20; rect.top = cy * (nCell / 3) + prect->top + 10; rect.bottom = rect.top + cy - 20; // Draw an X ? if (nType == 1) { pt[0].x = rect.left; pt[0].y = rect.top; pt[1].x = rect.right; pt[1].y = rect.bottom; Polyline (hdc, pt, 2); pt[0].x = rect.right; pt[1].x = rect.left; Polyline (hdc, pt, 2); // How about an O ? } else if (nType == 2) { Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom); } return;}//----------------------------------------------------------------------// DrawBoard - Draw the tic-tac-toe board.// VK_MENUvoid DrawBoard (HDC hdc, RECT *prect) { HPEN hPen, hOldPen; POINT pt[2]; LOGPEN lp; INT i, cx, cy; // Create a nice thick pen. lp.lopnStyle = PS_SOLID; lp.lopnWidth.x = 5; lp.lopnWidth.y = 5; lp.lopnColor = RGB (0, 0, 0); hPen = CreatePenIndirect (&lp); hOldPen = (HPEN)SelectObject (hdc, hPen); cx = (prect->right - prect->left)/3; cy = (prect->bottom - prect->top)/3; // Draw lines down. pt[0].x = cx + prect->left; pt[1].x = cx + prect->left; pt[0].y = prect->top; pt[1].y = prect->bottom; Polyline (hdc, pt, 2); pt[0].x += cx; pt[1].x += cx; Polyline (hdc, pt, 2); // Draw lines across. pt[0].x = prect->left; pt[1].x = prect->right; pt[0].y = cy + prect->top; pt[1].y = cy + prect->top; Polyline (hdc, pt, 2); pt[0].y += cy; pt[1].y += cy; Polyline (hdc, pt, 2); // Fill in X's and O's. for (i = 0; i < dim (bBoard); i++) DrawXO (hdc, hPen, &rectBoard, i, bBoard[i]); SelectObject (hdc, hOldPen); DeleteObject (hPen); return;}TicTac3DrawBoard,DrawXODoLbuttonUpMainDoLButtonUpMainWM_LBUTTONUPPtInRectPtInRectBOOL PtInRect(const RECT *lprc, POINT pt);lParamDoSizeMain

DoSizeMainWM_SIZEWindows CEWindowsWM_SIZEWM_CREATElParamCREATESTRUCTWindows CETicTac1TicTac10hot spotsWindows--

4

WindowsXPCEWindowsWindowsWindowsWindows CE

4.1

CreateWindowCreateWindowsExCreateWindowCreateWindowExdwStyleWS_CHILDWindows CEhMenuID

Windows CEWindows CE shellWS_OVERLAPPEDWS_VISIBLEWM_HIBERNATEZWM_HIBERNATEWM_HIBERNATE"Explorer shell"Windows CEWM_HIBERNATE

Z

4.2

WindowsGetParent(HWND hWnd)NULL

GetWindow

HWND GetWindow(HWND hWnd, UINT uCmd);

, GW_CHILDGW_HWNDFIRSTGW_HWNDLASTGW_HWNDNEXTGW_HWNDPREV

GW_CHILDGetWindowZZNULL

GW_HWNDFIRSTGW_HWNDLASTZZtopmostGetWindow

GW_HWNDNEXTGW_HWNDPREVZGetWindowGW_OWNER

EnumWindows

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);

lpEnumFuncEnumWindowslParamGetWindowGetWindowGetWindowEnumWindowsGetWindow

FindWindow

HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName)

FindWindowFindWindowNULL

desktop windowHWND GetDesktopWindow(void);

GetWindowLongSetWindowLong

LONG GetWindowLong(HWND hWnd, int nIndex);

LONG SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong);

RegisterClassWNDCLASScbWndExtraGetWindowLongSetWindowLongWindows CE412cbWndExtraGetWindowLongSetWindowLongnIndex048

GetWindowLongSetWindowLongWindows CE

GWL_STYLE

GWL_EXSTYLE

GWL_WNDPROC

GWL_ID

GWL_USERDATA32

DWL_DLGPROC

DWL_MSGRESULT

DWL_USER 32

Windows CEGWL_HINSTANCEGWL_HWNDPARENTWindows 2000Windows XP

WS_CAPTIONSetWindowPos

SetWindowPosWindowsZ

BOOL SetWindowPos(HWND hWnd,

HWND hWndInsertAfter,

//Z

int X, int Y, int cx, int cy,//

UINT uFlags);

XYcxcyhWndInsertAfterZ4Z

HWND_BOTTOM

HWND_TOP

HWND_TOPMOST

HWND_NOTTOPMOST (nontopmost windows)topmost window

uFlags

SWP_NOMOVE

SWP_NOSIZE

SWP_NOZORDER Z

SWP_NOACTIVATE Z

SWP_DRAWFRAME

SWP_FRAMECHANGED

SWP_SHOWWINDOWSWP_HIDEWINDOWShowWindowSetWindowPosSetWindowPos (hWnd, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);

SetWindowLongEdit

GetWindowLongSetWindowLongCallWindowProcWM_LBUTTONDOWN

// Prototype of subclass procedure

LRESULT CALLBACK SCWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);

// Variable that holds the pointer to the original WndProc

WNDPROC lpfnOldProc = 0;

// Routine that subclasses the requested window.

BOOL SubClassThisWnd (HWND hwndSC)

{

if (lpfnOldProc == 0) {

lpfnOldProc = (WNDPROC)GetWindowLong (hwndSC, GWL_WNDPROC);

return SetWindowLong (hwndSC, GWL_WNDPROC, (DWORD)SCWndProc);

}

return FALSE;

}

// Subclass procedure

LRESULT CALLBACK SCWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

switch (wMsg) {

case WM_LBUTTONDOWN:

MessageBeep(0);

break;

}

return CallWindowProc (lpfnOldProc, hWnd, wMsg, wParam, lParam);

}

SetWindowLongWndProc

4.3 Windows

WindowsWindowsWindows

CreateWindowsCreateWindowExWM_COMMANDID

WM_COMMANDwParamlParamWM_COMMANDwParamwParamIDIDIDlParamIDWM_COMMANDWM_COMMAND

case WM_COMMAND:

WORD idItem, wNotifyCode;

HWND hwndCtl;

// Parse the parameters.

idItem = (WORD) LOWORD (wParam);

wNotifyCode = (WORD) HIWORD(wParam);

hwndCtl = (HWND) lParam;

WM_COMMANDIDWM_COMMAND

IDWindowGetDlgItem

HWND GetDlgItem(HWND hDlg, int nIDDlgItem);

ID

SendDlgItemMessageID

LONG SendDlgItemMessage (HWND hParent, int nIDChild, UINT Msg, WPARAM wParam, LPARAM lParam);

SendMessageSendDlgItemMessageLONG SendMessage (GetDlgItem (hParent, nIDChild), Msg, wParam, lParam);SendMessageGetDlgItem

Button

Edit

List

Combo

Static

Scroll bar

--CtlView

/on/off

WM_COMMANDwParamBN_CLICKED

BS_CHECKBOXBS_3STATEBS_AUTOCHECKBOXBS_AUTO3STATE

BN_CLICKEDBM_SETCHECKwParamBM_SETCHECKwParamBM_GETCHECK

BS_RADIOBUTTONBS_AUTORADIONBUTTON

BOOL CheckRadioButton(

HWND hDlg,

int nIDFirstButton,

int nIDLastButton,

int nIDCheckButton

);

Parameters

hDlg[in] Handle to the dialog box that contains the radio button.

nIDFirstButton[in] Specifies the identifier of the first radio button in the group.

nIDLastButton[in] Specifies the identifier of the last radio button in the group.

nIDCheckButton[in] Specifies the identifier of the radio button to select.

Return ValuesNonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

Z

Windows CEWindows CEPocket PC

BS_RIGHT,BS_LEFT,BS_BOTTOMBS_TOPBS_MULTILINE\nWindows CEBS_ICONBS_BITMAP

BS_OWNERDRAWWM_DRAWITMEwParamIDlParamDRAWITEMSTRUCT

typedef struct tagDRAWITEMSTRUCT

{

UINT CtlType;

UINT CtlID;

//ID

UINT itemID;

UINT itemAction;//

UINT itemState;

//

HWND hwndItem;

HDC hDC;

RECT rcItem;

//

DWORD itemData;

} DRAWITEMSTRUCT;

CtlTypeODT_BUTTONCtlIDwParamIDitemActionitemStatehDCrcItemitemDateNULL

WM_DRAWITEMGDIGetSysColor

DWORD GetSysColor(int nIndex);

RGBCOLOR_BTNFACECOLOR_BTNSHADOW

WindowsEX_MULTILINEWindows

ES_PASSWORD*ES_READONLYES_LOWERCASEES_UPPERCASE

int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );

WM_SETTEXTWM_GETTEXTEM_SETSEL

LB_ADDSTRINGLB_INSERTSTRINGlParamLB_ADDSTRINGLB_INSERTSTRINGLB_FINDLB_GETCURSELLB_GETSELCOUNTLB_GETSELITEMSLB_SETCURSELLB_SETSEL

LB_DIRWindows CEWindowsWindows CELBS_EX_CONSTRINGDATA

Windows CE

CB_ADDSTRINGCB_INSERTSTRINGCB_FINDSTRINGCB_SETEDITSELECTCB_GETEDITSELECTCB_SHOWDROPDOWNCB_GETDROPPEDSTATE

Windows CE--CBS_EX_CONSTSTRINGDATALBS_EX_CONSTSTRINGDATAROM

Windows CE

SS_LEFT

SS_CENTER

SS_RIGHT

SS_LEFTNOWORDWRAP

SS_BITMAP

SS_ICON

SS_NOTIFYWM_COMMANDWindows CESS_CENTERIMAGESS_BITMAPSS_ICONSS_NOPREFIX&

Windows CESS_WHITEFRAMESS_BLACKRECT

WM_VSCROLLWM_HSCROLLWM_COMMANDWM_VSCROLLWM_HSCROLLSB_SETPOSITION

WM_VSCROLLWM_HSCROLLwParamlParam4-1wParamwParamSB_THUMBPOSITIONSB_THUMBTRACKlParam

4-1

4-1

4-1

Codes

Response

For WM_VSCROLL

SB_LINEUP

Program should scroll the screen up one line.

SB_LINEDOWN

Program should scroll the screen down one line.

SB_PAGEUP

Program should scroll the screen up one screen's worth of data.

SB_PAGEDOWN

Program should scroll the screen down one screen's worth of data.

For WM_HSCROLL

SB_LINELEFT

Program should scroll the screen left one character.

SB_LINERIGHT

Program should scroll the screen right one character.

SB_PAGELEFT

Program should scroll the screen left one screen's worth of data.

SB_PAGERIGHT

Program should scroll the screen right one screen's worth of data.

For both WM_VSCROLL and WM_HSCROLL

SB_THUMBTRACK

Programs with enough speed to keep up should update the display with the new scroll position.

SB_THUMBPOSITION

Programs that can't update the display fast enough to keep up with the SB_THUMBTRACK message should update the display with the new scroll position.

SB_ENDSCROLL

This code indicates that the scroll bar has completed the scroll event. No action is required by the program.

SB_TOP

Program should set the display to the top or left end of the data.

SB_BOTTOM

Program should set the display to the bottom or right end of the data.

SB_LINExxxSB_PAGExxx

SB_THUMBPOSITIONSB_THUM