发送lparam作为指向类的指针,并在windowproc()中使用它
sending lparam as a pointer to class, and use it in 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.
}
}
向窗口添加调用者信息:
m_window = CreateWindow(..., this);
与扩展的CreateWindowEx
相似。
获取调用者的指针:
template< typename CallerT >
[[nodiscard]]
CallerT* WindowCaller(HWND window, UINT message, LPARAM lParam) noexcept
{
if (message == WM_NCCREATE) [[unlikely]]
{
const auto caller = reinterpret_cast< CallerT* >(
reinterpret_cast< CREATESTRUCT* >(lParam)->lpCreateParams);
// Change the user data of the window for subsequent messages.
::SetWindowLongPtr(window, GWLP_USERDATA,
reinterpret_cast< LONG_PTR >(caller));
return caller;
}
else
{
// Retrieve the user data of the window.
return reinterpret_cast< CallerT* >(
::GetWindowLongPtr(window, GWLP_USERDATA));
}
}
这个方法需要在你的消息回调中调用。
最好的办法是
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
Samples *savearr = (Samples*)GetWindowLong(hWnd,GWL_USERDATA)
switch(Msg)
{
case WM_CREATE:
SetWindowLong(hWnd, GWL_USERDATA, (LONG)lParam);
break;
}
}
下次调用WndProc时,该值将保存在保存文件中,并且可以使用
来自参考:
lpParam [in, optional]
对象所指向的CREATESTRUCT结构(lpCreateParams成员)lParam WM_CREATE消息的参数。此消息被发送到在此函数返回之前创建的窗口。Type: LPVOID Pointer to a value to be passed to the window through the
If an application calls CreateWindow to create a MDI client
窗口,lpParam应该指向一个CLIENTCREATESTRUCT结构。如果一个MDI客户端窗口调用CreateWindow来创建一个MDI子窗口。lpParam应该指向一个MDICREATESTRUCT结构。lpParam可能是如果不需要额外的数据,则为NULL。
你期望lParam总是传递给WndProc,但它只与WM_CREATE传递。
请注意,即使这样,它也不是直接传递的,而是通过一个结构体传递的,这个结构体是WM_CREATE的实际lParam。
读取lParam的唯一机会是在WM_CREATE期间。如果您希望以后继续使用该值,则必须将其存储在某个地方。可能是作为WndProc的静态变量或者将它赋值给其他有作用域的东西
为什么坚持使用最后一个lpParam值设置为X,然后在WM_CREATE上捕获它(通过所有间接的结构体的东西,不低于!),然后设置GWL_USERDATA?!
为什么不开门见山,这样做:HWND H = CreateWindow(…)SetWindowLong (H, GWL_USERDATA, X)换句话说,你自己直接把X放在窗口创建语句后面。
在我的测试中,它工作,只要你测试窗口句柄的一些已知的句柄列表,你可以防止一些错误的消息被你的程序,并防止不适当地使用其他的userdata。
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 在函数内部的声明中初始化数组,并在外部使用它
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 定义C++新的环境变量并在 bat 文件中使用它
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 是否有必要获取锁并在不需要唤醒线程时通知condition_variable?
- 将通用对象传递给 Rust 并在使用后传递回 C++ 进行销毁
- 动态获取 esp32 的 mac 地址并在以太网库中使用它.
- 从.txt文件中读取浮点型数字并在公式中使用它们
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 在C++的头文件中使用常量并在程序中询问其地址的任何潜在危险
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 如何在组合框中列出所有可用的 VCL 样式,并在组合框更改事件中应用该样式C++生成器?
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 发送lparam作为指向类的指针,并在windowproc()中使用它