我新手,建了一个WIN32的项目,高手帮我看看,是什么问题,
出现两个错误如下
 error C3861: “OnPaint”: 即使使用参数相关的查找,也未找到标识符
 error C2365: “OnPaint” : 重定义;以前的定义是“原先未知的标识符”源代码如下
#include<windows.h>
HINSTANCE hInst;
HINSTANCE hInstance;
MSG msg;
char lpszClassName[]="window_class";
char *ShowText;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam, LPARAM lParam);
void OnDestory(HWND hWnd,UINT message,WPARAM wParam, LPARAM lParam);
class CFrameWnd
{
public:
HWND hWnd;
public:
int RegisterWindow();
void Create(LPCTSTR lpClassName,LPCTSTR lpWindowName)
;
void ShowWindow(int nCmdShow);
void UpdateWindow();
};
int CFrameWnd::RegisterWindow ()
{
WNDCLASS wc;
wc.style =0;
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=lpszClassName;
return RegisterClass(&wc);
}
void CFrameWnd::Create(LPCTSTR lpClassName,LPCTSTR lpWindowName)
{
RegisterWindow();
hInst=hInstance;
hWnd=CreateWindow(lpszClassName,
lpWindowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
}
void CFrameWnd::ShowWindow (int nCmdShow)
{
::ShowWindow(hWnd,nCmdShow);}
void CFrameWnd::UpdateWindow ()
{
::UpdateWindow (hWnd);}
class CWinApp{
public:
CFrameWnd*m_pMainWnd;
public:
BOOL InitInstance(int nCmdShow);
int Run();
~CWinApp();
};
BOOL CWinApp::InitInstance (int nCmdShow)
{
m_pMainWnd=new CFrameWnd;
m_pMainWnd->Create (NULL,"封装的Windows程序");
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
CWinApp::~CWinApp (){delete m_pMainWnd;}
CWinApp theApp;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow)
{
int ResultCode=-1;
theApp.InitInstance (nCmdShow);
return ResultCode=theApp.Run();}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
OnPaint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
OnDestory(hWnd,message,wParam,lParam);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;

}
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
ShowText="hello!";
InvalidateRect(hWnd,NULL,1);
}
void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,50,50,ShowText,6);
EndPaint(hWnd,&ps);
}
void OnDestory(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
}

解决方案 »

  1.   

    void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
    放到class CFrameWnd前声明下。
      

  2.   

    没有声明OnPaint函数,把以下放到WinMain之前void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
      

  3.   

    那是因为你在调用OnPaint函数之前没有声明它,所以报错,你在WndProc函数里调用了它,但是你并没有声明它,所以在程序最前面加一个OnPaint的声明。
    void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);