c++builder自定义控件

c++builder自定义控件

http://docwiki.embarcadero.com/CodeExamples/XE8/en/RegisterComponents_%28C%2B%2B%29

 

void __fastcall SetFocusControl(TWinControl* Value);
    void __fastcall CMDialogChar(TWMKey& Message);
    void __fastcall CMTextChanged(TMessage& Message);
 
protected:
    virtual void __fastcall Notification(TComponent* AComponent, TOperation Operation);
    virtual void __fastcall Paint(void);
 
public:
    __fastcall virtual TDemoLabel(TComponent* AOwner);
 
__published:
    __property Caption ;
    __property Color ;
    __property TWinControl* FocusControl = {read=FFocusControl, write=SetFocusControl, nodefault};
    __property Font ;
    __property ParentColor ;
    __property ParentFont ;
public:
    __fastcall virtual ~TDemoLabel(void) { }
 
BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(CM_DIALOGCHAR, TWMKey, CMDialogChar);
  MESSAGE_HANDLER(CM_TEXTCHANGED, TMessage, CMTextChanged);
END_MESSAGE_MAP(TGraphicControl);
};


void __fastcall PACKAGE Register()
{
  TComponentClass classes[1] = {__classid(TDemoLabel)};
  RegisterComponents("Samples", classes, 0);
  RegisterComponents("MySystem", classes, 0);
}
 
__fastcall TDemoLabel::TDemoLabel(TComponent *AOwner):
    TGraphicControl(AOwner)
{
  FComponentStyle >> csInheritable;
  Width=64;
  Height=13;
}
 
void __fastcall    TDemoLabel::Notification(TComponent *AComponent, TOperation Operation)
{
  TGraphicControl::Notification(AComponent, Operation);
  if ((Operation == opRemove)  && (AComponent == FFocusControl))
    FFocusControl = 0;
}
 
void __fastcall    TDemoLabel::SetFocusControl(TWinControl *Value)
{
  FFocusControl = Value;
 
  // Calling FreeNotification ensures that this component 
  // will receive an opRemove when Value is either removed 
  // from its owner or destroyed.
 
  Value->FreeNotification(Value);
}
 
void __fastcall    TDemoLabel::Paint()
{
  TRect    Rect = ClientRect;
  Canvas->Font = Font;
  Canvas->Brush->Color = Color;
  Canvas->FillRect(Rect);
  DrawText(
    Canvas->Handle,
    Caption.t_str(),
    Caption.Length(),
    &Rect,
    DT_EXPANDTABS | DT_WORDBREAK | DT_LEFT);
}
 
void __fastcall    TDemoLabel::CMDialogChar(TCMDialogKey &Message)
{
  if (FFocusControl != NULL &&
      Enabled == true &&
      IsAccel(Message.CharCode, Caption))
      if (FFocusControl->CanFocus()){
        FFocusControl->SetFocus();
        Message.Result = 1;
      }
}
 
void __fastcall    TDemoLabel::CMTextChanged(TMessage &Message)
{
  Invalidate();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++Builder中,TTabSet控件会自动根据添加的选项卡标题进行绘制。你可以通过设置TTabSet的Tabs属性来添加选项卡,并设置TabWidth属性来定义每个选项卡的宽度。 以下是一个简单的示例来演示如何绘制TTabSet控件的选项卡: 1. 在C++Builder的窗体设计器中,找到“标准”面板中的TTabSet控件,并将其拖放到窗体上。 2. 在Object Inspector(属性编辑器)中,你可以设置TTabSet的一些属性,如名称、位置、尺寸等。 3. 双击TTabSet控件,会自动跳转到OnDrawTab事件处理程序。 4. 在OnDrawTab事件处理程序中,你可以使用Canvas提供的绘图函数来绘制选项卡的外观。例如,你可以使用Canvas的FillRect函数来绘制选项卡的背景色,使用TextOut函数来绘制选项卡标题。 以下是一个简单的OnDrawTab事件处理程序示例: ```cpp void __fastcall TForm1::TabSet1DrawTab(TObject *Sender, int TabIndex, const TRect &Rect, bool Active) { TTabSet *tabSet = dynamic_cast<TTabSet*>(Sender); TCanvas *canvas = tabSet->Canvas; // 绘制选项卡背景色 canvas->Brush->Color = clSkyBlue; canvas->FillRect(Rect); // 绘制选项卡标题 canvas->Font->Color = clBlack; canvas->TextOut(Rect.Left + 5, Rect.Top + 5, tabSet->Tabs->Strings[TabIndex]); } ``` 在这个示例中,我们设置选项卡的背景色为天蓝色,并使用黑色字体绘制选项卡标题。你可以根据需要自定义绘制选项卡的外观。 通过上述步骤,你可以自定义绘制TTabSet控件的选项卡。希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值