1

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.

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Then how can I access them without global declaring them? HWND _3x3;(you mean, these are my global declarations, right?)
You can access them since they are global. The problem is that you are declaring new local variables which, because they have the same names as the global variables, hide the global variables. So, instead of HWND _3x3 = CreateWindowEx... write _3x3 = CreateWindowEx...
2

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; 

Comments

0

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

2 Comments

Including tchar.h tells nothing about the type of build. That's the point, actually.
Both windows and build configuration are ANSI. You can tell by the (narrow) string literals passed to CreateWindowEx, which implies CreateWindowExA. The main window is odd, though. It is registered as an ANSI window, but created using the UNICODE API.
0

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.

2 Comments

I need to change text on button click. By doing that, wouldn't i change text in start of the program?
@Jancis no because WindowProc is called every time there is a message to handle. One of these messages is the WM_SETTEXT that you send to the button, which is correctly handled by DefWindowProc
-1

You can change window's text by using SetWindowText function, or SetDlgItemText for dialog controls.

BOOL SetWindowTextA( [in] HWND hWnd, [in, optional] LPCSTR lpString ); 

Here's function declaration from MSDN.

So, to change button text on click, WM_COMMAND should look like this:

 case WM_COMMAND: switch(wParam) { case BUTTON_3X3: SetWindowText(_3x3, "NewText"); break; } 

Hope it helps.

1 Comment

This completely misses the core issue, as explained in this answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.