//======================================================================
// Header file:helloce.h
//======================================================================
// 返回元素的数量,主要用于搜索消息列表
#define dim(x)(sizeof(x) / sizeof(x[0]))
//----------------------------------------------------------------------
//数据类型定义
//
structdecodeUINT { //消息和消息函数的关联结构
UINT Code;
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM); //这里用到了函数指针
};
struct decodeCMD{ //菜单和处理函数的关联结构
UINT Code;
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); //这里用到了函数指针
};
//----------------------------------------------------------------------
#define IDC_CMDBAR 1 // 命令条ID
//----------------------------------------------------------------------
// 函数原型
//
int InitApp(HINSTANCE); //初始化应用函数原型
HWNDInitInstance (HINSTANCE, LPWSTR, int); //初始化实例函数原型
int TermInstance(HINSTANCE, int); //实例终止函数原型
// 窗口处理函数原型
LRESULT CALLBACKMainWndProc (HWND, UINT, WPARAM, LPARAM);
// 消息句柄
LRESULTDoCreateMain (HWND, UINT, WPARAM, LPARAM);
LRESULTDoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULTDoHibernateMain (HWND, UINT, WPARAM, LPARAM);
LRESULTDoActivateMain (HWND, UINT, WPARAM, LPARAM);
LRESULTDoDestroyMain (HWND, UINT, WPARAM, LPARAM);
//======================================================================
// HelloCE – helloce.c
//======================================================================
#include<windows.h>
#include<commctrl.h>
#include"helloce.h"
//----------------------------------------------------------------------
// 全局数据
//
const TCHARszAppName[] = TEXT ("HelloCE");
HINSTANCEhInst; // 程序的实例句柄
//主窗口过程函数的消息映射表用到decodeUINT结构
const structdecodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
WM_HIBERNATE, DoHibernateMain,
WM_ACTIVATE, DoActivateMain,
WM_DESTROY, DoDestroyMain,
};
//======================================================================
// 程序的入口
int WINAPIWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, intnCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
// 初始应用
rc = InitApp (hInstance);
if (rc) return rc;
// 初始化实例
hwndMain = InitInstance (hInstance,lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// 应用程序消息循环
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
//实例清除
return TermInstance (hInstance,msg.wParam);
}
//----------------------------------------------------------------------
// 应用程序初始化函数
//
int InitApp(HINSTANCE hInstance) {
WNDCLASS wc;
//注册应用程序的主窗口类
wc.style = 0; // 窗口样式
wc.lpfnWndProc = MainWndProc; // 回调函数
wc.cbClsExtra = 0; // 扩展的类数据
wc.cbWndExtra = 0; // 扩展的窗口数据
wc.hInstance = hInstance; //实例句柄
wc.hIcon = NULL, // 图标
wc.hCursor = NULL; // 鼠标
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL; //菜单
wc.lpszClassName = szAppName; //窗口类的名字
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
//初始化实例
//
HWNDInitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine,
int nCmdShow) {
HWND hWnd;
// 存储程序实例句柄到全局变量
hInst = hInstance;
// 建立主窗口
hWnd = CreateWindow (szAppName, // 窗口类
TEXT("你好蜥蜴"), //窗口标题
WS_VISIBLE, //样式
CW_USEDEFAULT, // x坐标
CW_USEDEFAULT, // y 坐标
CW_USEDEFAULT, // 初始宽度
CW_USEDEFAULT, // 初始高度
NULL, // 父窗口
NULL, //菜单,必须为NULL,WINCE窗口不支持菜单。
hInstance, // 实例
NULL); //建立参数的指针,用于WM_CRATE消息期间。
// 如果不能建立主窗口返回失败
if (!IsWindow (hWnd)) return 0;
// 显示和更新窗口函数
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance –程序清除
//
int TermInstance(HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// 下面是主窗口的消息处理函数
//
//----------------------------------------------------------------------
// MainWndProc – 主过程函数,这是一个回调函数
//
LRESULT CALLBACKMainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//搜索消息列表,如果编写了对应的函数来处理这个消息则调用这个函数
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 – 处理窗口建立(WM_CREATE)消息的函数.
//
LRESULTDoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndCB;
// 建立命令条.
hwndCB = CommandBar_Create (hInst, hWnd,IDC_CMDBAR);
// 添加退出按钮到命令条上
CommandBar_AddAdornments (hwndCB, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain – 处理窗口重画(WM_PAINT)消息的函数
//
LRESULTDoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
// 调整客户区域的大小并考虑命令条的高度
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem(hWnd, IDC_CMDBAR));
hdc = BeginPaint (hWnd, &ps);
DrawText (hdc, TEXT ("你好晕倒的蜥蜴!"), -1, &rect, //被改成了中文
DT_CENTER | DT_VCENTER |DT_SINGLELINE);
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoHibernateMain – 处理窗口挂起消息(WM_HIBERNATE)的函数,这是WINCE独有的消息,目的//是将内存的使用量将到最小.
//
LRESULTDoHibernateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
// 如果窗口不是活动的,则取消命令条,释放内存
if (GetActiveWindow () != hWnd)
CommandBar_Destroy (GetDlgItem (hWnd,IDC_CMDBAR));
return 0;
}
//----------------------------------------------------------------------
// DoActivateMain – 处理窗口激活(WM_ACTIVATE)消息的函数
//
LRESULTDoActivateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndCB;
// 如果窗口正处在活动状态而没有命令条则建立它
if ((LOWORD (wParam) != WA_INACTIVE)&&
(GetDlgItem (hWnd, IDC_CMDBAR) == 0)) {
// 建立命令条
hwndCB = CommandBar_Create (hInst,hWnd, IDC_CMDBAR);
// 添加退出按钮到命令条
CommandBar_AddAdornments (hwndCB, 0,0);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain – 处理窗口销毁(WM_DESTROY)消息函数.
//
LRESULTDoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
// tmpwin32.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TMPWIN32, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TMPWIN32);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TMPWIN32);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TMPWIN32;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}