要使用普通控件的话,包含语句
#include <commctrl.h>
通过追加和初始化
InitCommonControls();
是必要的。 也可以通过InitCommonControlsEx函数单独注册使用的类。 另外,需要导入comctl32.dll。
1. 标签、控件
只显示标签、控件的程序tabctrl.c和根据按下的标签进行显示的程序tabcontr0l.c和各自的执行结果如下所示。
Tabctrl01.c
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#define WND_CLASS_NAME "My_Window"
HWND hTab;
char* strText[] = { "Tokyo", "Yokohama", "Nagoya", "Kyoto", "Osaka" };
void onCreate(HWND hWnd, LPARAM lParam) {
int i;
int Num = sizeof(strText) / sizeof(char*);
CREATESTRUCT* lp = (CREATESTRUCT*)lParam;
TC_ITEM tc_item;
hTab = CreateWindowEx(0, WC_TABCONTROLA, NULL,
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, 10, 10, hWnd, (HMENU)0x10, lp->hInstance, NULL);
tc_item.mask = TCIF_TEXT;
for (i = 0; i < Num; i++) {
tc_item.pszText = strText[i];
SendMessage(hTab, TCM_INSERTITEM, i, (LPARAM)&tc_item);
}
}
void onSize(HWND hWnd, WPARAM wp, LPARAM lp) {
MoveWindow(hTab, 0, 0, lp&0xFFFF, 30, TRUE);
}
void onNotify(HWND hWnd, LPARAM lParam) {
NMHDR *lp = (NMHDR *)lParam;
if (lp->code == NM_CLICK) // TCN_SELCHANGE
SendMessage(hTab, TCM_GETCURSEL, 0, 0);
InvalidateRect(hWnd, NULL, TRUE);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg) {
case WM_CREATE:
onCreate(hwnd, lParam);
return 0;
case WM_SIZE:
onSize(hwnd, wParam, lParam);
return 0;
case WM_NOTIFY:
onNotify(hwnd, lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
WNDCLASS wcl;
MSG msg;
memset(&wcl, 0, sizeof(wcl));
wcl.hInstance = hInstance;
wcl.lpszClassName = WND_CLASS_NAME;
wcl.lpfnWndProc = WindowProc;
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
if (!RegisterClass(&wcl)) return FALSE;
hWnd = CreateWindowEx(0, WND_CLASS_NAME, "tabctrl",
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 10, 10, 400, 100, NULL, NULL, hInstance, NULL);
if(!hWnd) return FALSE;
while (GetMessage(&msg,NULL,0,0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Tabcontrol01.c
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#define WND_CLASS_NAME "My_Window"
HWND hTab;
int ix = -1;
char* strText[] = { "USD", "EUR", "AUD", "NZD", "GBP", "CAD", "CHF", "HKD", "平均" };
int Num = 9;
void onPaint(HWND hWnd, HDC hDc) {
if (ix >= 0) TextOut(hDc, 0, 30, strText[ix], strlen(strText[ix]));
}
void onSize(HWND hWnd, WPARAM wp, LPARAM lp) {
MoveWindow(hTab, 0, 0, lp&0xFFFF, 25, TRUE);
}
void onNotify(HWND hWnd, LPARAM lParam) {
NMHDR *lp = (NMHDR *)lParam;
if (lp->code == TCN_SELCHANGE) {
ix = SendMessage(hTab, TCM_GETCURSEL, 0, 0);
InvalidateRect(hWnd, NULL, TRUE);
}
}
void onCreate(HWND hWnd, LPARAM lParam) {
int i;
int Num = sizeof(strText) / sizeof(char*);
CREATESTRUCT* lp = (CREATESTRUCT*)lParam;
TC_ITEM tc_item;
hTab = CreateWindowEx(0, WC_TABCONTROLA, NULL,
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, 10, 10, hWnd, (HMENU)0x10, lp->hInstance, NULL);
tc_item.mask = TCIF_TEXT;
for (i = 0; i < Num; i++) {
tc_item.pszText = strText[i];
SendMessage(hTab, TCM_INSERTITEM, i, (LPARAM)&tc_item);
}
SendMessage(hTab, TCM_SETITEMSIZE, 0, (23<<16) + 23);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg) {
case WM_CREATE:
onCreate(hwnd, lParam);
return 0;
case WM_SIZE:
onSize(hwnd, wParam, lParam);
return 0;
case WM_NOTIFY:
onNotify(hwnd, lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
onPaint(hwnd, hdc);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(h