C++新手,请帮我看看这段代码,问题出在哪里

2024-12-21 16:15:56
推荐回答(5个)
回答1:

不少书写错误和一些用法错误,你把下面这段代码试试,在VC6.0下能通过。
特别指出:
CreateWindow("zhaoyao","hubeiren",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);//第一个参数要和你注册窗口名一致,改为"zhaoyao2003",否则createwindows会失败!

注意,建工程时使用win32 application
#include
#include

LRESULT CALLBACK WindSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{

WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindSunProc;
wndcls.lpszClassName= "zhaoyao2003";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;

if(!RegisterClass(&wndcls))return 0;

HWND hwnd;
hwnd=CreateWindow("zhaoyao2003","hubeiren",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return 0;
}

LRESULT CALLBACK WindSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"zhaoyao",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","zhaoyao",0);
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"湖北人学习",strlen("湖北人学习"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:

PAINTSTRUCT ps;
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"zhaoyaopeixun",strlen("zhaoyaopeixun"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","zhaoyao",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

回答2:

#include 的下面加
main()

回答3:

int winMain{

这个地方少了()

回答4:

改成这样如果还有问题HI我

#include
#include

LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="zhaoyao2003";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd;
hwnd=CreateWindow("zhaoyao","hubeiren",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg,NULL,0,0));
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WindSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{ //这里写错了,不应该有int winmain
HDC hdc = NULL;
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"zhaoyao",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","zhaoyao",0);
hdc=GetDC(hwnd); //是hwnd不是hWnd
TextOut(hdc,0,50,"湖北人学习",strlen("湖北人学习"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
PAINTSTRUCT ps; //PAINTSTRUCT要大写
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"zhaoyaopeixun",strlen("zhaoyaopeixun"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","zhaoyao",MB_YESNO));
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam); //这里只有四个参数,不要加最后的0
}
return 0;
}

回答5:

一个程序只有一个MAIN函数,你怎么加两个啊?

if(IDYES==MessageBox(hwnd,"是否真的结束?","zhaoyao",MB_YESNO));
{
DestroyWindow(hwnd);
}

你的if 后面多了个";"号

将你
LRESULT CALLBACK WinSunProc声明复制到定义处!替换原来的!