动态创建组件后出现的问题……含源程序
按书上的做法,先是动态生成了一个TShape组件,然后就为它指定鼠标按下的事件处理函数。
具体程序如下:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TRadioGroup *RadioGroup1;
TButton *Button2;
TButton *Button1;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall RadioGroup1Click(TObject *Sender);
private: // User declarations
TShape *shape;
void __fastcall shapeMouseDown(TObject * Sender, TMouserButton Button, TShiftState Shift, int x, int y);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
shape=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
delete shape;
shape=new TShape(this);
shape->Parent=Form1;
shape->Left=80;
shape->Top=30;
shape->OnMouseDown=shapeMouseDown;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
delete shape;
shape=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
{
if(shape==NULL)
{
return;
}
if(RadioGroup1->ItemIndex==0)
{
shape->Shape=stCircle;
}
else
{
shape->Shape=stRectangle;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::shapeMouseDown(TObject * Sender, TMouserButton Button, TShiftState Shift, int x, int y)
{
ShowMessage("X="AnsiString(X)+"; Y="AnsiString(Y));
}
编译后出现了下面的错误提示:
Build
[C++ Error] Unit1.h(25): E2303 Type name expected
[C++ Error] Unit1.cpp(26): E2451 Undefined symbol 'ShapeMouseDown'
[C++ Error] Unit1.cpp(51): E2303 Type name expected
请求高手解答
问题点数:20、回复次数:6Top
1 楼stevenjscn(小胖)回复于 2006-05-04 19:56:12 得分 0
先顶一下Top
2 楼stevenjscn(小胖)回复于 2006-05-04 19:56:33 得分 0
先顶一下Top
3 楼jjwwang((空园歌独酌,春日赋闲居))回复于 2006-05-04 21:36:03 得分 20
错误1.
void __fastcall shapeMouseDown(TObject * Sender, TMouserButton Button, TShiftState
TMouserButton 多的了一个 "r"
错误2.
ShowMessage("X="+ AnsiString(x)+"\; Y="AnsiString(y));
少 "+" 号, 另外在分号前加 转义符 , c++是区分大小写的, X 和 x, Y 和 y 是不一样的.Top
4 楼jjwwang((空园歌独酌,春日赋闲居))回复于 2006-05-04 21:36:25 得分 0
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TRadioGroup *RadioGroup1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall RadioGroup1Click(TObject *Sender);
private:
void __fastcall shapeMouseDown(TObject * Sender, TMouseButton Button, TShiftState Shift, int x, int y); // User declarations
TShape *shape;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Top
5 楼jjwwang((空园歌独酌,春日赋闲居))回复于 2006-05-04 21:36:36 得分 0
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
delete shape;
shape=new TShape(this);
shape->Parent=Form1;
shape->Left=80;
shape->Top=30;
// shape->OnMouseDown=shapeMouseDown;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
delete shape;
shape=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
{
if(shape==NULL)
{
return;
}
if(RadioGroup1->ItemIndex==0)
{
shape->Shape=stCircle;
}
else
{
shape->Shape=stRectangle;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::shapeMouseDown(TObject * Sender, TMouseButton Button, TShiftState Shift, int x, int y)
{
ShowMessage("X=" + AnsiString(x)+"\; Y="+AnsiString(y));
}Top
6 楼jjwwang((空园歌独酌,春日赋闲居))回复于 2006-05-04 21:39:11 得分 0
// shape->OnMouseDown=shapeMouseDown;
上面这行被我屏蔽了, 请重新去掉 //, 不然事件不会触发.Top




