下面c++代码问题好多。帮我改下、、
#include "windows.h"
BOOL InitWindow(HINSTANCE hInstance,int nCmdShow);
LERSULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR IpCmdLine,int nCmdShow)
{
MSG msg;
if(! InitWindow(hInstance,nCmdShow))
return 0;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
static BOOL InitWindow(HINSTANCE hInstance,int nCmdShow)
{ HWND hWnd;
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName =NULL;
wc.lpszClassName ="hello vc++";
if(! RegisterClass(&wc))
{
MessageBox(NULL,"窗口注册失败 !","hello,vc++",0);
return false;
}
hWnd=CreateWindow(
"hello vc++",
"我的窗口",
WS_OVERLAPPEDWINDOW,
100,
100,
400,
300,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)return false;
ShoWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return true;
}
LERSULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,
LPARAM lParam)
{
char hello[]="你好,我是VC++";
switch(message)
{
case WM_CREATE:
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
MessageBox(hWnd,"ESC键按下!","keyboard",MB_OK);
break;
}
break;
case WM_LBUTTONDOWN:
MessageBox(hWnd,"鼠标左键按下!","Mouse",0);
return 0;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc=BeginPaint(hWnd,&ps);
SetTextColor(hdc,RGB(0,0,255));
TextOut(hdc,20,10,hello,strlen(hello));
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
答案:一共有4处错误,
两处把函数类型写错了,你写成了LERSULT,本来是LRESULT。
一处函数名写错了,ShowWindow()你写成了ShoWindow()。
还有窗口类初始化的时候,要确保每个值都可预料,你只给其中的几个值赋值了,其他值都不知道,其运行结果是不可预料的,所以程序无法注册窗口类。可以将窗口类所有的元素都赋值,或者用memset函数清零,详见注释。下附可运行程序。
#include<windows.h>
BOOL InitWindow(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //你写成了LERSULT
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR IpCmdLine,int nCmdShow)
{
MSG msg;
if(! InitWindow(hInstance,nCmdShow))
return 0;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
static BOOL InitWindow(HINSTANCE hInstance,int nCmdShow)
{ HWND hWnd;
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS)); //新加的一行,用于把其他的不赋值元素清零,否则结果不可预料
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName =NULL;
wc.lpszClassName ="hello vc++";
if(!RegisterClass(&wc))
{
MessageBox(NULL,"窗口注册失败 !","hello,vc++",0);
return false;
}
hWnd=CreateWindow(
"hello vc++",
"我的窗口",
WS_OVERLAPPEDWINDOW,
100,
100,
400,
300,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)return false;
ShowWindow(hWnd,nCmdShow);//你写成了ShoWindow
UpdateWindow(hWnd);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam, //你写成了LERSULT
LPARAM lParam)
{
char hello[]="你好,我是VC++";
switch(message)
{
case WM_CREATE:
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
MessageBox(hWnd,"ESC键按下!","keyboard",MB_OK);
break;
}
break;
case WM_LBUTTONDOWN:
MessageBox(hWnd,"鼠标左键按下!","Mouse",0);
return 0;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc=BeginPaint(hWnd,&ps);
SetTextColor(hdc,RGB(0,0,255));
TextOut(hdc,20,10,hello,strlen(hello));
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}你的错误是什么?看这段代码应该是很多window编程都有涉及的,而且第一章一般就是教你如果创建一个win32 窗体然后怎么实现消息循环,这个东西建议你自己对下书,网上也有,百度一下很多的,你没有给出错误问题,一行行给你看代码很痛苦的
上一个:求几本C++相关的游戏编程的书籍?
下一个:能不能帮我分析一个题目c++的题目