00 用win32函数对CHECKBOX进行消息响应

Post date: 2012/4/5 上午 08:28:30

下面是很久以前从网上找到的一个程序,里面的范例很好,说明了怎么用win32函数创建主窗口后,再用win32函数去响应消息,特别是哪个CHECKBOX的响应方式,估计在网上不好找,贴出来或许对别人也有所帮助。

#include <stdio.h>

#include <Windows.h>

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:WinMain")

const wchar_t szWindowClass[] = {L"MyClassName"};

const wchar_t szTitle[] = {L"MyWindow"};

ATOM MyRegisterClass(HINSTANCE hInstance);

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

HINSTANCE m_hInstance;

HWND hBtn,hBtn2;

HWND hStatic;

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

MSG msg;

HWND hWnd;

//注册窗口类

MyRegisterClass(hInstance);

m_hInstance = hInstance;

//使用已注册的窗口类,创建主窗口

hWnd = ::CreateWindow(szWindowClass,

szTitle,

WS_OVERLAPPEDWINDOW|WS_BORDER,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,

NULL,

NULL,

hInstance,

NULL);

if (!hWnd)

{

return FALSE;

}

//显示主窗口

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

// 先创建主窗口,然后进入消息循环

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

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

//收到WM_QUIT消息,退出

return msg.wParam;

}

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

//窗口类风格

wcex.style = CS_HREDRAW | CS_VREDRAW;

//处理窗口消息的窗口过程,函数指针指向WndProc()

wcex.lpfnWndProc = (WNDPROC)WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

//实例句柄

wcex.hInstance = hInstance;

//图标光标

wcex.hIcon = NULL;

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

//背景刷

wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);

//主菜单

wcex.lpszMenuName =NULL;

//类名称

wcex.lpszClassName = szWindowClass;

wcex.hIconSm = NULL;

//注册窗口类

return RegisterClassEx(&wcex);

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

//处理窗口消息的窗口过程函数

PAINTSTRUCT ps;

HDC hdc;

switch (message)

{

case WM_CREATE:

RECT rect;

GetClientRect(hWnd, &rect);

CreateWindow(L"EDIT",

NULL,

WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,

0,0,40,rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

hBtn = CreateWindow(L"BUTTON",

TEXT("^O^"),

WS_CHILD | WS_VISIBLE | WS_BORDER,

40,0,40,20,//rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

CreateWindow(L"COMBOBOX",

NULL,

WS_CHILD | WS_VISIBLE,

80,0,40,20,//rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

CreateWindow(L"LISTBOX",

NULL,

WS_CHILD | WS_VISIBLE,

120,0,40,20,//rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

hStatic = CreateWindow(L"STATIC",

NULL,

WS_CHILD | WS_VISIBLE | SS_NOTIFY,

160,0,40,20,//rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

hBtn2 = CreateWindow(L"BUTTON",

NULL,

WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,

210,0,40,20,//,rect.bottom,

hWnd,

NULL,

m_hInstance,

NULL);

case WM_SHOWWINDOW:

case WM_KEYDOWN:

return ::DefWindowProc(hWnd, message, wParam, lParam);

case WM_NOTIFY:

if (HIWORD(wParam) == BN_CLICKED)

{

MessageBox(NULL,_T("通知消息"),

L"hi",

MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION);

}

case WM_COMMAND:

//控件通知消息,以命令消息的形式发送

if (HIWORD(wParam) == BN_CLICKED && hBtn == (HWND)lParam)

{

MessageBox(NULL,L"BUTTON was clicked!",L"Hi!",MB_ICONERROR | MB_ABORTRETRYIGNORE);

SetWindowText(hBtn ,L"Hi !");

}

//CheckBox

if (HIWORD(wParam) == BN_CLICKED && hBtn2 == (HWND)lParam)

{

LRESULT r = SendMessage(hBtn2,BM_GETCHECK,0,0);

if(r==BST_CHECKED)//被选中

{

MessageBox(NULL,L"CheckBox is CHECKED!",L"Hi!",MB_OK);

}

else if(r==BST_UNCHECKED)//不被选中

{

MessageBox(NULL,L"CheckBox is UNCHECKED!",L"Hi!",MB_OK);

}

SetWindowText(hBtn ,L"Hi !");

}

if (HIWORD(wParam) == STN_CLICKED && hStatic == (HWND)lParam)

{

MessageBox(NULL,_T("STATIC 通知消息"),L"hi",MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION);

SetWindowText((HWND)lParam ,L"Hi !");

} //.........

return ::DefWindowProc(hWnd, message, wParam, lParam);

//

case WM_LBUTTONDOWN:

int ScreenWidth;

int ScreenLength;

RECT rcWindow;

::GetWindowRect(hWnd,&rcWindow); ScreenWidth=GetSystemMetrics(SM_CXSCREEN);

ScreenLength=GetSystemMetrics(SM_CYSCREEN);

//鼠标单击,设置窗口位置

::SetWindowPos(hWnd,HWND_TOP,ScreenWidth/4, ScreenWidth/4,ScreenLength/2,ScreenLength/2,SWP_SHOWWINDOW);

Sleep(300);

::SetWindowPos(hWnd,HWND_TOP,ScreenWidth/3, ScreenWidth/3,ScreenLength/3,ScreenLength/3,SWP_SHOWWINDOW);

Sleep(100);

::SetWindowPos(hWnd,HWND_TOP,ScreenWidth/4, ScreenWidth/4,ScreenLength/2,ScreenLength/2,SWP_SHOWWINDOW);

Sleep(100);

::SetWindowPos(hWnd,HWND_TOP,rcWindow.left,rcWindow.top, rcWindow.right-rcWindow.left,rcWindow.bottom-rcWindow.top, SWP_SHOWWINDOW);

return ::DefWindowProc(hWnd, message, wParam, lParam);

case WM_RBUTTONDOWN:

//单击鼠标右键,隐藏、显示窗口

ShowWindow(hWnd,SW_HIDE);

Sleep(300);

ShowWindow(hWnd,SW_SHOW);

return ::DefWindowProc(hWnd, message, wParam, lParam); case WM_PAINT:

//绘制窗口

hdc = BeginPaint(hWnd, &ps);

RECT rt;

GetClientRect(hWnd, &rt);

::SetBkColor(hdc,::GetSysColor(COLOR_BACKGROUND));

::SetTextColor(hdc,RGB(255,255,255)); DrawText(hdc, L"My window created by win32 API",-1,&rt, DT_CENTER);

EndPaint(hWnd, &ps);

return ::DefWindowProc(hWnd, message, wParam, lParam);

break;

case WM_DESTROY:

//主窗口关闭时,给UI线程消息循环发送WM_QUIT消息,线程退出代码为0

PostQuitMessage(0);

break;

default: //调用缺省的消息处理过程处理当前消息

return ::DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}