学习SDK中的小问题:vs2005 中使用C语言出错
大家好,学习SDK编程时,看到一本书上的一段程序,是在VC++6.0中Win32Application中写的,我用的平台是vs2005,我创建的.c文件的过程是这样的fi...
大家好,学习SDK编程时,看到一本书上的一段程序,是在VC++6.0中 Win32 Application中写的,我用的平台是vs2005,我创建的.c文件的过程是这样的file-new-project-win32 project,然后在application setting中,我选择的是windows application和empty project。然后我在这个project的source files选择add new items -c++ file(.cpp),但是最后保存这个文件的时候以.c结尾,这样创建的一个c文件,这样似乎没什么错吧,因为还有一个类似的程序我也用一样的方法创建c文件,运行都成功了,然而这个程序报错。下面是代码:
#include "stdafx.h"
#include "windows.h"
LRESULT CALLBACK WNDProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style= CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc= WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC ARROW);
wc.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if(!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Whindows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd=CreateWindow(szAppName, TEXT("欢迎你的到来!"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL);
ShowWindow (hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
LRESULT CALLBACK WNDProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("你好,欢迎你来到vc之路"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
报错Error 1 fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory e:\sdk_work\121\121\2.c 1
当我把#include "stdafx.h" 去掉后,报错更多~~
有22个错误,比如:
Error 17 error C2275: 'RECT' : illegal use of this type as an expression e:\sdk_work\121\121\2.c 49
Error 3 error C2065: 'IDC' : undeclared identifier e:\sdk_work\121\121\2.c 19
我刚学SDK,很多东西不明白,请问到底错在哪里,如何解决?
谢谢~~~
to totozii: 我确实是照书手敲的代码,呵呵,谢谢你帮我调程序,太感谢了~~~ ps:这SDK编程要记得东西好多啊,名字又长~~
to aaaa9813:也谢谢你的解答~~~ 展开
#include "stdafx.h"
#include "windows.h"
LRESULT CALLBACK WNDProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style= CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc= WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC ARROW);
wc.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if(!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Whindows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd=CreateWindow(szAppName, TEXT("欢迎你的到来!"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL);
ShowWindow (hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
LRESULT CALLBACK WNDProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("你好,欢迎你来到vc之路"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
报错Error 1 fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory e:\sdk_work\121\121\2.c 1
当我把#include "stdafx.h" 去掉后,报错更多~~
有22个错误,比如:
Error 17 error C2275: 'RECT' : illegal use of this type as an expression e:\sdk_work\121\121\2.c 49
Error 3 error C2065: 'IDC' : undeclared identifier e:\sdk_work\121\121\2.c 19
我刚学SDK,很多东西不明白,请问到底错在哪里,如何解决?
谢谢~~~
to totozii: 我确实是照书手敲的代码,呵呵,谢谢你帮我调程序,太感谢了~~~ ps:这SDK编程要记得东西好多啊,名字又长~~
to aaaa9813:也谢谢你的解答~~~ 展开
展开全部
你是手敲的代码吧。?
给你调通了。。修改的地方在注释
#include "windows.h"
LRESULT CALLBACK WNDProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style= CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc= WNDProc; // 大小写
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW); // 下划线
wc.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if(!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Whindows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
}
// TEXT("欢迎你的到来!") 后面的逗号。。中文的不对
hwnd=CreateWindow(szAppName, TEXT("欢迎你的到来!"),WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL);
ShowWindow (hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
} // 少了这个括号
LRESULT CALLBACK WNDProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps; // 拼写错误
RECT rect;
switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("你好,欢迎你来到vc之路"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER); //逗号不能用中文的
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
// 多了个括号
给你调通了。。修改的地方在注释
#include "windows.h"
LRESULT CALLBACK WNDProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style= CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc= WNDProc; // 大小写
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW); // 下划线
wc.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if(!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Whindows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
}
// TEXT("欢迎你的到来!") 后面的逗号。。中文的不对
hwnd=CreateWindow(szAppName, TEXT("欢迎你的到来!"),WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL);
ShowWindow (hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
} // 少了这个括号
LRESULT CALLBACK WNDProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps; // 拼写错误
RECT rect;
switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("你好,欢迎你来到vc之路"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER); //逗号不能用中文的
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
// 多了个括号
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询