穿拖鞋的兔子 大约12小时前 平静 的说 翘课   沙漠★寂寞 11月30日 平静 的说 机电那伙人又开始分钱了,刚刚又赚了很,见者有份,速度去抢!!!   一千年胡杨 11月30日 平静 的说 我爱你   穿拖鞋的兔子 11月30日 平静 的说 鼻子冻的红红的,不想上校内   鲍豪斯 11月30日 平静 的说 机电那伙正在分钱..要的都去看要吧!!!   小鱼游 11月30日 悲伤 的说 俺消失咧将近一整天,都米有个人想我   zdk6105 11月30日 无聊 的说 风好大,期待一场雪:天好冷,渴望一场爱   jacky200247 11月30日 平静 的说 输入要叽歪的内容_   农水007 11月30日 平静 的说 不生气,不发火,我难过,她的世界就会崩溃,就算为了她吧!   zhl2008 11月29日 生气 的说 哪 个鸟人 把我的头衔 签名给我搞没了.真是太气人了.发个牢骚还要凑够5个帖子.不仁道的家伙,好想骂你.   [查看全部 448 条唧唧歪歪...]


打印

求助!为什么我的消息处理函数会出错?

求助!为什么我的消息处理函数会出错?

  一个在窗体上绘制正弦曲线的程序,不知道为什么消息处理函数总是不对,因为是自学,也不知道是哪里的问题~请大家指点迷津~
程序如下:
[box=white]

// 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 &#39;;&#39; before &#39;__stdcall&#39;
C:\my works\a\a.cpp(4) : error C2501: &#39;LRESULT&#39; : missing storage-class or type specifiers
C:\my works\a\a.cpp(4) : error C2065: &#39;HWND&#39; : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: &#39;UNIT&#39; : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: &#39;WPARAM&#39; : undeclared identifier
C:\my works\a\a.cpp(4) : error C2065: &#39;LPARAM&#39; : undeclared identifier
d:\program files\microsoft visual studio\vc98\include\excpt.h(36) : error C2448: &#39;<Unknown>&#39; : 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)


[/box]

TOP

你写的是SDK的程序
wndclass.lpfnWndProc=MainWndProc;
这个应该写的是消息循环函数的名字WndProc

LRESULT _stdcall WndProc(HWND,UNIT,WPARAM,LPARAM)  //消息处理函数声明
写在#include 后面
还要加分号

问题太多了
http://blog.csdn.net/thisisll/ http://spaces.msn.com/thisisll/

TOP

干脆我贴一个SDK的经典教程你看看吧
http://blog.csdn.net/thisisll/ http://spaces.msn.com/thisisll/

TOP

是啊 同意菠菜说的  起码INCLUDE应该放在函数声明前面嘛,要不然这些类型都是未定义,就出现undeclared identifier错误了

TOP

引用:
LRESULT _stdcall WndProc(HWND,UNIT,WPARAM,LPARAM)  //消息处理函数声明
写在#include 后面
呵呵,很不好意思~~~贴的时候没有注意,现眼了~~~~

TOP

现在呢?哪儿的问题?

[box=white]#include "stdafx.h"
#include "math.h"
#include <windows.h>
#include <stdio.h>
// a.cpp : Defines the entry point for the application.
//



LRESULT _stdcall WndProc(HWND,UNIT,WPARAM,LPARAM);



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=WndProc;
    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(10) : error C2061: syntax error : identifier &#39;UNIT&#39;
C:\my works\a\a.cpp(24) : error C2440: &#39;=&#39; : cannot convert from &#39;long (__stdcall *)(struct HWND__ *)&#39; to &#39;long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)&#39;
      This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\my works\a\a.cpp(52) : error C2061: syntax error : identifier &#39;UNIT&#39;
C:\my works\a\a.cpp(62) : error C2065: &#39;message&#39; : undeclared identifier
C:\my works\a\a.cpp(83) : error C2065: &#39;wParam&#39; : undeclared identifier
C:\my works\a\a.cpp(83) : error C2065: &#39;lParam&#39; : undeclared identifier
Error executing cl.exe.

a.obj - 6 error(s), 0 warning(s)
[/box]

TOP

error C2061: syntax error : identifier &#39;UNIT&#39;  写错了,UINT,unsigned int 的意思。第2个错误是由这个错误引起的

TOP

唉  下面的错误全是由这个手误造成的。。。。

TOP

引用:
下面是引用vikey于2005-10-21 14:23发表的:
error C2061: syntax error : identifier &#39;UNIT&#39;  写错了,UINT,unsigned int 的意思。第2个错误是由这个错误引起的
酱子阿~~还是太粗心了~ [s:12]

TOP

把上面的错改过来后,COMPILE完全正确,BUILD的时候有如下出错信息:
[BOX=WHITE]
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

a.exe - 2 error(s), 0 warning(s)

[/COLOR][/BOX]

TOP

我把你的程序拷下来都能执行的。。可能是环境问题吧  ,也可能是你的WinMain()有问题

TOP

引用:
下面是引用vikey于2005-10-21 15:10发表的:
我把你的程序拷下来都能执行的。。可能是环境问题吧  ,也可能是你的WinMain()有问题
晕······呵呵~~别的几个程序也是这个问题,无奈了~ [s:12]

TOP

估计是这个LRESULT _stdcall 的问题
知道这个是什么意思吗?
stdcall是VC的调用规范:
调用规范决定从左至右或从右至左的参数传递,有何负责清空堆栈以及函数名称的解释
_stdcall   右->左

我觉的没写这个的必要
winproc
应该是个回调函数
你看看我贴的SDK的教程
http://blog.csdn.net/thisisll/ http://spaces.msn.com/thisisll/

TOP

引用:
下面是引用冬天的菠菜于2005-10-21 15:43发表的:
估计是这个LRESULT _stdcall 的问题
知道这个是什么意思吗?
stdcall是VC的调用规范:
调用规范决定从左至右或从右至左的参数传递,有何负责清空堆栈以及函数名称的解释
_stdcall   右->左
.......
“调用规范决定从左至右或从右至左的参数传递,有何负责清空堆栈以及函数名称的解释”这个也在书上看到过,去掉以后~~~似乎不行~

TOP

这是个回调函数
还要加CALLBACK
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM)
http://blog.csdn.net/thisisll/ http://spaces.msn.com/thisisll/

TOP