0
0

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 10.2 Tokyo > 派生クラス + オーバライド > TCalendarクラスを派生して、DrawCell()をオーバライドする

Last updated at Posted at 2019-07-18
動作環境
RAD Studio 10.2 Tokyo Update 3 

概要

  • TCalendarクラスを派生してDrawCell()をオーバライドできるようにする

関連

コードは10.2 Tokyoではそのまま動かないため、こちらの環境に合わせて変更した。

実装

CalendarUnit.h
//---------------------------------------------------------------------------

# ifndef CalendarUnitH
# define CalendarUnitH
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
# include <Vcl.Grids.hpp>
# include <Vcl.Samples.Calendar.hpp>
# include <Vcl.WinXCalendars.hpp>
//---------------------------------------------------------------------------

// 追加した派生クラス
class PACKAGE TSampleCalendar : public TCalendar
{
protected:
	virtual void __fastcall DrawCell(int ACol, int ARow, const TRect &Rect, TGridDrawState AState);
public:
	__fastcall TSampleCalendar(TComponent *Owner);
};
//---------------------------------------------------------------------------

class TFormCalendar : public TForm
{
__published:	// IDE で管理されるコンポーネント
private:	// ユーザー宣言
	TSampleCalendar *m_sampCal;
public:		// ユーザー宣言
	__fastcall TFormCalendar(TComponent* Owner);
	__fastcall ~TFormCalendar();
};

//---------------------------------------------------------------------------
extern PACKAGE TFormCalendar *FormCalendar;
//---------------------------------------------------------------------------
# endif
CalendarUnit.cpp
//---------------------------------------------------------------------------

# include <vcl.h>
# pragma hdrstop

# include "CalendarUnit.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TFormCalendar *FormCalendar;
//---------------------------------------------------------------------------
__fastcall TFormCalendar::TFormCalendar(TComponent* Owner)
	: TForm(Owner)
{
	m_sampCal = new TSampleCalendar(this);
	m_sampCal->Name = L"SampleCalendar1";
	m_sampCal->Height = 300;
	m_sampCal->Width = 300;
	m_sampCal->Top = 10;
	m_sampCal->Left = 10;
	m_sampCal->Visible = true;
	m_sampCal->Parent = this;
	m_sampCal->Year = 2019;
	m_sampCal->Month = 7;
	m_sampCal->Day = 18;
}

__fastcall TFormCalendar::~TFormCalendar()
{
	delete m_sampCal;
}

//---------------------------------------------------------------------------

__fastcall TSampleCalendar::TSampleCalendar(TComponent *Owner) : TCalendar(Owner)
{
	ColCount = 7;
	RowCount = 7;
	FixedCols = 0;
	FixedRows = 1;
	ScrollBars = ssNone;
	Options = (Options >> goRangeSelect) << goDrawFocusSelected;
}
void __fastcall TSampleCalendar::DrawCell(int ACol, int ARow, const TRect &ARect, TGridDrawState AState)
{
	// ベースクラスの実行
	TCalendar::DrawCell(ACol, ARow, ARect, AState);

	// 以下に追加の処理を加える
}

実行結果

2019-07-18_17h38_01.png

TSampleCalendar::DrawCell()にてブレークポイントを設定すると、きちんと停止した。

これでDrawCellをカスタマイズできるだろう。

検索用キーワード

  • override
  • inherite

オーバーライドの例 > 背景色を変える

3行目の背景色を変える実装例

void __fastcall TSampleCalendar::DrawCell(int ACol, int ARow, const TRect &ARect, TGridDrawState AState)
{
	// 以下に追加の処理を加える
	if (ARow == 3) {
		this->Canvas->Brush->Color = clRed;
		this->Canvas->FillRect(ARect);
	}

	// ベースクラスの実行
	TCalendar::DrawCell(ACol, ARow, ARect, AState);
}

2019-07-18_17h50_47.png

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?