0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder XE4 > TPanelとColor > TPanel上の指定コンポーネントの背景色を変更する

Posted at
動作環境
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押下後

2018-12-21_15h32_18.png

PNL_baseの色変更時に子コンポーネントの色も合わせて変更する
時などに使う。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?