動作環境
C++ Builder XE4
処理概要
- TCalendarとTStringGridがある
- TStringGridにTCalendarの文字列をコピーする
- TStringGridの一部のセルの背景色を変更する
実装
Unit1.h
//---------------------------------------------------------------------------
# ifndef Unit1H
# define Unit1H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
# include <Vcl.ComCtrls.hpp>
# include <Vcl.Grids.hpp>
# include <Vcl.Samples.Calendar.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TButton *Button1;
TCalendar *Calendar1;
TStringGrid *StringGrid1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State);
private: // ユーザー宣言
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
# endif
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include <memory>
# 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)
{
// カレンダー文字列のコピー
StringGrid1->RowCount = 7;
StringGrid1->ColCount = 7;
StringGrid1->DefaultColWidth = 40;
StringGrid1->FixedRows = 1;
StringGrid1->FixedCols = 0;
for(int ri=0; ri<7; ri++) {
for(int ci=0; ci<7; ci++) {
StringGrid1->Cells[ci][ri] = Calendar1->CellText[ci][ri];
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
if (ARow == 3) {
StringGrid1->Canvas->Brush->Color = clRed;
StringGrid1->Canvas->FillRect(Rect);
StringGrid1->Canvas->Brush->Color = clWhite;
String str = StringGrid1->Cells[ACol][ARow];
DrawText(StringGrid1->Canvas->Handle, str.c_str(), str.Length(), &Rect, Position);
}
}
動作例
備考
TCalendarのDrawCell()をoverrideして、色を付くようにする。
というのが早いという意見がある。
その方法は今のところ見つかっていない。