为什么我的Win32(c ++,Visual Studio 2013)窗口没有显示?
Why isn't my Win32(c++, visual studio 2013) Window showing up?
代码如下:
#include <windows.h>
#include <windowsx.h>
// the WindowProc callback function prototype
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
// win32 entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
///*
// window handler
HWND hwnd;
// struct for window information
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// setting wc struct with window values/properties
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "TestWindow";
wc.lpfnWndProc = WindowProc;
// register window before creation/use
RegisterClassEx(&wc);
// creating the window class
hwnd = CreateWindowEx(NULL, "TestWindow", "First Win32 Program", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);
// show the window
ShowWindow(hwnd, nCmdShow);//*/
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){
return 0;
}
下面是编译器/调试/运行时控制台输出:
'wintest.exe' (Win32): Loaded
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64ntdll.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64kernel32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64KernelBase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64user32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64gdi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64lpk.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64usp10.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcrt.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64advapi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64sechost.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64rpcrt4.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64sspicli.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64cryptbase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcr120d.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64imm32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64msctf.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:WindowsSysWOW64uxtheme.dll'. Cannot find or open the PDB file.
The program '[3484] wintest.exe' has exited with code 0 (0x0).
这是一个非常基本的简单程序,它所做的就是从左上角开始弹出一个1000x1000大小的win32窗口。
我看到的第一个错误是在您的窗口程序中。任何您没有显式处理的消息都应该传递给DefWindowProc
。
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
return DefWindowProc(hwnd, message, wparam, lparam);
}
这是窗口过程所需的绝对最小值。在现实中,您至少需要处理WM_CLOSE
和WM_DESTROY
,然后在添加功能时还要处理更多。
你的破窗过程导致CreateWindowEx
在创建过程中将WM_NCCREATE
消息发送到窗口时失败。
另一个明显的问题是根本不执行任何错误检查。忽略你所做的每个API调用的返回值,你没有任何方法来诊断你的程序在哪里失败。
最后,你没有包含消息循环。因此,即使您可以让窗口显示出来,进程也会立即终止。
程序将显示一个窗口。显然,还有更多的工作要做,但这是一个开始。
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "TestWindow";
wc.lpfnWndProc = WindowProc;
if (!RegisterClassEx(&wc))
return 1;
HWND hwnd = CreateWindowEx(0, "TestWindow", "First Win32 Program",
WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);
if (!hwnd)
return 1;
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 用于窗口的 HID 终端
- SFML 文本未绘制在窗口上
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- 为什么我的Win32(c ++,Visual Studio 2013)窗口没有显示?