我正在编写一个C++程序,我想在其中使用CreateWindow()函数创建一个窗口,但我无法让它工作。我无法编译该程序,Visual Studio在错误列表中给我的唯一信息是“不允许使用类型名”。我该如何解决这个问题?我还不能自己决定如何修复它。以下是该程序的代码:
#include "stdafx.h"
#include <Windows.h>
int main()
{
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND window = CreateWindow("Melter", NULL, WS_POPUP, 0, 0, screenWidth, screenHeight, HWND_DESKTOP, NULL, HINSTANCE, NULL);
return 0;
}发布于 2016-01-08 16:57:45
要从控制台应用程序创建窗口,您有很多事情要做。首先,你必须通过RegisterClass注册你自己的窗口类,带有样式参数,一个模块句柄,最重要的是一个窗口过程。您可以通过GetModuleHandle(0)获取模块句柄,它返回用于创建调用进程的文件的句柄。你必须定义你自己的窗口程序。这是一个处理发送到窗口的消息的函数。有了这个窗口类和模块句柄,您就可以使用CreateWindow创建您的窗口。在你的窗口被创建之后,你必须用ShowWindow显示它。最后,您需要为您的窗口创建一个消息循环:
#include <Windows.h>
// Window procedure which processes messages sent to the window
LRESULT CALLBACK WindowProcedure( HWND window, unsigned int msg, WPARAM wp, LPARAM lp )
{
switch(msg)
{
case WM_DESTROY: PostQuitMessage(0); return 0;
default: return DefWindowProc( window, msg, wp, lp );
}
}
int main()
{
// Get module handle
HMODULE hModule = GetModuleHandle( 0 );
if (!hModule)
return 0;
// Register window class
const char* const myWindow = "MyWindow" ;
//const wchar_t* const myWindow = L"MyWindow"; // unicode
WNDCLASS myWndClass = {
CS_DBLCLKS, WindowProcedure, 0, 0, hModule,
LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW),
CreateSolidBrush(COLOR_WINDOW+1), 0, myWindow };
if ( !RegisterClass( &myWndClass ) )
return 0;
// Create window
int screenWidth = GetSystemMetrics(SM_CXSCREEN)/2;
int screenHeight = GetSystemMetrics(SM_CYSCREEN)/2;
HWND window = CreateWindow( myWindow, NULL, WS_OVERLAPPEDWINDOW, 0, 0, screenWidth, screenHeight, HWND_DESKTOP, NULL, hModule, NULL);
if( !window )
return 0;
// Show window
ShowWindow( window, SW_SHOWDEFAULT );
// Message loop
MSG msg ;
while( GetMessage( &msg, 0, 0, 0 ) )
DispatchMessage(&msg);
return 0;
}https://stackoverflow.com/questions/34668993
复制相似问题