发送 lparam 作为指向类的指针,并在 WndProc() 中使用它
我有这个抽象代码: 我想在 CreateWindowEx() 中使用 lParam (最后一个参数)来保存指向在 main - SaveArr 开头声明的类的指针。然后,我想在函数 WndProc 中使用它。 一开始我做了一个全局数组,然后我可以在任何地方使用它,但就 C++ 而言,它并不是那么“聪明”,所以我尝试对其进行一些升级。
class Samples
{
int arr[ITERATIONS+1];
int index;
...
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
Samples * SaveArr;
...
hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
ClsName,
WindowCaption,
WS_OVERLAPPEDWINDOW,
INITIAL_WIN_LOCAT_X,
INITIAL_WIN_LOCAT_Y,
WIN_WIDTH,
WIN_HIGHT,
NULL,
NULL,
hInstance,
NULL); //here i want to pass SaveArr, so that i can use it in the WndProc(...) function
...
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
... //here i would like to use lParam as the class pointer, meaning using the
SaveArr declared in the main function.
}
}
i have this abstract code :
i want to use lParam (last parameter) in CreateWindowEx() to save a pointer to a class thats declared in the begining of main - SaveArr. then, i want to use it in the function WndProc.
in the begining i did a global array, and then i could use it anywhere, but its not so "clever" as far as c++ concern, so im trying to upgrade it a bit.
class Samples
{
int arr[ITERATIONS+1];
int index;
...
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
Samples * SaveArr;
...
hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
ClsName,
WindowCaption,
WS_OVERLAPPEDWINDOW,
INITIAL_WIN_LOCAT_X,
INITIAL_WIN_LOCAT_Y,
WIN_WIDTH,
WIN_HIGHT,
NULL,
NULL,
hInstance,
NULL); //here i want to pass SaveArr, so that i can use it in the WndProc(...) function
...
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
... //here i would like to use lParam as the class pointer, meaning using the
SaveArr declared in the main function.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
向窗口添加调用者信息:
与扩展的
CreateWindowEx
类似。获取指向调用者的指针:
需要在消息回调中调用此方法。
Adding caller information to the window:
Similar for the extended
CreateWindowEx
.Obtaining a pointer to the caller:
This method needs to be called in your message callback.
最好的方法是
下次调用 WndProc 时,该值将位于 savearr 中,并且可以使用。
Best way would be
The next time the WndProc is called the value would be in savearr, and can be used.
来自参考:
您期望 lParam 始终传递到 WndProc,但它仅与 WM_CREATE 一起传递。
请注意,即使如此,它也不是直接传递的,而是通过一个结构传递的,该结构是 WM_CREATE 的实际 lParam。
From the reference:
You're expecting the lParam to be always passed to WndProc, but it is only passed with WM_CREATE.
Note, that even then it's not passed directly, but rather through a structure which is the actual lParam for WM_CREATE.
读取 lParam 的唯一机会是在 WM_CREATE 期间。如果您想稍后继续使用该值,则必须将其存储在某个地方。也许作为 WndProc 的静态或以其他方式将其分配给其他将被限定作用域的东西。
Your only chance to read the lParam is during WM_CREATE. If you want to keep using the value later then you must store it somewhere. Maybe as a static of the WndProc or otherwise assign it to something else which is going to be scoped.
为什么坚持使用最后一个 lpParam 值设置为 X,然后在 WM_CREATE 上捕获它(通过所有间接结构的东西,不少于!),然后设置 GWL_USERDATA?!
为什么不开门见山地这样做:
HWND H=创建窗口(.....)
SetWindowLong(H,GWL_USERDATA,X)
换句话说,您自己直接将 X 放在窗口创建语句之后。
在我的测试中它是有效的,只要您针对某些已知句柄列表测试窗口句柄,您就可以防止程序拾取一些错误消息,并防止不当使用其他用户数据。
Why all the insistence on using that last, lpParam value set to X, then catching it on WM_CREATE (through all that indirect struct stuff, no less!) and then setting GWL_USERDATA?!
Why not cut to the chase and do this:
HWND H=CreateWindow(.....)
SetWindowLong(H,GWL_USERDATA,X)
In other words, just put X there directly, yourself, right after the window creation statement.
In my tests it works, and so long as you test the window handle against some list of known handles, you can prevent some errant message picked up by your program, and prevent inappropriate use of something else's userdata.