604
社区成员
发帖
与我相关
我的任务
分享
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//几个RadioButton复用此事件处理函数
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
TRadioButton *obj=(TRadioButton *)Sender;
Memo1->Lines->Add(IntToStr(obj->Tag)+"."+obj->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if(Msg.message==WM_LBUTTONUP) //鼠标左键UP消息
{
TPoint point=GroupBox1->ScreenToClient(Msg.pt); //鼠标相对于GroupBox客户区坐标
if(PtInRect(GroupBox1->ClientRect,point)) //鼠标在GroupBox客户区内
{
//获取鼠标位置所在的控件
TRadioButton *obj=(TRadioButton *)GroupBox1->ControlAtPos(point,true,true);
if(obj)
{
obj->OnClick=NULL;
obj->Checked=true; //选中该选项
// obj->OnClick=RadioButton1Click;
RadioButton1Click(obj); //调用选项点击函数
}
Handled=true; //该鼠标消息已处理
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
int tag;
tag=((TRadioButton *)Sender)->Tag;
switch(tag)
{
}
}
RadioButton2、RadioButton3等的OnClick都选为下拉的RadioButton1Click,就是复用RadioButton1Click事件处理函数,
几个RadioButton的Tag属性事先设置为不同的值就可以了。
Sender代表实际触发事件(比如点击事件)的那个控件。