读书人

在vs中编译c语言win32应用程序失误

发布时间: 2013-06-26 14:29:32 作者: rapoo

在vs中编译c语言win32应用程序出错
就是一个创建窗口的程序



#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
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_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Weixin2003";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd=CreateWindow("Weixin2003","北京维新科学技术培训中心",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 ahwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(ahwnd,szChar,"weixin",0);
break;
case WM_LBUTTONDOWN:
MessageBox(ahwnd,"mouse clicked","weixin",0);
HDC hdc;
hdc=GetDC(ahwnd);
TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
ReleaseDC(ahwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(ahwnd,&ps);
TextOut(hDC,0,0,"维新培训",strlen("维新培训"));
EndPaint(ahwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(ahwnd,"是否真的结束?","weixin",MB_YESNO))
{
DestroyWindow(ahwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(ahwnd,uMsg,wParam,lParam);
}
return 0;
}





出现的第一个错误

1>d:\编程\2222\2222\222.c(31): error C2275: “HWND”: 将此类型用作表达式非法

在vc++6.0是可以编译的,搬过来vs2010就不行了,求解。。





[解决办法]
把扩展名改为CPP
[解决办法]
2楼正解,C语言中变量只能在函数的开头部分定义,不能在代码段中临时定义。
[解决办法]
"Weixin2003"
改为
_T("Weixin2003")或L"Weixin2003"
其它字符串常量同上。

读书人网 >C++

热点推荐