【发布时间】:2017-07-02 04:34:33
【问题描述】:
我正在从我的控制台应用程序创建消息窗口。窗口类已正确注册并且窗口已正确创建,但它从来没有标题(而我的 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”),但没有名称。 此外,在块中添加这些代码行:
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);
解决问题。
【问题讨论】:
-
"控制台程序可以创建带有名称的窗口吗?" - 当然可以。毕竟,控制台本身只是一个普通的 Win32 应用程序。控制台应用程序可以完全访问 Win32 API。话虽如此,
pWndProc究竟指向什么,它是否正确处理窗口消息? -
你需要一个按摩环
-
投票结束,因为缺乏可重复的例子。
-
您是否将所有未处理的消息传递给
DefWindowProc()?