WinMain/Win32窗口未显示,但显示在任务管理器的“进程”选项卡中 我是C++新手,我坚持显示窗口。我没有收到任何错误,但是我的窗口没有显示在桌面上。当我打开任务管理器时,它显示在“进程”选项卡下。我还没有找到任何解决这个问题的方法,所以非常感谢您的帮助。谢谢!:)
**注意:我正在使用Microsoft Visual Studio 2012 **注意:不是C++的新手,而是创建Win32应用程序的更多内容WinMain/Win32窗口未显示,但显示在任务管理器的“进程”选项卡中 我是C++新手,我坚持显示窗口。我没有收到任何错误,但是我的窗口没有显示在桌面上。当我打开任务管理器时,它显示在“进程”选项卡下。我还没有找到任何解决这个问题的方法,所以非常感谢您的帮助。谢谢!:),c++,winapi,visual-studio-2012,winmain,C++,Winapi,Visual Studio 2012,Winmain,**注意:我正在使用Microsoft Visual Studio 2012 **注意:不是C++的新手,而是创建Win32应用程序的更多内容 #include <Windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> static TCHAR WindowClass[] = L"Window"; LRESULT CALLBACK WindowProc(
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
static TCHAR WindowClass[] = L"Window";
LRESULT CALLBACK WindowProc(
HWND WinH,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (Msg)
{
PAINTSTRUCT pntStruct;
static HDC hdc;
case WM_PAINT:
{
BeginPaint(
WinH,
&pntStruct
);
TextOut(hdc,
5, 5,
L"Hello, World!", _tcslen(L"Hello, World!"));
//pntStruct.rcPaint
EndPaint(
WinH,
&pntStruct
);
} break;
case WM_SIZE:
{
} break;
case WM_MOVE:
{
} break;
case WM_DESTROY:
{
} break;
case WM_CLOSE:
{
} break;
default:
{
return DefWindowProc(WinH, Msg, wParam, lParam);
} break;
case WM_ACTIVATEAPP:
{
if (WM_ACTIVATEAPP)
{
OutputDebugStringA("WM_ACTIVEAPP->TRUE");
}
else
{
OutputDebugStringA("WM_ACTIVEAPP->FALSE");
}
} break;
}
return 0;
};
int WINAPI WinMain(
HINSTANCE Window,
HINSTANCE PrevInstance,
LPSTR Cmd,
int CmdShow
)
{
WNDCLASSEX wclass;
//wclass.cbSize = sizeof(WNDCLASS);
wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wclass.lpfnWndProc = WindowProc;
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hInstance = Window;
//wclass.hIcon; TODO: CREATE ICON
//wclass.hCursor;
//wclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wclass.lpszMenuName = NULL;
wclass.lpszClassName = (LPCTSTR)WindowClass;
// HICON hIconSm;
RegisterClassEx(&wclass);
HWND CreateWin = CreateWindow(
WindowClass,
L"NAME OF WINDOW",
WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,//WIDTH:[TODO]->Make custom width to fit window
CW_USEDEFAULT,//HEIGHT:[TODO]->Make custom width to fit window
0,
0,
Window,
0
);
ShowWindow(CreateWin, CmdShow);
UpdateWindow(CreateWin);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
};
return 0;
};
#包括
#包括
#包括
#包括
静态TCHAR WindowClass[]=L“窗口”;
LRESULT回调WindowProc(
温文德,
味精,
WPARAM WPARAM,
LPARAM
)
{
开关(Msg)
{
PAINTSTRUCT pntStruct;
静态HDC-HDC;
案例WM_油漆:
{
开始粉刷(
嗯,
&PNT结构
);
文本输出(hdc,
5, 5,
L“你好,世界!”,(L“你好,世界!”);
//pntStruct.rcPaint
端漆(
嗯,
&PNT结构
);
}中断;
案例WM_大小:
{
}中断;
案例WM_MOVE:
{
}中断;
案例WM_销毁:
{
}中断;
案例WM_结束:
{
}中断;
违约:
{
返回DefWindowProc(WinH、Msg、wParam、lParam);
}中断;
案例WM_激活EAPP:
{
如果(WM_激活EAPP)
{
OutputDebugStringA(“WM_ACTIVEAPP->TRUE”);
}
其他的
{
OutputDebugStringA(“WM_ACTIVEAPP->FALSE”);
}
}中断;
}
返回0;
};
int-WINAPI-WinMain(
站在窗口,
例如,,
LPSTR Cmd,
int CmdShow
)
{
WNDCLASSEX WCLASSE;
//wclass.cbSize=sizeof(WNDCLASS);
wclass.style=CS|HREDRAW | CS|VREDRAW | CS|OWNDC;
wclass.lpfnWndProc=WindowProc;
wclass.cbClsExtra=0;
wclass.cbWndExtra=0;
wclass.hInstance=窗口;
//wclass.hIcon;TODO:创建图标
//wclass.hCursor;
//wclass.hbrBackground=(HBRUSH)(彩色窗口+1);
wclass.lpszMenuName=NULL;
wclass.lpszClassName=(LPCTSTR)WindowClass;
//HICON hIconSm;
RegisterClassEx(&wclass);
HWND CreateWin=CreateWindow(
WindowClass,
L“窗口名称”,
WS|U可见| WS|U重叠窗口,
CW_使用默认值,
CW_使用默认值,
CW_USEDEFAULT,//宽度:[TODO]->自定义宽度以适应窗口
CW\u usefault,//高度:[TODO]->自定义宽度以适应窗口
0,
0,
窗
0
);
ShowWindow(CreateWin、CmdShow);
更新窗口(CreateWin);
消息;
while(GetMessage(&message,NULL,0,0)>0)
{
翻译消息(和消息);
DispatchMessage(&message);
};
返回0;
};
此代码有很多错误
您正在将WindowClass
声明为TCHAR[]
,但使用wchar\u t[]
初始化它。与TextOut()
的lpString
参数相同。只有在为项目定义了UNICODE
时,这才有效,否则将出现编译器错误。使用TCHAR
时,需要使用TEXT()
宏包装字符串文字,以便它们使用正确的字符类型。否则,停止使用TCHAR
,一切都使用Unicode API。如果代码需要同时支持ANSI(Win9x/ME)和Unicode(NT4+)编译,则只需使用TCHAR
。没有人真正支持Win9x/Me了,所以新代码应该只关注Unicode API
您没有将WNDCLASSEX
结构的内容归零,因此您有意注释掉的所有字段(最重要的是cbSize
字段)都将包含堆栈中的随机值。这可能会导致RegisterClassEx()
失败,而您没有进行检查。为避免此问题,请在使用API结构之前始终将其归零。这对于随时间而增长的结构(当较新的Windows版本引入新的结构字段时)尤为重要。此类结构通常有一个cbSize
字段,因此API知道您使用的是哪个版本的结构,因此必须提供准确的值。您需要将所有未使用的字段归零,这样就不会从API中获得意外行为
您没有检查CreateWindow()
是否失败,例如RegisterClassEx()
失败的副作用
您的WindowProc()
应该将未处理的消息传递给DefWindowProc()
,但您没有这样做。大多数案例
块只是丢弃消息,因此Windows无法处理它们。这就是你真正想要的吗?我对此表示怀疑。特别是WM\u CLOSE
的DefWindowProc()
的默认行为是销毁窗口,触发WM\u destroy
消息
您的WM\u DESTROY
处理程序没有调用PostQuitMessage()
将WM\u QUIT
消息放入调用线程的消息队列,因此GetMessage()
可以返回0来中断消息循环并让应用程序退出
您的WM_PAINT
处理程序没有使用BeginPaint()
提供给您的HDC
,您正在使用未初始化的HDC
变量进行绘图
话虽如此,不妨尝试以下方式:
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h> // Or: remove this
static TCHAR WindowClass[] = TEXT("Window");
// or: static WCHAR WindowClass[] = L"Window";
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
static const TCHAR* HelloWorld = TEXT("Hello, World!");
// or: const WCHAR* HelloWorld = L"Hello, World!";
PAINTSTRUCT pntStruct = {0};
HDC hdc = BeginPaint(hWnd, &pntStruct);
TextOut(hdc, 5, 5, HelloWorld, _tcslen(HelloWorld));
// or: TextOutW(hdc, 5, 5, HelloWorld, lstrlenW(HelloWorld));
EndPaint(hWnd, &pntStruct);
break;
}
case WM_SIZE:
{
//...
break;
}
case WM_MOVE:
{
//...
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_CLOSE:
{
//...
break;
}
case WM_ACTIVATEAPP:
{
if (WM_ACTIVATEAPP)
{
OutputDebugString(TEXT("WM_ACTIVEAPP->TRUE"));
// or: OutputDebugStringW(L"WM_ACTIVEAPP->TRUE");
}
else
{
OutputDebugString(TEXT("WM_ACTIVEAPP->FALSE"));
// or: OutputDebugStringW(L"WM_ACTIVEAPP->FALSE");
}
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wclass = {0}; // Or: WNDCLASSEXW
wclass.cbSize = sizeof(wclass);
wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wclass.lpfnWndProc = &WindowProc;
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hInstance = hInstance;
wclass.hIcon = NULL; // TODO: CREATE ICON
wclass.hCursor = NULL;
wclass.hbrBackground = NULL;//(HBRUSH)(COLOR_WINDOW+1);
wclass.lpszMenuName = NULL;
wclass.lpszClassName = WindowClass;
wclass.hIconSm = NULL;
if (!RegisterClassEx(&wclass)) // Or: RegisterClassExW()
{
// error! Use GetLastError() to find out why...
return 0;
}
HWND hCreateWin = CreateWindow( // Or: CreateWindowW()
WindowClass,
TEXT("NAME OF WINDOW"), // Or: L"NAME OF WINDOW"
WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,//WIDTH:[TODO]->Make custom width to fit window
CW_USEDEFAULT,//HEIGHT:[TODO]->Make custom width to fit window
0,
0,
hInstance,
0
);
if (!hCreateWin)
{
// error! Use GetLastError() to find out why...
return 0;
}
ShowWindow(hCreateWin, nCmdShow);
UpdateWindow(hCreateWin);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
};
return 0;
};
#包括
#包括
#包括
#包括//或:删除此
静态TCHAR WindowClass[]=文本(“窗口”);
//或:静态WCHAR WindowClass[]=L“窗口”;
LRESULT回调WindowProc(HWND HWND、UINT uMsg、WPARAM WPARAM、LPARAM lPa