Win32 SDK Gui编程系列之--Win32 API通用控件

要使用普通控件的话,包含语句

#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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值