更改按钮文本winapi

时间:2013-12-07 17:33:34

标签: c++ winapi

我是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);
}

抱歉这一堆代码。如果我知道哪个部分很重要,我会缩短它。对不起,我的英语不好。谢谢。

4 个答案:

答案 0 :(得分:7)

你的行

HWND _3x3 = CreateWindowEx...

影响全球。如果您调试了该程序,那么当您尝试发送该消息时,您会注意到_3x3为NULL。

答案 1 :(得分:1)

尝试一下:

case WM_COMMAND:
{
   if(LOWORD(wParam) == BUTTON_3x3)
   {
         _3x3 = NULL;
         _3x3 = CreateWindowEx(0,
                                         WC_BUTTON,
                                        "Text",230, 300, 100, 100,
                                         hwnd, 
                                        (HMENU)BUTTON_3x3,
                                         NULL, NULL);
   }
}
      break;

答案 2 :(得分:0)

我的窗口是 ANSI ,但您将项目编译为 UNICODE 。 (使用TCHAR.h)

答案 3 :(得分:0)

您必须调用DefWindowProc来处理任何其他消息,例如WM_SETTEXT和其他消息。您可以简单地将它放在WindowProc中已有的交换机的默认情况下。