Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525318
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-15 17:08:04

最重要的东西是写, 看是其次的,哈哈。运行结果:

#include <windows.h>

HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    static TCHAR szAppName[] = TEXT("buttonDemo");
    WNDCLASS wndclass;
    HWND hwnd;
    MSG msg;
    hInst = hInstance;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxClient, cyClient, cxChar, cyChar;
    static HWND hwndButton1, hwndButton2, hwndCheckBox, hwndRadioButton,
        hwndAutoCheckBox, hwndAutoRadioButton;
    HDC hdc;
    PAINTSTRUCT ps;
    HBRUSH hBrush;
    RECT rect;

    switch(message) {
    case WM_CREATE:
        cxChar = LOWORD(GetDialogBaseUnits());
        cyChar = HIWORD(GetDialogBaseUnits());

        hwndButton1 = CreateWindow(TEXT("button"),
            TEXT("button1"),
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            0, 0, 0, 0,
            hwnd,
            (HMENU)1,
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);
        hwndButton2 = CreateWindow(TEXT("button"),
            TEXT("button2"),
            WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
            0, 0, 0, 0,
            hwnd,
            (HMENU)2,
            ((LPCREATESTRUCT)lParam)->hInstance,
            NULL);
        hwndCheckBox = CreateWindow(TEXT("button"),
            TEXT("CheckBox"),
            WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
            0, 0, 0, 0,
            hwnd,
            (HMENU)3,
            hInst,
            NULL);
        hwndRadioButton = CreateWindow(TEXT("button"),
            TEXT("RadioButton"),
            WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
            0, 0, 0, 0,
            hwnd,
            (HMENU)4,
            hInst,
            NULL);
        hwndAutoCheckBox = CreateWindow(TEXT("button"),
            TEXT("AutoCheckBox"),
            WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
            0, 0, 0, 0,
            hwnd,
            (HMENU)5,
            hInst,
            NULL);
        hwndAutoRadioButton = CreateWindow(TEXT("button"),
            TEXT("AutoRadioButton"),
            WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
            0, 0, 0, 0,
            hwnd,
            (HMENU)6,
            hInst,
            NULL);
        return 0;

    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        MoveWindow(hwndButton1, cxChar, cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
        MoveWindow(hwndButton2, cxChar, 3 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
        MoveWindow(hwndCheckBox, cxChar, 5 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
        MoveWindow(hwndRadioButton, cxChar, 7 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
        MoveWindow(hwndAutoCheckBox, cxChar, cyChar * 9, 20 * cxChar, cyChar * 7 / 4, TRUE);
        MoveWindow(hwndAutoRadioButton, cxChar, cyChar * 11, 20 * cxChar, cyChar * 7 / 4, TRUE);
        return 0;

    case WM_COMMAND:
        switch(LOWORD(wParam)) {
        case 1:
            MessageBeep(-1);
            InvalidateRect(hwnd, NULL, TRUE);
            break;

        case 2:
            MessageBeep(MB_ICONEXCLAMATION);
            InvalidateRect(hwnd, NULL, TRUE);
            break;
            
        case 3:
            MessageBeep(MB_ICONERROR);
            InvalidateRect(hwnd, NULL, TRUE);
            break;

        case 4:
            MessageBeep(MB_ICONINFORMATION);
            InvalidateRect(hwnd, NULL, TRUE);
            break;

        case 5:
            MessageBeep(MB_ICONSTOP);
            InvalidateRect(hwnd, NULL, TRUE);
            break;

        case 6:
            MessageBeep(MB_ICONWARNING);
            InvalidateRect(hwnd, NULL, TRUE);
            break;

        default:
            break;
        }
        return 0;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            hBrush = CreateSolidBrush(RGB(rand() % 255, rand() % 255, rand() % 255));
            GetClientRect(hwnd, &rect);
            FillRect(hdc, &rect, hBrush);
            EndPaint(hwnd, &ps);
            DeleteObject(hBrush);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(738) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~