動作環境
C++ Builder XE4
処理概要
- TPanel(PNL_base)上にコンポーネントがある
- TEdit
- TChart
- TPanel
- TShape
- これらのコンポーネントに対して同じ背景色で塗りたい
実装
Unit1.cpp
//---------------------------------------------------------------------------
# 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)
{
changeColor(PNL_base, clAqua);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::changeColor(TPanel *pnlBasePtr, TColor toColor)
{
TComponent *ptarget;
for(int idx=0; idx < pnlBasePtr->ControlCount; idx++) {
ptarget = pnlBasePtr->Controls[idx];
// 1. TEdit
if (dynamic_cast<TEdit *>(ptarget) !=NULL) {
TEdit *dstPtr = (TEdit *)ptarget;
dstPtr->Color = toColor;
}
// 2. TChart
if (dynamic_cast<TChart *>(ptarget) != NULL) {
TChart *dstPtr = (TChart *)ptarget;
dstPtr->Color = toColor;
}
// 3. TPanel
if (dynamic_cast<TPanel *>(ptarget) != NULL) {
TPanel *dstPtr = (TPanel *)ptarget;
dstPtr->ParentBackground = false;
dstPtr->ParentColor = false;
dstPtr->Color = toColor;
}
// 4. TShape
if (dynamic_cast<TShape *>(ptarget) != NULL) {
TShape *dstPtr = (TShape *)ptarget;
dstPtr->Brush->Color = toColor;
}
}
}
動作例
Button1押下後
PNL_baseの色変更時に子コンポーネントの色も合わせて変更する
時などに使う。