- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下是我的部分代码。每次运行程序,SPanel的CreateWindowEx
返回NULL
,触发错误。谁能看出这段代码有什么问题?
SPanelProc
和 MainWndProc
已经声明,并且是原型(prototype) LRESULT CALLBACK SPanelProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
.
我的编程环境是 Visual C++ 2010。
为什么 CreateWindowEx
总是返回 NULL
?
#include <Windows.h>
#include <WindowsX.h>
LRESULT CALLBACK SPanelProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_MOUSEMOVE:
break;
}
return 0;
}
void Init ()
{
HWND child;
HINSTANCE hInstance = (HINSTANCE)(GetWindowLongPtr (parent, GWLP_HINSTANCE));
WNDCLASSEX wc;
const char PanelClassName[] = "SPanel"; //ClassName of the panel. Do not reuse on another window
//Create the window, initialize GWLP_USERDATA, check for errors, register the window class, and hide the window
//Create the windowclass for the button
wc.cbSize = sizeof (WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = SPanelProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor (hInstance, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = PanelClassName;
wc.hIconSm = LoadIcon (hInstance, IDI_APPLICATION);
//If could not register window class
if(!RegisterClassEx (&wc))
{
MessageBox (NULL, "Panel registration failed!", "Error!", MB_OK);
PostQuitMessage (0);
return;
}
//Create window
child = CreateWindowEx (WS_EX_NOPARENTNOTIFY, PanelClassName, "TITLE_TEXT", WS_CHILD, 0, 0, 400, 200, parent, NULL, hInstance, NULL);
//Check for error in window creation
if (child == NULL)
{
MessageBox (NULL, "Panel creation failed!", "Error!", MB_OK);
PostQuitMessage (0);
return;
}
return;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG Msg;
int length, width;
//Register the window, create the window, and check for errors
{
//Window class
wc.cbSize = sizeof (WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance; //Instance used to create the window
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_CODERICON)); //Change here to change large application icon
wc.hCursor = LoadCursor (hInstance, IDC_ARROW); //Change here to change application cursor
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //Change here to change starting background color (white)
wc.lpszMenuName = NULL;
wc.lpszClassName = Coder::Constants::MAINWNDCLASSNAME;
wc.hIconSm = (HICON)LoadImage (hInstance, MAKEINTRESOURCE (IDI_CODERICON), IMAGE_ICON, GetSystemMetrcs (SM_CXSMICON), GetSystemMetrics (SM_CYSMICON), 0); //Change here to change application icon (small, as in taskbar)
//Register the window class so that the ClassName can be used to create the window and then check for error
if (!RegisterClassEx (&wc))
{
MessageBox (NULL, "Main window registration failed!", "Error!", MB_OK);
return 0;
}
//Get screen dimensions
length = GetSystemMetrics (SM_CXSCREEN);
width = GetSystemMetrics (SM_CYSCREEN);
//Create window
hWnd = CreateWindowEx (WS_EX_CLIENTEDGE, /*Border*/
Coder::Constants::MAINWNDCLASSNAME,
Coder::Constants::MAINWNDTITLETEXT, /*Title of window*/
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN, /*Window style*/
(length - Coder::Constants::XDEFAULT - Coder::Constants::XSHIFT) / 2, (width - Coder::Constants::YDEFAULT - Coder::Constants::YSHIFT) / 2, /*Top-left x and y coordinates*/
Coder::Constants::XDEFAULT + Coder::Constants::XSHIFT, Coder::Constants::YDEFAULT + Coder::Constants::YSHIFT, /*Lengths and widths of window*/
NULL, NULL,
hInstance, NULL);
//Check for error in window creation
if (hWnd == NULL)
{
MessageBox (NULL, "Main window creation failed!", "Error!", MB_OK);
return 0;
}
}
//Call function
Init (hWnd);
//Show and redraw the window
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
//Put window in message loop
while (GetMessage (&Msg, NULL, 0, 0) > 0) //While not terminated
{
TranslateMessage (&Msg);
DispatchMessage (&Msg);
}
//Return
return static_cast<int>(Msg.wParam);
}
最佳答案
每个窗口过程必须在每条消息上返回 DefWindowProc(hwnd,msg,wparam,lparam)
,WM_ERASEBKGND
和 WM_PAINT
等消息除外> 当您使用自定义绘制程序时(您必须返回 0,这样操作系统就不会使用默认程序重新绘制窗口)
关于c++ - CreateWindowEx 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105507/
我正在用 msvc 和 C++ 做一些测试。我正在尝试使用 ResEdit 创建对话框(可在此处获得 http://www.resedit.net/ ),当创建 .RC 文件时,我正在尝试使用相同的
我有一个父窗口和一些附加到它的子窗口。使用 SpyXX,我可以看到所有的子项都有特定的样式类,比如说 ChildWindowClass。 当我用这个特定类的名称创建窗口时,CreateWindowEx
我有一个问题。我想记录我的工具开发,所以我不想在屏幕截图上绘制日期,而是想让窗口名称带有日期和时间数据。但我得到的不是字符串,而是中文字符。 这是我要将字符串分配给 CreateWindowEx()
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在学习 NeHe gamedev 教程(同时将它们更改为面向对象),但我遇到了 CreateWindowEx 演示 (http://nehe.gamedev.net/tutorial/creati
使用 C#,我很容易获得我想要的效果: 但是,我在使用 C 中的 Win32 API 做同样的事情时遇到了麻烦。我不知道如何创建一个没有图标的窗口(根本没有),但仍然有标题、最小化按钮和关闭按钮。 我
我想制作一个按钮,当您转到“我的电脑”->“帮助”->“关于 Windows”时,它看起来就像“确定”按钮。 我正在创建一个这样的按钮: hButton = CreateWindowEx(WS_EX_
好的,所以首先概述一下我正在尝试做的事情.. 我正在尝试在主父窗口中创建 3 个子窗口,并使用 EnumChildWindow 和 EnumChildProc 来枚举它们,我想创建 3 个与父窗口高度
我试图找出为什么我的应用程序有一个小的、很少像素的边框。 我的理解是 WS_POPUP 应该解决这个问题; DWORD wndExStyle = WS_EX_OVERLAPPEDWINDOW; DWO
CreateWindowEx API 真的会发送 WM_SIZE 消息吗? 当我通过 CreateWindowEx 创建一个窗口作为全屏模式时, CreateWindowEx 发布 WM_SIZE 但
我使用 CreateWindowEx 创建我的窗口,但我只在任务栏中得到一个图标;窗口的标题栏和当我 Alt+Tab 时显示的图标都显示这种类型的虚拟图标: .我将 WNDCLASSEX 中的 hIc
我使用 CreateWindowEx 创建了一个窗口,它使用以下代码作为向导对话框; DWORD dwStyle = WS_DLGFRAME | DS_CENTER | WS_POPUP | WS_C
我知道 ES_NUMBER 的存在是为了将 CreateWindowEx 限制为仅数字输入,是否有类似的机制将其限制为仅字母数字 (a-z,0-9) 输入?或者另一种方法来做类似的事情。我知道我可以事
我想使用 winAPI 创建一个窗口: int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,
我正在尝试创建一个具有特定类类型(“VSDebugger CDataTipWnd”)的窗口,但是,当我运行 CreateWindowEx 时它失败了,并且 GetLastError 告诉我找不到该类。
我正在尝试使用 C++ 设置一个简单的窗口,但我对 CreateWindowEx 的调用返回了 NULL。我使用的大部分代码来自 example在 MSDN 网站上。我尝试过的所有方法均无效,如有任何
以下是我的部分代码。每次运行程序,SPanel的CreateWindowEx返回NULL,触发错误。谁能看出这段代码有什么问题? SPanelProc 和 MainWndProc 已经声明,并且是原型
我正在尝试使用 CreateWindowEx 生成仅消息窗口: _hWnd = CreateWindowEx( 0, NULL, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE,
以下代码在Window 7上运行,但是在Windows XP上运行时,它失败并显示Windows返回的错误消息:“系统找不到指定的文件”。 #include #include #include
我想创建一个新类,然后显示一个包含指定类的窗口。我为此编写了以下代码: namespace WindowsFormsApplication1 { public partial clas
我是一名优秀的程序员,十分优秀!