c++ Win32 API应用程序故障[已关闭]

68bkxrlz  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(188)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
1小时前关闭。
Improve this question
我是Win32 API的新手,我试着制作自己的计算器,但是当我运行代码时,编辑控件开始出现故障,我看不到输入了什么。
我试着添加一个按钮,将值1存储到编辑控件中(计算器的文本将显示在那里),但每当我使用该按钮,或试图直接键入时,文本就开始出现小故障。

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

#define ONE_BUTTON 1
/*#define TWO_BUTTON 2
#define THREE_BUTTON 3
#define FOUR_BUTTON 4
#define FIVE_BUTTON 5
#define SIX_BUTTON 6
#define SEVEN_BUTTON 7
#define EIGHT_BUTTON 8
#define NINE_BUTTON 9
#define TEN_BUTTON 0*/



void AddControls(HWND);

HWND hOut;

HWND hOne;

LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst , LPSTR args, int ncmdshow)
{
    WNDCLASSW wc = {0};
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"mywindowclass";
    wc.lpfnWndProc = WindowProcedure;
    if(!RegisterClassW(&wc))
        return -1;

    CreateWindowW(L"mywindowclass",L"calculatorGUi",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,500,350,NULL,NULL,NULL,NULL);

    MSG msg = {0};
    while(GetMessage(&msg,NULL,NULL,NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch(msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case ONE_BUTTON:
            char one[50];
            char out[15];
            GetWindowText(hOne,one,50);

            strcpy(out,one);

            SetWindowText(hOut,out);
        }

    case WM_CREATE:
        AddControls(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

void AddControls(HWND hWnd)
{
    hOut = CreateWindowW(L"Edit",(int)NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,2,2,480,106,hWnd,NULL,NULL,NULL);

    hOne= CreateWindowW(L"Button",L"1",WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON|BS_DEFPUSHBUTTON 2,110,98,50,hWnd,(HMENU)ONE_BUTTON,NULL,NULL);

    /*CreateWindowW(L"Button",L"2",WS_VISIBLE|WS_CHILD,102,110,99,50,hWnd,(HMENU)TWO_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"3",WS_VISIBLE|WS_CHILD,202,110,99,50,hWnd,(HMENU)THREE_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"4",WS_VISIBLE|WS_CHILD,302,110,99,50,hWnd,(HMENU)FOUR_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"5",WS_VISIBLE|WS_CHILD,402,110,80,50,hWnd,(HMENU)FIVE_BUTTON,NULL,NULL);

    CreateWindowW(L"Button",L"6",WS_VISIBLE|WS_CHILD,2,165,99,50,hWnd,(HMENU)SIX_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"7",WS_VISIBLE|WS_CHILD,102,165,99,50,hWnd,(HMENU)SEVEN_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"8",WS_VISIBLE|WS_CHILD,202,165,99,50,hWnd,(HMENU)EIGHT_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"+",WS_VISIBLE|WS_CHILD,302,165,99,50,hWnd,(HMENU)NINE_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"-",WS_VISIBLE|WS_CHILD,402,165,99,50,hWnd,(HMENU)TEN_BUTTON,NULL,NULL);

    CreateWindowW(L"Button",L"9",WS_VISIBLE|WS_CHILD,2,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"0",WS_VISIBLE|WS_CHILD,102,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"*",WS_VISIBLE|WS_CHILD,202,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"/",WS_VISIBLE|WS_CHILD,302,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"=",WS_VISIBLE|WS_CHILD,402,220,99,50,hWnd,NULL,NULL,NULL);*/

}
k2fxgqgv

k2fxgqgv1#

您的WM_COMMANDONE_BUTTON案例都缺少break语句,因此每当Edit向主窗口发送更新通知时,或者每当单击按钮时,您的代码就会调用AddControls(),在以前的子控件上创建越来越多的子控件,并重用相同的子ID。
此外,如果用户输入超过14个字符的文本,ONE_BUTTON处理程序将发生缓冲区溢出。
试试这个:

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch(msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case ONE_BUTTON: {
            char one[50] = {};
            GetWindowText(hOne, one, 50);
            SetWindowText(hOut, one);
            break; // <-- add this!
        }
        break; // <-- add this!

    case WM_CREATE:
        AddControls(hWnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break; // <-- add this!

    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }

    return 0; // <-- add this!
}

相关问题