C++ 在Win32桌面应用程序上创建按钮时出错

C++ 在Win32桌面应用程序上创建按钮时出错,c++,user-interface,winapi,button,desktop,C++,User Interface,Winapi,Button,Desktop,我正试图在Windows32桌面应用程序的主窗口上创建一个按钮,但当我运行该按钮时,该窗口显示“Hello,Windows Desktop!”但该按钮不显示,我不确定原因。我认为问题出在“HWND hwndButton=CreateWindow”上,但我不确定问题是什么,或者我可能遗漏了什么。我的代码如下所示: #include <windows.h> #include <stdlib.h> #include <string.h> #include <t

我正试图在Windows32桌面应用程序的主窗口上创建一个按钮,但当我运行该按钮时,该窗口显示“Hello,Windows Desktop!”但该按钮不显示,我不确定原因。我认为问题出在“HWND hwndButton=CreateWindow”上,但我不确定问题是什么,或者我可能遗漏了什么。我的代码如下所示:

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

#define IDC_BUTTON                  3456

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("CD_ROM_READER_APP");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("CD Rom Reader App");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int CALLBACK WinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPSTR     lpCmdLine,
    _In_ int       nCmdShow
)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("CD Rom Reader App"),
            NULL);

        return 1;
    }

    // Store instance handle in our global variable
    hInst = hInstance;

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        1000, 500,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("CD Rom Reader App"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, Windows desktop!");

    switch (message)
    {
    case WM_CREATE:
        return 0;

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
            case IDC_BUTTON:
            
                HWND hwndButton = CreateWindow(
                L"BUTTON",  // Predefined class; Unicode assumed 
                L"OK",      // Button text 
                WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
                10,         // x position 
                10,         // y position 
                100,        // Button width
                100,        // Button height
                hWnd,     // Parent window
                NULL,       // No menu.
                (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                NULL);      // Pointer not needed.

                //do whatever you want to do here when button is pressed
                break;
        }
    }
    break;


    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, Windows desktop!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}
#包括
#包括
#包括
#包括
#定义IDC_按钮3456
//全局变量
//主窗口类名称。
静态TCHAR szWindowClass[]=“CD_ROM_READER_APP”);
//显示在应用程序标题栏中的字符串。
静态TCHAR szTitle[]=“CD-Rom阅读器应用程序”);
HINSTANCE hInst;
//转发此代码模块中包含的函数声明:
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int回调WinMain(
_在uhinstance中,
_在当前情况下,
_在LPSTR lpCmdLine中,
_In_uuint nCmdShow
)
{
WNDCLASSEX wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc=WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=加载图标(hInstance,IDI_应用程序);
wcex.hCursor=LoadCursor(空,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+2);
wcex.lpszMenuName=NULL;
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=加载图标(wcex.hInstance,IDI_应用程序);
如果(!RegisterClassEx(&wcex))
{
MessageBox(空,
_T(“调用RegisterClassEx失败!”),
_T(“CD-Rom阅读器应用程序”),
无效);
返回1;
}
//将实例句柄存储在全局变量中
hInst=hInstance;
//CreateWindow的参数解释如下:
//szWindowClass:应用程序的名称
//szTitle:出现在标题栏中的文本
//WS_OVERLAPPEDWINDOW:要创建的窗口类型
//CW_使用默认值,CW_使用默认值:初始位置(x,y)
//500、100:初始尺寸(宽度、长度)
//NULL:此窗口的父级
//NULL:此应用程序没有菜单栏
//hInstance:WinMain的第一个参数
//NULL:此应用程序中未使用
HWND HWND=CreateWindow(
szWindowClass,
szTitle,
WS_重叠窗口,
CW_USEDEFAULT,CW_USEDEFAULT,
1000, 500,
无效的
无效的
hInstance,
无效的
);
如果(!hWnd)
{
MessageBox(空,
_T(“调用CreateWindow失败!”),
_T(“CD-Rom阅读器应用程序”),
无效);
返回1;
}
//ShowWindow的参数解释如下:
//hWnd:从CreateWindow返回的值
//nCmdShow:WinMain的第四个参数
展示窗口(hWnd,
nCmdShow);
更新窗口(hWnd);
//主消息循环:
味精;
while(GetMessage(&msg,NULL,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}
//功能:WndProc(HWND、UINT、WPARAM、LPARAM)
//
//用途:处理主窗口的消息。
//
//WM_绘制-绘制主窗口
//WM_DESTROY-发布退出消息并返回
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
PAINTSTRUCT-ps;
HDC-HDC;
TCHAR问候语[]=\T(“你好,Windows桌面!”);
开关(信息)
{
案例WM_创建:
返回0;
case WM_命令:
{
开关(LOWORD(wParam))
{
机箱按钮:
HWND hwndButton=创建窗口(
L“BUTTON”,//预定义类;假定为Unicode
L“确定”,//按钮文本
WS|TABSTOP | WS|u VISIBLE | WS|u CHILD | BS|u def按钮,//样式
10,//x位置
10,//y位置
100,//按钮宽度
100,//按钮高度
hWnd,//父窗口
NULL,//没有菜单。
(HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE),
NULL);//不需要指针。
//按下按钮后,在此处执行任何操作
打破
}
}
打破
案例WM_油漆:
hdc=开始喷漆(hWnd和ps);
//这是你的申请表。
//对于本介绍,我们只需打印“你好,Windows桌面!”
//在左上角。
文本输出(hdc,
5, 5,
问候语;
//结束特定于应用程序的布局部分。
端漆(hWnd和ps);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
打破
}
返回0;
}

您应该在
WM\u create
中(或在主功能中)创建一个按钮,并传递
HMENU
参数,然后使用
WM\u命令
处理按钮按下功能

您可以参考以下代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, Windows desktop!");

    switch (message)
    {
    case WM_CREATE:
    {
        HWND hwndButton = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed 
            L"OK",      // Button text 
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
            10,         // x position 
            10,         // y position 
            100,        // Button width
            100,        // Button height
            hWnd,     // Parent window
            (HMENU)IDC_BUTTON,       // No menu.
            (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
            NULL);      // Pointer not needed.
        return 0;
    }
    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDC_BUTTON:
            //do whatever you want to do here when button is pressed
            //like
            MessageBox(NULL,L"hello windows", L"title", 0);
        }
    }
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, Windows desktop!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}
您将在何处(或如何)向主窗口发送
IDC\u按钮
命令?代码仅在消息循环处理该消息时创建该按钮。