Win32窗口控件的创建与响应

1、按钮(标题)文本动态变化,显示该按钮被点击的次数,每点击一次,次数加1;
2、动态创建编辑框控件;
3、点击按钮时,弹出提示框,提示框的内容是编辑框中的字符串;

#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <tchar.h>
#include <CommCtrl.h> //WC_BUTTON的头文件
static TCHAR szWindowClass[] = _T("WindowClass");
static TCHAR szTitle[] = _T("标题");
UINT IDC_BUTTON_1 = 100;
UINT IDC_EDIT_1 = 101;
LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch (uMsg)
    {
    
    case WM_CREATE://窗口消息——完成创造后
    {
                                        //窗口控件按钮        //窗口样式                    //按键样式
        HWND hBuutonWnd = CreateWindow(WC_BUTTON, _T("按钮1"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            100, 100, 200, 25, hWnd, (HMENU)IDC_BUTTON_1, NULL, NULL);

        HWND hEditWnd = CreateWindow(WC_EDIT, _T("编辑框1"), WS_CHILDWINDOW
                                        //编辑框样式
            | WS_BORDER | WS_VISIBLE  | ES_AUTOHSCROLL,
            0, 0, 200, 25, hWnd, (HMENU)IDC_EDIT_1, NULL, NULL);
    }
        break;
    case WM_COMMAND: //窗口消息——指挥某按键后
    {
        UINT nCtrlID = LOWORD(wParam);    //获取被指挥的ID
        UINT nCode = HIWORD(wParam);    //获取被指挥消息
        if (nCode == BN_CLICKED)    //比较被指挥消息
        {
            if (nCtrlID == IDC_BUTTON_1) //比较被指挥ID
            {
                static int nButton1_Times = 0;
                TCHAR szButton1Times[128] = { 0 };
                _stprintf(szButton1Times, _T("按钮1被点击了 %d 次!"), ++nButton1_Times);    //格式化字符串
                //MessageBox(hWnd, szButton1Times, _T("Tip"), MB_OK);
                //设置窗口文本//获取句柄//父窗口句柄,子窗口ID//被格式化的字符串
                SetWindowText(GetDlgItem(hWnd, nCtrlID), szButton1Times); 

                TCHAR szEditText[256] = { 0 };
                //获取控件文本//父窗口句柄,子窗口ID,接收文本的缓冲区,长度
                GetDlgItemText(hWnd, IDC_EDIT_1, szEditText, 256);
                MessageBox(hWnd, szEditText, _T("Tip"), MB_OK); 
            }
        }
    }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
        break;
    }
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
    WNDCLASSEX wcex = { 0 };
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.lpfnWndProc = WindowProc;
    wcex.lpszClassName = szWindowClass;
    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, _T("注册窗口类失败"), _T("Tip"), NULL);
        return 1;
    }
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 300,
        NULL, NULL, hInstance, NULL
    );
    if (!hWnd)
    {
        MessageBox(NULL, _T("创建窗口失败"), _T("Tip"), NULL);
        return 1;
    }
    ShowWindow(hWnd, SW_SHOW);
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

 

Win32窗口控件的创建与响应

上一篇:C#基础语法-以Cpp为参照


下一篇:windows下安装rocketmq采坑全记录