注册 登录
编程论坛 C++教室

源代码问题

sunweihang 发布于 2007-07-25 20:02, 612 次点击

请看这个程序
#include<windows.h>
#include<stdio.h>

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

int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS win2007;
win2007.cbClsExtra=0;
win2007.cbWndExtra=0;
win2007.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
win2007.hCursor=LoadCursor(NULL,IDC_CROSS);
win2007.hIcon=LoadIcon(NULL,IDI_ERROR);
win2007.hInstance=hInstance;
win2007.lpfnWndProc=WinSunProc;
win2007.lpszClassName="sunweihang";
win2007.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&win2007);
HWND hwnd;
hwnd=CreateWindow("win2007","孙伟航",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 WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"swh",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","swh",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"孙伟航",strlen("孙伟航"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"孙伟航",strlen("孙伟航"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","swh",MB_YESNO))
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

编译的时候没有任何错误,可是为什么就是不能运行呢?
本人初学者,请指教!

9 回复
#2
aipb20072007-07-25 21:45

[CODE]#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS win2007;
win2007.cbClsExtra=0;
win2007.cbWndExtra=0;
win2007.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
win2007.hCursor=LoadCursor(NULL,IDC_CROSS);
win2007.hIcon=LoadIcon(NULL,IDI_ERROR);
win2007.hInstance=hInstance;
win2007.lpfnWndProc=WinSunProc;
win2007.lpszClassName="sunweihang";
win2007.lpszMenuName = NULL; //WNDCLASS 成员赋值漏了他
win2007.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&win2007);
HWND hwnd;
hwnd=CreateWindow("sunweihang","孙伟航",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 WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"swh",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","swh",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"孙伟航",strlen("孙伟航"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"孙伟航",strlen("孙伟航"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","swh",MB_YESNO))
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}[/CODE]

不是很了解api,还好你这个简单。

#3
sunweihang2007-07-26 16:03

太感谢了
我这就回去试验一下~~~
对了,请问我安装的c++6.0是那种解压缩的,有没有那种硬盘安装版的
还有在那里能够下载2001版的msdn
谢谢了

#4
sunweihang2007-07-26 16:07
没有试验呢,大概看了一下,有个小问题

#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS win2007; 请问这里不是在定义我的类名吗?
win2007.cbClsExtra=0;
win2007.cbWndExtra=0;
win2007.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
win2007.hCursor=LoadCursor(NULL,IDC_CROSS);
win2007.hIcon=LoadIcon(NULL,IDI_ERROR);
win2007.hInstance=hInstance;
win2007.lpfnWndProc=WinSunProc;
win2007.lpszClassName="sunweihang";
win2007.lpszMenuName = NULL; //WNDCLASS 成员赋值漏了他
win2007.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&win2007);
HWND hwnd;
hwnd=CreateWindow("sunweihang","孙伟航",WS_OVERLAPPEDWINDOW, //要与你上面的类名相同” 难道我的类名不是“win2007”吗
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 WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"swh",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","swh",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"孙伟航",strlen("孙伟航"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"孙伟航",strlen("孙伟航"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","swh",MB_YESNO))
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
#5
aipb20072007-07-26 16:08

baidu下吧,应该有的。

现在的vc下载都是镜像文件吧,可以用虚拟光去安装,也可以直接解压安装。

#6
sunweihang2007-07-26 19:44
再次感谢,我全部都试验过了,你的解答完全正确!~~
非常感谢
#7
sunweihang2007-07-26 19:46
还有个小问题

#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinSunProc 这里与下面的红字部分,我不是很了解,那位高手能给
( 解答一下
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS win2007;
win2007.cbClsExtra=0;
win2007.cbWndExtra=0;
win2007.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
win2007.hCursor=LoadCursor(NULL,IDC_CROSS);
win2007.hIcon=LoadIcon(NULL,IDI_ERROR);
win2007.hInstance=hInstance;
win2007.lpfnWndProc=WinSunProc;
win2007.lpszClassName="sunweihang";
win2007.lpszMenuName = NULL; //WNDCLASS 成员赋值漏了他
win2007.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&win2007);
HWND hwnd;
hwnd=CreateWindow("sunweihang","孙伟航",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 WinSunProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"swh",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","swh",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"孙伟航",strlen("孙伟航"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"孙伟航",strlen("孙伟航"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","swh",MB_YESNO))
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
#8
aipb20072007-07-26 21:44
回复:(sunweihang)还有个小问题#include
简单来说就是个函数声明。

ex

fun();//声明函数fun

int main(){
fun();
//
}

fun(){
//
}
就这个意思,要在main函数中用它就要先让编译器看见他
#9
sunweihang2007-07-27 15:59
谢谢,明白了
非常感谢 ~~~
#10
野比2007-07-28 18:52
去迅雷下.. 要184MB的rar版... 400多MB那个就是这个解压出来的, 不要下错了..
1