问题:以TPageControl为例,程序中需要“鼠标左键双击”关闭某激活页[如腾讯浏览器]。
因该控件没有定义鼠标双击事件,只有自己为该控件添加消息映射。映射的消息为:
左键双击(WM_LBUTTONDBLCLK)。本文利用运行时间派生控件的方法实现,较自己
制作一个单独控件的方法来讲,这个更简单些。
// ---------单元头文件------------------------------------------------------
class TPageControlEx : public TPageControl
{ // TPageControl的公有派生类:增加了左键双击消息映射
protected:
void __fastcall OnLeftDbClick(TMessage &Msg);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_LBUTTONDBLCLK,TMessage,OnLeftDbClick)
END_MESSAGE_MAP(TPageControl)
public: // 内联构造:父类构造
inline __fastcall TPageControlEx(TComponent* Owner):TPageControl(Owner){ };
};
// -----------------------------------------------------------------
class TForm1 : public TForm
{
__published:
void __fastcall FormCreate(TObject *Sender);
private:
public:
__fastcall TForm1(TComponent* Owner);
TPageControlEx *PageControlEx;
};
// --------单元文件---------------------------------------------------------------
void __fastcall TPageControlEx::OnLeftDbClick(TMessage &Msg) // 派生类消息处理
{
AnsiString area=Form1->PageControlEx->ActivePage->Caption;
ShowMessage("收到左键双击消息\n双击区域:"+area);
// PageControl接受消息的区域:Tab按钮矩形区,无须再判断了!
}
// --------------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PageControlEx = new TPageControlEx(this);
PageControlEx->Parent=Form1;
PageControlEx->Align=alClient;
// -------------生成2个TabSheet实验一下------------------------
TTabSheet* newTabSheet = NULL;
newTabSheet = new TTabSheet(this);
newTabSheet->PageControl=PageControlEx;
newTabSheet->Caption="TabSheet1";
newTabSheet = new TTabSheet(this);
newTabSheet->PageControl=PageControlEx;
newTabSheet->Caption="TabSheet2";
}
// --------------------------------------------------------------------------
本文地址:http://com.8s8s.com/it/it29879.htm