Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.
動作環境
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して、色を付くようにする。
というのが早いという意見がある。

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away