我通过以下方式注册了回调:
SetWindowLongW(hWindow, GWL_WNDPROC, (LONG)WindowCallback);
我在那里收到消息没有问题。我在回调中有if()样式代码,如下所示:
if (MSG == 1)
{
*boolptr = true;
...
} else if(MSG == 2) {
if (*boolptr == true)
return;
// do stuff
}
变量的定义如下(globaly,#includes right ...)
volatile bool boolVar = false; volatile bool* volatile boolptr = &boolVar;
如果我在main()中等待* boolptr为TRUE,那么一切都很好,但是如果我在同一个回调中等待它应该更改,它就不起作用。显然我不明白这里有一些概念,所以我要求帮助或解释为什么它不起作用。
值为2的MSG总是首先到达,然后经过一些处理后的MSG值为1.经过一段时间相同的事情发生但是我不想做同样的处理并浪费CPU周期因此我需要知道MSG是否[1]正在关注它并返回。我希望你理解我。
谢谢!
答案 0 :(得分:0)
你的描述留下了很多细节,但我想我明白你想要什么。 CreateWindow()API函数的最后一个参数可用于将指向回调的指针传递给某个外部变量:
// MAKE SURE this variable stays in scope throughout the lifetime of your window !
// Make it global or static
bool myVar;
// let's create an overlapped window of some class with size 400 x 300
HWND hWnd = CreateWindow("My class name", "My Title",
WS_VISIBLE | WS_BORDER| WS_OVERLAPPED | WS_CAPTION |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
0,0, 400, 300, NULL, NULL, GetModuleHandle(NULL), &myVar);
然后在回调中,您可以在WM_CREATE消息中访问它:
// ... start of your callback
// get a pointer to your variable here and use it in all messages
// *except* WM_CREATE that sets it during window instance creation
bool *pMyVar = (bool *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
switch(uMsg)
{
case WM_CREATE:
{
// get your pointer from the CREATESTRUCT passed in
// and store it in the user data area of your window class
// note: your WNDCLASS should have its cbWndExtra = sizeof(void *);
CREATESTRUCT *pC = (CREATESTRUCT *)lParam;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pC->lpCreateParams);
// ....
}
break;
case 1 : // VERY bad choice for a message, use WM_USER+1 instead
*pMyVar = true;
break;
// ... rest of your callback
这是你想要的吗?
编辑:好的,这是你想看的课程:
class MyClass
{
public:
MyClass() : _hasRun(false) { }
void DoStuff(HWND hW, UINT uMsg)
{
if (!_hasRun)
{
_hasRun = true;
// ... do your work here, you have the window handle and the message
}
}
private:
bool _hasRun;
};
现在,在您的main中创建此类的实例并将其保存在静态或全局变量中:
MyClass *pC = new MyClass;
然后,将其传递给我在开头显示的回调,然后始终调用它。 DoStuff成员函数将检查消息并根据消息执行您想要的操作一次。即使你把它保存在全局中并且始终从回调中调用这个全局,功能也是一样的。
// ... callback code before you handle any message
pC->DoStuff(hWnd, uMsg);
// ... rest of callback code