创建的窗口没有标题 [英] Created windows have no title

查看:32
本文介绍了创建的窗口没有标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating messages windows from my console application. The window class is registered correctly and the window is created correctly however it never has a title (while my createwindow function call does specify a title). Got me thinking, can console programs create windows with name? Googled it, found nothing. This is my code, kept to the minimum :

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"),但没有名称.此外,在块中添加这些代码行:

Checking with Nirsoft's Winlister, the window exists, has the right class ("EME.LauncherWnd"), but has no name. furthermore, adding these lines of code in the block :

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

输出为

title set

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

And yet, the window still doesn't have a title. If console program couldn't have title I'd assume the SetWindowText call would return 0. What am I doing wrong ? Edit : Adding pWndProc as requested

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 中插入此代码行作为默认值

Though after the comment pointing out the pWndProc (which body i thought was irrelevant to the construction of the window), it turns out inserting this code line as a default in the switch case

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

解决问题.

推荐答案

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

I am posting the answer to the question as suggested by a comment : The answer is that for the window creation to complete, the pWndProc passed to RegisterClass WINAPI has to process default messages (in particular OS messages). During the execution of CreateWindow(after the call has started and before it has returned), the pWndProc function already receives messages that it has to process, in my case it didn't process them. This is a standard pWndProc function:

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);
         }
}

来源:

窗口过程通常不会忽略消息.如果它不处理消息,它必须将消息发送回系统进行默认处理.窗口过程通过调用 DefWindowProc 函数来执行此操作,该函数执行默认操作并返回消息结果.然后窗口过程必须返回这个值作为它自己的消息结果.大多数窗口过程只处理一些消息,并通过调用 DefWindowProc 将其他消息传递给系统.

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.

这篇关于创建的窗口没有标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆