[英]change button text 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);
}
對不起,這堆代碼。 如果我知道哪個部分很重要,我會縮短它。 抱歉我的英語不好。 謝謝。
你的線路
HWND _3x3 = CreateWindowEx...
籠罩全球。 如果您調試了程序,您會注意到當您嘗試發送消息時_3x3
為 NULL。
試試這個:
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;
我認為您的窗口是ANSI ,但是您將項目編譯為UNICODE 。 (使用 TCHAR.h)
您必須調用DefWindowProc來處理任何其他消息,例如 WM_SETTEXT 和其他消息。 您可以簡單地將它放在 WindowProc 中已有的開關的默認情況下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.