我对 WinAPI 很陌生,所以有时会犯很多基本错误。你已被警告,让我们转向我的问题 :P/
我想制作类似网格的东西。如果用户单击网格的一个字段,应该会出现一个位图。我制作了一个我想用作按钮的字段位图。一开始用户输入网格的大小,所以我用该位图制作了一个动态按钮库。不幸的是,我不知道单击它们时如何处理它们。这是我的代码:
//there I create my window. I also make a global variable bool* table and HWND next.
table = new bool[x*y];
for (int i = 0; i < x*y; ++i)
table[i] = 0;
//the table represents if the fields are already filled or not.
HWND* buttons = new HWND[x*y];
next = CreateWindowEx(4, _T("BUTTON"), _T("MOVE"), WS_CHILD | WS_VISIBLE, x * 12 - 25, (y + 4) * 25 - 90, 100, 50, hwnd, NULL, hThisInstance, NULL);
for (int i = 0; i < x; ++i)
{
for (int j = 0; j < y; ++j)
{
buttons[i + j * x] = CreateWindowEx(0, _T("BUTTON"), NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP, 0 + i * 25, 0 + j * 25, 25, 25, hwnd, NULL, hThisInstance, NULL);
SendMessage(przyciski[i + j * x], BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)field);
}
}
现在在 WindowProcedure() 中:
switch (message)
{
case WM_COMMAND:
if ((HWND)lParam == next) /* there will be some code in the future ;) */;
else
{
//So here I need to set correct the value in table to 1
//I have a handle to clicked button (HWND)lParam, but I don't know how to get it's position in the table
}
break;
我尝试用 HWND 和 int x, y 做一些结构,但我仍然不知道如何管理它。顺便说一句,代码可能看起来很旧,我需要创建一个 Windows XP 可以运行的应用程序(这是一个学校项目,不要询问 :P),此外我使用了一个非常旧的教程。
最佳答案
使用 HMENU
参数为每个按钮分配一个 ID。 ID 不能为零,因此添加任意偏移量,例如 100
:
buttons[i + j * x] = CreateWindowEx(0, _T("BUTTON"), NULL,
WS_CHILD | WS_VISIBLE | BS_BITMAP, 0 + i * 25, 0 + j * 25, 25, 25,
hwnd, HMENU(100 + i + j * x), hThisInstance, NULL);
您还可以从 button_index
中提取行和列,了解总行数和总列数。示例:
case WM_COMMAND:
{
if (HIWORD(wParam) == BN_CLICKED)
{
int id = LOWORD(wParam);
int button_index = id - 100;
if (button_index >= 0 && button_index < x * y)
{
int row = button_index / x;
int column = button_index % x;
...
}
...
}
break;
}
关于c++ - 处理按钮表 (WinAPI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40963047/