#include "windows.h"
#include "resource.h"
#include "commctrl.h"
#include "main.h"
// Global variables:
static TCHAR szAppName[] = TEXT("Glowworm"); // title bar text
static TCHAR szClassName[] = TEXT("GlowwormClass"); // main window class name
HINSTANCE g_hInst;
INT g_CheckOnce = 1;
//主窗口过程函数的消息映射表
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
//WM_HIBERNATE, DoHibernateMain, // only for wince
//WM_ACTIVATE, DoActivateMain,
WM_COMMAND, DoCommandMain,
WM_DESTROY, DoDestroyMain,
};
//------------------------------------------------------------------------------------------------------------
// WinMain -主程序入口
//
int WINAPI WinMain(HINSTANCE hInstance, //当前实例句柄
HINSTANCE hPrevInstance, // 总是置空
LPTSTR lpCmdLine, //运行程序的命令行
int nCmdShow) //程序窗口显示模式
{
MSG msg;
if (! InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DFDTEFFG));
//应用程序消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
if (! TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//实例清除
return (int) msg.wParam;
}
//-------------------------------------------------------------------
// MyRegisterClass 应用程序初始化函数
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW; //窗口显示模式
wc.lpfnWndProc = WndProc; //窗口过程函数
wc.cbClsExtra = 0; //调用RegisterClass时不提供额外的空间
wc.cbWndExtra = 0; //调用CreateClass时不提供额外的空间
wc.hInstance = hInstance; //窗口实例句柄
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GLOWWORM)); //图标成员句柄
wc.hCursor = 0;//LoadCursor(hInstance, IDC_ARROW); //鼠标成员句柄赋值
wc.hbrBackground= (HBRUSH) GetStockObject(LTGRAY_BRUSH); //背景成员句柄赋值
wc.lpszMenuName = NULL; // 指向菜单名称的长整形之争为空
wc.lpszClassName= szWindowClass; //指向类名的长整形指针指向数组szClassName
// 注册主窗口类
return RegisterClass(&wc);
}
//-------------------------------------------------------------
//InitInstance -初始化实例
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
g_hInst = hInstance;
hWnd = FindWindow(szClassName, szAppName);
if (hWnd)
{
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
if (! MyRegisterClass(hInstance, szClassName))
{
return FALSE;
}
hWnd = CreateWindow(szClassName, //窗口类名
szAppName, //窗口标题名
//WS_CAPTION | WS_SYSMENU, //窗口样式
WS_VISIBLE,
CW_USEDEFAULT, // 窗口X轴位置缺省,即使用默认值
CW_USEDEFAULT, // 窗口Y轴位置缺省,即使用默认值
CW_USEDEFAULT, // 窗口宽度缺省
CW_USEDEFAULT, // 窗口高度缺省
NULL, //窗口没有父窗口
NULL,// 窗口菜单栏为空
hInstance, //窗口实例句柄
NULL);
if (! hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//---------------------------------------------------------
//WndProc -窗口处理函数
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
INT i;
// 搜索消息列表,如果有对应的消息处理函数则调用这个函数
for (i=0; i<dim(MainMessages); i++)
{
if (message == MainMessages[i].Code)
{
return (*MainMessages[i].Func)(hWnd, message, wParam, lParam);
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//--------------------------------------------------
// DoCreateMain 处理窗口建立 (WM_CREATE) 消息的函数
//
LRESULT DoCreateMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/*
HWND hwndCB;
// 建立命令条
hwndCB = CommandBar_Create(g_hInst, hWnd, IDC_CMDBAR);
// 添加退出按钮到命令条上
CommandBar_AddAdornments(hwndCB, 0, 0);
*/
CreateControl(hWnd);
return 0;
}
//-----------------------------------------------------
// DoPaintMain 处理窗口重画(WM_PAINT_消息函数
//
LRESULT DoPaintMain(HWND hWnd, UINT message, 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("Hi Glowworm"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
return 0;
}
//------------------------------------------------------
// DoCommandMain 处理控件消息函数
//
LRESULT DoCommandMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case IDC_BUTTON:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
case IDC_BUTTON_SUBMIT:
if (HIWORD(wParam) == BN_CLICKED && g_CheckOnce == 1)
{
AddItemsInListBox(hWnd);
g_CheckOnce = 2;
}
break;
case IDC_BUTTON_RESET:
if (HIWORD(wParam) == BN_CLICKED)
{
for (INT i=0; i<4; i++)
{
SendMessage(GetDlgItem(hWnd, (IDC_RADIO+i)), BM_SETCHECK, BST_UNCHECKED, 0);
SendMessage(GetDlgItem(hWnd, (IDC_CHECKBOX+i)), BM_SETCHECK, BST_UNCHECKED, 0);
}
SendMessage(GetDlgItem(hWnd, IDC_EDIT), WM_SETTEXT, 0, NULL);
SendMessage(GetDlgItem(hWnd, IDC_COMBOBOX), CB_SETCURSEL, 0, 0);
SendMessage(GetDlgItem(hWnd, IDC_LISTBOX), LB_RESETCONTENT, 0, 0);
g_CheckOnce = 1;
}
break;
default:
DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
/*
//----------------------------------------------------------------------
// DoHibernateMain - 处理窗口挂起消息(WM_HIBERNATE)的函数,这是WINCE独有的消息,目的//是将内存的使用量将到最小.
//
LRESULT DoHibernateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
// 如果窗口不是活动的,则取消命令条,释放内存
if (GetActiveWindow () != hWnd)
CommandBar_Destroy (GetDlgItem (hWnd, IDC_CMDBAR));
return 0;
}
*/
//--------------------------------------------------------
// DoActivateMain 处理窗口激活(WM_ACTIVATE)消息
//
LRESULT DoActivateMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hwndCB;
if ((LOWORD(wParam) != WM_ACTIVATE) && (GetDlgItem(hWnd, IDC_CMDBAR) == 0))
{
// 建立命令条
hwndCB = CommandBar_Create(g_hInst, hWnd, IDC_CMDBAR);
//添加退出按钮在命令条上
CommandBar_AddAdornments(hwndCB, 0, 0);
}
return 0;
}
//-------------------------------------------------------
// DoDestroyMain 处理窗口销毁(WM_DESTROY)消息函数
//
LRESULT DoDestroyMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//CommandBar_Destroy (GetDlgItem (hWnd, IDC_CMDBAR));
PostQuitMessage(0);
return 0;
}
//----------------------------------------------------------
//CreateControl -
//
void CreateControl(HWND hWnd)
{
// Label
CreateWidget(hWnd, TEXT("Static"), TEXT("Static Label"), WS_CHILD | WS_VISIBLE |SS_LEFT, NULL, 0, 0, 100, 20);
// Button
CreateWidget(hWnd, TEXT("Button"), TEXT("Button"), WS_CHILD | WS_VISIBLE | SS_LEFT, (HMENU)IDC_BUTTON, 0, 20, 100, 20);
// Group Box
CreateWidget(hWnd, TEXT("Button"), TEXT("Group"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, NULL, 0, 40, 100, 20);
// radio button group
CreateRadioButtonGroup(hWnd, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON, IDC_RADIO, 60);
// Push button
CreateWidget(hWnd, TEXT("Button"), TEXT("Submit"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
(HMENU)IDC_BUTTON_SUBMIT, 0, 80, 100, 20);
// check box group
CreateCheckBoxGroup(hWnd, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP, IDC_CHECKBOX, 100);
// ComboBox
TCHAR *comboxitem[] = {TEXT("First"), TEXT("Second"), TEXT("Third"), TEXT("Forth"), TEXT("Fifth")};
INT i;
CreateWidget(hWnd, TEXT("ComboBox"), TEXT(""), CBS_DROPDOWNLIST | WS_VSCROLL| WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
(HMENU)IDC_COMBOBOX, 0, 120, 100, 80);
//Add items
for (i=0; i<5; i++)
{
SendMessage(GetDlgIt