C++ Win32 窗口无响应

标签 c++ winapi

我有一个控制台应用程序,我从中创建了一个窗口。

我可以很好地渲染窗口中的内容。但是窗口没有响应/用户无法控制。

一旦您将鼠标悬停在窗口上,您就会看到沙漏光标并且无法移动窗口。

这可能是什么原因造成的?

编辑:

    WNDCLASSEX wndClass;         // Window class
    ZeroMemory(&wndClass, sizeof(wndClass)); // Clear the window class structure
    wndClass.cbSize = sizeof(WNDCLASSEX); 
    wndClass.style          = CS_HREDRAW | CS_VREDRAW | CS_CLASSDC;
    wndClass.lpfnWndProc    = DefWindowProc;
    wndClass.cbClsExtra     = 0;
    wndClass.cbWndExtra     = 0;
    wndClass.hInstance      = nullptr;
    wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wndClass.lpszMenuName   = NULL;//MAKEINTRESOURCE(IDR_MAINMENU);
    wndClass.lpszClassName  = _classname.c_str();
    wndClass.hIconSm        = 0;

    if (RegisterClassEx(&wndClass) == 0)// Attemp to register the window class
        throw std::exception("WINDOW ERROR: Failed to register the window class!");

    DWORD dwStyle = 0;              // Window styles
    DWORD dwExStyle = 0;            // Extended window styles

    dwStyle = WS_OVERLAPPEDWINDOW |        // Creates an overlapping window
              WS_CLIPCHILDREN |            // Doesn"t draw within child windows
              WS_CLIPSIBLINGS;              // Doesn"t draw within sibling windows

    //adjust window size
    RECT rMain;
    rMain.left = 0;
    rMain.right = width;
    rMain.top = 0;
    rMain.bottom = height;  

    AdjustWindowRect(&rMain, dwStyle, 0);

    // Attempt to create the actual window
    _hwnd = CreateWindowEx( dwExStyle,     
                            className,
                            windowTitle,      
                            dwStyle,         
                            0, 0,           
                            rMain.right - rMain.left,
                            rMain.bottom - rMain.top,  
                            nullptr,             
                            0,  
                            nullptr,
                            0); 


    ShowWindow(_hwnd, SW_SHOW);
    SetForegroundWindow(_hwnd);
    SetFocus(_hwnd);

最佳答案

既然已经在评论中提到了,我将制作这个社区 wiki

您需要获取窗口的消息并相应地发送它们。

/* 
 *  HWND hWnd: this is the handle to your window (that is returned from CreateWindow[Ex]
*/
MSG msg;
while (GetMessage(&msg, hWnd, NULL, NULL) > 0){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

如果您不这样做,您的 wndProc 函数将永远不会收到任何消息,并且 Windows 会发现它没有响应(因此是沙漏)。

关于C++ Win32 窗口无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3529740/

相关文章:

c++ - 为什么要为 char* 和 wchar* 而不是其他原始类型制作一个特殊版本的 uninitialized_copy()?

c++ - 获取 Windows 启动消息的语言

c++ - Listview 三态复选框

c++ - 类(class)模板与友元

c++ - 如何在 gcc 中声明和定义一个纯函数?

c++ - 如何将标准 IP 地址格式字符串转换为十六进制和长?

c++ - 右值到左值的转换?

c++ - Windows 10 相机框架服务器,使用网络摄像头识别实际应用程序

windows - Win32 ShellEx StorageHandler 的任何文档?

c++ - 在 native Win32 GDI 中绘制稍微透明的蓝色矩形