C++ Builder XE4 > TStringGrid 및 TCalendar > TStringGrid에 TCalendar 문자열을 복사하여 배경색을 변경하는 구현

운영 환경
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하여 색을 띠게 한다.
    라는 것이 빠르다는 의견이 있다.

    그 방법은 현재 발견되지 않았다.

    좋은 웹페이지 즐겨찾기