简体   繁体   中英

change button text winapi

I'm new to WINAPI programming. I want to make a simple tic-tac-toe game on windows forms, but i have a problem when i want to change button text. My code compiles, but that SendMessage function just don't work. My code :

#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);
}

Sorry for this bunch of code. I would make it shorter, if I knew which part is important. Sorry for my bad english. Thank you.

Your line

HWND _3x3 = CreateWindowEx...

shadows the global. If you debugged the program, you would have noticed that _3x3 was NULL when you tried to send the message.

try this:

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;

I thing your window is ANSI , but you compiled your project as UNICODE . (Using TCHAR.h)

You have to call DefWindowProc to handle any other message, like WM_SETTEXT and others. You can simply put it in the default case of the switch you already have in your WindowProc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM