【发布时间】:2022-01-05 15:08:19
【问题描述】:
int Num = 0;
LRESULT CALLBACK TestWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rc;
GetClientRect(hWnd, &rc);
RECT Winrc;
GetWindowRect(hWnd, &Winrc);
SYSTEMTIME time;
GetLocalTime(&time);
static const wchar_t* BoxTxt = L"";
static int MeIs = Num;
switch (message)
{
case WM_CREATE:
{
SetWindowLong(hWnd, GWL_EXSTYLE,
GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 220, LWA_ALPHA);
//GhWnd = hWnd;
break;
}
case WM_LBUTTONUP:
{
wchar_t meChar[20] = L"";
_itow(MeIs, meChar, 10);
MessageBox(0, meChar, meChar, 0);
}
case WM_SIZE:
{
InvalidateRect(hWnd, &rc, 1);
break;
}
case WM_NCLBUTTONDBLCLK:
{
break;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CLOSE:
{
Num -= 1;
DestroyWindow(hWnd);
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int CreateTestWindow()
{
//Call testwndproc. To reduce the length of the problem description, omit these codes
Num+=1;
return 0;
}
在上面的代码中,当我创建多个窗口并点击它时,应该会弹出“1”、“2”、“3”……但实际上都是弹出“1”。
static int MeIs = 0;
case WM_CREATE:
{
MeIs = Num;
}
改成上面的代码,会弹出最后一个窗口的序列号。比如第四个窗口创建时,所有窗口都会弹出“4”
在实际应用中,每个窗口都有自己的设置,并存储在向量中。每个窗口根据自己的序列号找到自己的设置:
struct Data
{
int x;
int y;
int width;
int height;
const wchar_t* text;
}
std::vector<data>UserData(32);//Max:32
//then read them from file,But the window must know which window it is:UserData[i].
例如,第一个窗口会将它们的坐标设置为UserData[1].x和UserData[1].y,关闭时也需要保存文件。 有什么想法吗?谢谢!
【问题讨论】:
-
static int MeIs- 你在程序中只得到其中一个,而不是每个窗口一个。如果您想要每个窗口的数据,您可以在注册窗口类时添加存储空间。 -
但是如何增加存储空间呢?我希望每个窗口数据是独立的,但是WndProc只有一个。
-
你应该阅读 Petzold 的书。像这样学习真的不会有成效。
-
hwnd不是已经表示窗口了吗?