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

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-16 14:25:22

请参考《windows程序设计》- 子窗口控件 - 清单控件方面的样本, 此程序为模仿。
源代码:listDemo.c

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    static TCHAR szAppName[] = TEXT("listDemo");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    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 HWND hwndList, hwndStatic;
    static int cxChar, cyChar;
    TCHAR* buffer[] = {
        "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "ten"
    };
    int i, iIndex, iLength;
    TCHAR* text;

    switch(message) {
    case WM_CREATE:
        hwndList = CreateWindow(TEXT("listbox"),
            NULL,
            WS_CHILD | WS_VISIBLE | LBS_STANDARD,
            0, 0, 0, 0,
            hwnd,
            (HMENU)1,
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);
        hwndStatic = CreateWindow(TEXT("static"),
            NULL,
            WS_CHILD | WS_VISIBLE | SS_LEFT,
            0, 0, 0, 0,
            hwnd,
            (HMENU)2,
            ((LPCREATESTRUCT)lParam)->hInstance,
            NULL);
        cxChar = LOWORD(GetDialogBaseUnits());
        cyChar = HIWORD(GetDialogBaseUnits());
        SendMessage(hwndList, WM_SETREDRAW, FALSE, 0);
        for(i = 0; i < 10; i++) {
            SendMessage(hwndList, LB_ADDSTRING, 0, buffer[i]);
        }
        SendMessage(hwndList, WM_SETREDRAW, TRUE, 0);
        return 0;
    case WM_SIZE:
        MoveWindow(hwndStatic, cxChar, cyChar, cxChar * 10, cyChar, TRUE);
        MoveWindow(hwndList, cxChar, 4 * cyChar, cxChar * 6, cyChar * 5, TRUE);
        return 0;

    case WM_SETFOCUS:
        SetFocus(hwndList);
        return 0;

    case WM_COMMAND:
        if(LOWORD(wParam) == 1 && HIWORD(wParam) == LBN_SELCHANGE) {
            iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
            iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1;
            text = calloc(iLength, sizeof(TCHAR));
            SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)text);
            SetWindowText(hwndStatic, text);
                    free(text);
        }

        return 0;

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


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