Win32 Api 学习记录-Ocrosoft
浙江财经大学
信工学院ACM集训队

Win32 Api 学习记录

本文由 Ocrosoft 于 2018-05-15 9:18:25 发表

摘要:预定义控件列表框(listbox)的使用,获取系统环境变量(GetEnvironmentStrings)

1.1.源码

#include <windows.h>

#define ID_LIST 1
#define ID_TEXT 2

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

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

    hInstance = 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 = (HBRUSH)GetStockObject(GRAY_BRUSH);
    wndclass.lpszMenuName = szAppName;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, TEXT("Environment List Box"), 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;
}

void FillListBox(HWND hwndList)
{
    int iLength;
    TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName;

    pVarBlock = GetEnvironmentStrings(); // 获取到的是一个指针,如下
    //变量名1=变量值1\0
    //变量名2=变量值2\0
    //...
    //变量名N=变量值N\0\0

    while (* pVarBlock)
    {
        if (* pVarBlock != '=') // 就是实现了一个split("=")[0]的功能,并且将环境变量名称填到列表中
        {
            pVarBeg = pVarBlock;
            while(* pVarBlock++ != '=');
            pVarEnd = pVarBlock - 1;
            iLength = pVarEnd - pVarBeg;

            pVarName = calloc(iLength + 1, sizeof(TCHAR));
            CopyMemory(pVarName, pVarBeg, iLength * sizeof(TCHAR));
            pVarName[iLength] = '\0';

            SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)pVarName);
            free(pVarName);
        }
        while(* pVarBlock++ != '\0'); // 跳到\0的下一个字符,如果还是\0则外面的while结束
    }
    FreeEnvironmentStrings(pVarBlock);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndList, hwndText;
    int iIndex, iLength, cxChar, cyChar;
    TCHAR * pVarName, * pVarValue;

    int tmp;

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

        // 列表框
        hwndList = CreateWindow(TEXT("listbox"), NULL,
                                WS_CHILD | WS_VISIBLE | LBS_STANDARD,
                                cxChar, cyChar * 3,
                                cxChar * 16 + GetSystemMetrics(SM_CXVSCROLL),
                                cyChar * 5,
                                hwnd, (HMENU)ID_LIST,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL);

        // 静态窗口,宽度是屏幕宽度(为了方便,否则要处理WM_SIZE)
        hwndText = CreateWindow(TEXT("static"), NULL,
                                WS_CHILD | WS_VISIBLE | SS_LEFT,
                                cxChar, cyChar,
                                GetSystemMetrics(SM_CXSCREEN), cyChar,
                                hwnd, (HMENU)ID_TEXT,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL);

        FillListBox(hwndList);
        return 0;

    case WM_SETFOCUS:
        SetFocus(hwndList);
        return 0;

    case WM_COMMAND:
        if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_SELCHANGE) // 选择项变化
        {
            iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0); // 获取当前选择项的下标
            iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1; // 获取当前选择项文本长度
            pVarName = calloc(iLength, sizeof(TCHAR)); // 申请空间
            SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)pVarName); // 获取选择项文本

            iLength = GetEnvironmentVariable(pVarName, NULL, 0); // 根据环境变量名称获取值,参数二NULL,函数返回值是长度
            pVarValue = calloc(iLength, sizeof(TCHAR)); // 申请空间
            GetEnvironmentVariable(pVarName, pVarValue, iLength); // 获取值,到缓冲区

            SetWindowText(hwndText, pVarValue); // 显示在子窗口
            free(pVarName);
            free(pVarValue);
        }
        return 0;

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

欢迎转载,请保留出处与链接。Ocrosoft » Win32 Api 学习记录

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址