LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > TStringGrid と TCalendar > TStringGridにTCalendarの文字列をコピーして、背景色を変更する実装

Posted at
動作環境
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);
    }
}

動作例

2018-11-28_18h18_01.png

備考

TCalendarのDrawCell()をoverrideして、色を付くようにする。
というのが早いという意見がある。

その方法は今のところ見つかっていない。

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