主题:求助!为什么我的消息处理函数会出错?
求助!为什么我的消息处理函数会出错?
一个在窗体上绘制正弦曲线的程序,不知道为什么消息处理函数总是不对,因为是自学,也不知道是哪里的问题~请大家指点迷津~
程序如下:
// a.cpp : Defines the entry point for the application.
//
LRESULT _stdcall WndProc(HWND,UNIT,WPARAM,LPARAM) //消息处理函数声明
#include "stdafx.h"
#include "math.h"
int APIENTRY WinMain(HINSTANCE hInstance, //构建窗体
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
char szClassName[]="MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=MainWndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=::LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);
wndclass.hbrbackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szClassName;
wndclass.hIconSm=NULL;
::RegisterClassEx(&wndclass);
HWND hwnd=::CreateWindowEx(0,szClassName,"My first Window!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(hwnd==NULL)
{
::MessageBox(NULL,"创建窗口出错!","error",MB_OK);
return -1;
}
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
MSG msg;
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
//以下为消息处理函数:
LRESULT _stdcall WndProc(HWND hWnd,UNIT message,WPARAM wParam,LPARAM lParam)
{
#define SEGMENTS 500
#define PI 3.1415926
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
int cxClient,cyClient;
POINT pt[SEGMENTS];
int i;
switch(message)
{
case WM_PAINT:
hdc=::BeginPaint(hWnd,&ps);
::GetClientRect(hWnd,&rt);
cxClient=rt.right-rt.left;
cyClient=rt.bottom-rt.top;
::MoveToEx(hdc,0,cyClient/2,NULL);
::LineTo(hdc,cxClient,cyClient/2);
for(i=0;i<SEGMENTS;i++)
{
pt.x=cxClient*i/SEGMENTS;
pt.y=(int)((cyClient/2)*(1-sin(2*PI*i/SEGMENTS)));
}
::Polyline(hdc,pt,SEGMENTS);
::EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
return ::DefWindowProc(hWnd,message,wParam,lParam);
}
===========================
以下为调试信息:
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
C:\my works\a\a.cpp(4) : error C2143: syntax error : missing ';' before '__stdcall'
C:\my works\a\a.cpp(4) : error C2501: 'LRESULT' : missing storage-class or type specifiers
C:\my works\a\a.cpp(4) : error C2065: 'HWND' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'UNIT' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'WPARAM' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'LPARAM' : undeclared identifier
d:\program files\microsoft visual studio\vc98\include\excpt.h(36) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
d:\program files\microsoft visual studio\vc98\include\excpt.h(36) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
a.obj - 8 error(s), 0 warning(s)
一个在窗体上绘制正弦曲线的程序,不知道为什么消息处理函数总是不对,因为是自学,也不知道是哪里的问题~请大家指点迷津~
程序如下:
// a.cpp : Defines the entry point for the application.
//
LRESULT _stdcall WndProc(HWND,UNIT,WPARAM,LPARAM) //消息处理函数声明
#include "stdafx.h"
#include "math.h"
int APIENTRY WinMain(HINSTANCE hInstance, //构建窗体
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
char szClassName[]="MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=MainWndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=::LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);
wndclass.hbrbackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szClassName;
wndclass.hIconSm=NULL;
::RegisterClassEx(&wndclass);
HWND hwnd=::CreateWindowEx(0,szClassName,"My first Window!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(hwnd==NULL)
{
::MessageBox(NULL,"创建窗口出错!","error",MB_OK);
return -1;
}
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
MSG msg;
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
//以下为消息处理函数:
LRESULT _stdcall WndProc(HWND hWnd,UNIT message,WPARAM wParam,LPARAM lParam)
{
#define SEGMENTS 500
#define PI 3.1415926
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
int cxClient,cyClient;
POINT pt[SEGMENTS];
int i;
switch(message)
{
case WM_PAINT:
hdc=::BeginPaint(hWnd,&ps);
::GetClientRect(hWnd,&rt);
cxClient=rt.right-rt.left;
cyClient=rt.bottom-rt.top;
::MoveToEx(hdc,0,cyClient/2,NULL);
::LineTo(hdc,cxClient,cyClient/2);
for(i=0;i<SEGMENTS;i++)
{
pt.x=cxClient*i/SEGMENTS;
pt.y=(int)((cyClient/2)*(1-sin(2*PI*i/SEGMENTS)));
}
::Polyline(hdc,pt,SEGMENTS);
::EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
return ::DefWindowProc(hWnd,message,wParam,lParam);
}
===========================
以下为调试信息:
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
C:\my works\a\a.cpp(4) : error C2143: syntax error : missing ';' before '__stdcall'
C:\my works\a\a.cpp(4) : error C2501: 'LRESULT' : missing storage-class or type specifiers
C:\my works\a\a.cpp(4) : error C2065: 'HWND' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'UNIT' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'WPARAM' : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: 'LPARAM' : undeclared identifier
d:\program files\microsoft visual studio\vc98\include\excpt.h(36) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
d:\program files\microsoft visual studio\vc98\include\excpt.h(36) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
a.obj - 8 error(s), 0 warning(s)