c++ - 创建的窗口没有标题

标签 c++ winapi window

<分区>

我正在从我的控制台应用程序创建消息窗口。窗口类已正确注册并且窗口已正确创建,但它从来没有标题(而我的 createwindow 函数调用确实指定了标题)。 让我思考,控制台程序可以创建带有名称的窗口吗?谷歌了一下,一无所获。 这是我的代码,保持在最低限度:

using namespace std;
hInstance = GetModuleHandle(NULL);
WNDCLASS WndClass = {};
WndClass.style = CS_HREDRAW | CS_VREDRAW; // == 0x03
WndClass.lpfnWndProc = pWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hIcon = 0;
WndClass.hCursor = 0;
WndClass.hbrBackground = (HBRUSH)COLOR_WINDOWFRAME;
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "EME.LauncherWnd";
int style = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU | WS_THICKFRAME | WS_CAPTION;
if (RegisterClassA(&WndClass))
{
    cout << "class registered. Hinstance : " << hInstance <<  " style : (expect 0xcf0000) " << std::hex << style << endl;
    HWND hwind2 = CreateWindowExA(0, "EME.LauncherWnd", "Mytitle", style, 0x80000000, 0x80000000, 0x80000000, 0x80000000, NULL, NULL, hInstance, NULL);
    if (hwind2 == 0)
        cout << "Couldn't create window" << endl;
    else
        cout << "created window" << endl;
}

输出:

class registered. Hinstance : 00E40000
created window

检查 Nirsoft 的 Winlister,窗口存在,具有正确的类(“EME.LauncherWnd”),但没有名称。 此外,在 block 中添加这些代码行:

if (0 == SetWindowText(hwind2, "aTitle"))
            cout << "couldn't set a title" << endl;
        else
            cout << "title set " << endl;

输出是

title set

然而,窗口仍然没有标题。如果控制台程序没有标题,我会假设 SetWindowText 调用将返回 0。 我究竟做错了什么 ? 编辑:根据要求添加 pWndProc

LRESULT CALLBACK pWndProc(HWND hwnd,            // Handle to our main window
    UINT Msg,             // Our message that needs to be processed
    WPARAM wParam,        // Extra values of message 
    LPARAM lParam)        // Extra values of message
{
        switch (Msg)

        {
    case WM_DESTROY: 
....
break; 
         }
}

尽管在注释指出 pWndProc(我认为它与窗口的构造无关)之后,结果是将此代码行作为默认值插入 switch case

return DefWindowProc(hwnd, Msg, wParam, lParam);

解决问题。

最佳答案

我正在按照评论的建议发布问题的答案: 答案是要完成窗口创建,传递给 RegisterClass WINAPI 的 pWndProc 必须处理默认消息(特别是操作系统消息)。 在 CreateWindow 执行期间(调用开始后返回之前),pWndProc 函数已经收到它必须处理的消息,在我的例子中它没有处理它们。 这是一个标准的 pWndProc 函数:

LRESULT CALLBACK pWndProc(HWND hwnd,            // Handle to our main window
    UINT Msg,             // Our message that needs to be processed
    WPARAM wParam,        // Extra values of message 
    LPARAM lParam)        // Extra values of message
{
        switch (Msg)

        {
    case WM_DESTROY: 
...
    default:
        return DefWindowProc(hwnd, Msg, wParam, lParam);
         }
}

来源:

A window procedure does not usually ignore a message. If it does not process a message, it must send the message back to the system for default processing. The window procedure does this by calling the DefWindowProc function, which performs a default action and returns a message result. The window procedure must then return this value as its own message result. Most window procedures process just a few messages and pass the others on to the system by calling DefWindowProc.

关于c++ - 创建的窗口没有标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216797/

相关文章:

c++ - 使用opencv检测相机运动

c++ - 捕获破坏了我的 lambda 函数

c++ - unsigned char 类型的负数

windows - 如何通过扫描码(不是虚拟键码)获取键状态?

javascript - 窗口 ["object.something"] 不适用于对象 javascript

jquery - 如何在不使用 jQuery 的情况下获取元素的 offset().top 值?

c++ - 创建虚拟分区

c++ - 强制完整性级别值 0x2010 代表什么?

c++ - 如何确定特定驱动器的类型?

c++ - 将 WindowClass 更改为全屏