我一直在尝试编写一个适用于windows API的基本程序(WinMain和WndProc),现在我已经完成了4个教程,所有这些教程都说了同样的话。我创建了前面提到的两个重要函数,但是当我编译和运行时,没有显示任何窗口。
我没有得到错误或崩溃,程序运行良好,只是窗口应该出现,但没有出现。
任何帮助都是很好的,我已经尝试在VS2010中使用Win32控制台项目设置、Win32项目设置和空项目设置。
谢谢。
编辑:抱歉,这是我用来设置和显示窗口的代码:
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(WNDCLASS));
wcex.cbSize = sizeof(WNDCLASS);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
HWND hWnd = CreateWindowEx(NULL, "MyWindowClass", // Name of window class
"Window Name", // Title of window
WS_OVERLAPPEDWINDOW, // Window style
300, 500, // x,y position of window
800, 600, // w,h of window
NULL, // Parent window
NULL, // Menus
hInstance, // Application handle
NULL); // Multiple windows
ShowWindow(hWnd, nCmdShow);发布于 2011-02-08 01:38:54
ZeroMemory(&wcex, sizeof(WNDCLASS));
wcex.cbSize = sizeof(WNDCLASS)上面的参数应该是WNDCLASSEX,以便与它们上面定义的WNDCLASSEX结构相匹配。
发布于 2011-02-08 02:09:42
wcex.lpfnWndProc = (WNDPROC)WndProc;这是一个非常可疑的演员阵容,永远不应该有这个必要。这个问题可以通过一个截取的窗口过程来解释。你没有把它贴出来。从删除强制转换开始,并解决您得到的任何编译错误。确保它总是为您自己不处理的消息调用DefWindowProc()。
考虑使用从选择Win32项目模板中获得的样板代码来正确获取这些详细信息。
发布于 2011-02-08 01:43:10
这里有一个非常棒的介绍,介绍如何构建您的第一个Win32应用程序available on MSDN。
在此之后,您不会走得太远(本系列中的“下一步”链接将开始教您有关Windows窗体的知识,这很有趣,但与您的目标无关)。但是,如果您的目标只是了解样板文件是什么,那么这是一个很好的地方。
或者,正如我在评论中提到的,您应该能够在Visual Studio中创建一个新的Win32项目,并自动为您插入所有基本内容。当然,然后你会有一场理解它的战斗,但这不是你在这里要问的。:-)
以下是该教程的基本框架:
int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
hInst = hInstance; // Store instance handle in our global variable
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application dows not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); // send the window a WM_PAINT message
// Main message loop:
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
MessageBox(NULL,
_T("Error encountered in message loop!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}https://stackoverflow.com/questions/4924305
复制相似问题