Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winapi C++;-窗口消息循环已冻结_Winapi_Loops_Message - Fatal编程技术网

Winapi C++;-窗口消息循环已冻结

Winapi C++;-窗口消息循环已冻结,winapi,loops,message,Winapi,Loops,Message,我在这里有一节课,我是根据另一节课做的。它应该处理整个创建窗口的工作,但现在似乎陷入了困境。以前的版本很好用,我不知道我可能忘了在这个版本中添加什么,这可能会导致它像这样挂起 这是消息循环: int Window::HandleMessages() { while(GetMessage(&this->windat.msgs, NULL, 0, 0)) { TranslateMessage(&this->windat.msgs);

我在这里有一节课,我是根据另一节课做的。它应该处理整个创建窗口的工作,但现在似乎陷入了困境。以前的版本很好用,我不知道我可能忘了在这个版本中添加什么,这可能会导致它像这样挂起

这是消息循环:

int Window::HandleMessages()
{
    while(GetMessage(&this->windat.msgs, NULL, 0, 0))
    {
        TranslateMessage(&this->windat.msgs);
        DispatchMessage(&this->windat.msgs);
    }
    return this->windat.msgs.wParam;
}
相当基本的东西,我不知道为什么,但它只是挂。。。当我运行这个程序时,它只会显示一个空的提示窗口,通过测试,如果我在while循环之前使用它,我会让它显示一个消息框,但在它内部不起作用。我一直在尝试比较这个类和旧类,还没有弄清楚这个类可能有什么问题。有人能告诉我什么可能触发这种行为吗? 谢谢


好了,这让我很困惑。通过混淆GetLastError,它似乎在我实例化窗口类之前,将错误2(找不到文件)返回到我放置的任何位置,即使是在Main的开头。若我在CreateWindowEx之后随时调用GetLastError,它将返回类似1047之类的错误,关于找不到类之类的错误。HWND也变为空
以下是main.cpp的代码:

#include "SimpWin/SimpWin.h"
#include <stdio.h>

//  Make the class name into a global variable
char szClassName[] = "WindowsApp";


void ErrorExit(LPTSTR lpszFunction)
{
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
    sprintf((char*)lpDisplayBuf,
        TEXT("%s failed with error %d: %s"),
        lpszFunction, dw, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw);
}

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    ErrorExit(TEXT("CreateWindowEx"));
    Window* win = Window::CreateWindowClass(hThisInstance, szClassName, WindowProcedure);
    if(!win->Register())
    {
        return 0;
    }


    win->Show(nFunsterStil);

    int res = win->HandleMessages();

    delete win;

    return res;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc (hwnd, message, wParam, lParam);
}

我在这里迷路了,我不知道为什么会这样/

使用PeekMessage而不是GetMessage。

检查-如果出现错误,while循环将不会退出。应该是这样的:

while (GetMessage(&this->windat.msgs, NULL, 0, 0) > 0)
{
 ...
}

好吧,我终于成功了D

这实际上和我在这里上的一门完全无关的课有关。这是我创建的一个字符串类(从数组派生),复制函数有一个bug,它会复制我传递给它的字符数组,但不会更新该类的长度字段。。。 每当我必须通过运算符=,将类设置为一个值时,就会调用copy函数。运算符char*需要该长度才能将类转换为c格式字符串。在将类名和标题值传递给CreateWindowEx时,我会使用该强制转换,它将返回一个0个字符的数组,这就是地狱发生的地方。

现在我修好了,它现在工作得很好。谢谢:D

虽然它很旧了。。。发件人:

GetMessage
不同,
PeekMessage
函数在返回之前不会等待消息被发布

也就是说,
GetMessage
等待下一条消息可用。您将此等待过程视为冻结,可能是因为您实际上并不打算等待消息


请注意,您可以在假定冻结时附加调试器,暂停执行并检查线程的调用堆栈。一旦在堆栈上找到线程及其调用堆栈及其
GetMessage
,您就可以很好地隔离问题,从而知道在何处可以阅读记录的行为。

听起来好像完全跳过了while。好的,所以我就这样试过了。。。通过再次摆弄MessageBox,程序似乎从未收到任何消息,因为当处于while循环时,警报框将继续出现,但当我将其放入if时(PeekMessage…它将不会出现:/n无论类型签名表明什么,
GetMessage
都不会返回布尔值。为什么要进行向下投票?检查doc链接,引用“返回值可以是非零、零或-1”Hi,但是为什么类错误让您认为它挂在GetMessage中?我的意思是,您认为它在GetMessage,不在代码中的其他位置,对吗?
while (GetMessage(&this->windat.msgs, NULL, 0, 0) > 0)
{
 ...
}