我是WINAPI编程的新手。我想在Windows窗体上制作一个简单的井字游戏,但是当我想更改按钮文本时遇到问题。我的代码可以编译,但是SendMessage函数只是不起作用。我的代码:

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>

#define __T(x) L ## x

#define BUTTON_1x1 1
#define BUTTON_1x2 2
#define BUTTON_1x3 3

#define BUTTON_2x1 4
#define BUTTON_2x2 5
#define BUTTON_2x3 6

#define BUTTON_3x1 7
#define BUTTON_3x2 8
#define BUTTON_3x3 9

HWND hWnd;
HWND _3x3;
HWND _3x2;
HWND _3x1;
HWND _2x3;
HWND _2x2;
HWND _2x1;
HWND _1x3;
HWND _1x2;
HWND _1x1;

LRESULT CALLBACK WindowProc(HWND hWnd,
                     UINT message,
                     WPARAM wParam,
                     LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";


RegisterClassEx(&wc);
hWnd = CreateWindowExW(NULL,
                      L"WindowClass1",    // name of the window class
                      L"Desas",   // title of the window
                      WS_OVERLAPPEDWINDOW,    // window style
                      300,    // x-position of the window
                      300,    // y-position of the window
                      380,    // width of the window
                      480,    // height of the window
                      NULL,    // we have no parent window, NULL
                      NULL,    // we aren't using menus, NULL
                      hInstance,    // application handle
                      NULL);    // used with multiple windows, NULL
ShowWindow(hWnd, nCmdShow);
MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
    case WM_CREATE:
        {
            HWND _1x1 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "21",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        30, 100, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_1x1,
                                        NULL,
                                        NULL);

            HWND _1x2 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "32",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        130, 100, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_1x2,
                                        NULL,
                                        NULL);
            HWND _1x3 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "12",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        230, 100, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_1x3,
                                        NULL,
                                        NULL);

            HWND _2x1 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "32",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        30, 200, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_2x1,
                                        NULL,
                                        NULL);
            HWND _2x2 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "12",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        130, 200, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_2x2,
                                        NULL,
                                        NULL);

            HWND _2x3 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "23",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        230, 200, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_2x3,
                                        NULL,
                                        NULL);

            HWND _3x1 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "12",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        30, 300, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_3x1,
                                        NULL,
                                        NULL);

            HWND _3x2 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "33",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        130, 300, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_3x2,
                                        NULL,
                                        NULL);

            HWND _3x3 = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "BUTTON",
                                        "12",
                                        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_TEXT,
                                        230, 300, 100, 100,
                                        hWnd,
                                        (HMENU) BUTTON_3x3,
                                        NULL,
                                        NULL);

            HWND _PLAYER_TXT = CreateWindowEx(NULL,
                                        "STATIC",
                                        "PLAYER :",
                                        WS_VISIBLE | WS_CHILD | SS_LEFT,
                                        230, 50, 100, 30,
                                        hWnd,
                                        (HMENU) 999,
                                        NULL,
                                        NULL);
        } break;

        case WM_COMMAND:
        {
                if (LOWORD(wParam) == BUTTON_3x3)
            {
                std::cout << "!!!!!" << std::endl; //test(if buttonclick message is  recived)

                SendMessage(_3x3, WM_SETTEXT, 0, (LPARAM) _T("NewText")); //not working
            }
        } break;

    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}


对不起,这些代码。如果我知道哪一部分很重要,我会简化它。对不起,我的英语不好。谢谢。

最佳答案

你的线

HWND _3x3 = CreateWindowEx...


笼罩全球。如果调试程序,则在尝试发送消息时会注意到_3x3为NULL。

关于c++ - 更改按钮文本winapi,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20444352/

10-12 16:14