C++ Builder XE4 > TStringGrid: 달력 표시(색상) > 월 변경 가능 | 클릭할 때 해당 날짜를 [DateTimePicker1]로 설정

운영 환경
C++ Builder XE4

지난번


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

  • 처리 개요


  • TStringGrid에서 달력 표시
  • 착색하기 위해
  • forward, backward 버튼으로 달 변경 가능

  • TStringGrid에서 특정 날짜를 클릭하면 [DateTimePicker1]에 ​​해당 날짜를 설정합니다.
  • [DateTimePicker1]은 날짜 표시에만 사용됩니다.
  • [DateTimePicker1]일 필요는 없다



  • 구현



    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 *B_copy;
        TCalendar *Calendar1;
        TStringGrid *StringGrid1;
        TButton *B_forward;
        TButton *B_backward;
        TLabel *L_datetime;
        TDateTimePicker *DateTimePicker1;
        void __fastcall B_copyClick(TObject *Sender);
        void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
              TGridDrawState State);
        void __fastcall B_forwardClick(TObject *Sender);
        void __fastcall B_backwardClick(TObject *Sender);
        void __fastcall StringGrid1Click(TObject *Sender);
    
    private:    // ユーザー宣言
        TDateTime m_calendarDate;
    public:     // ユーザー宣言
        __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    

    Unit1.cpp
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include <memory>
    #include <DateUtils.hpp>
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        m_calendarDate = Now();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
              TGridDrawState State)
    {
        String str = StringGrid1->Cells[ACol][ARow];
    
        if (str.Length() == 0) {
            return;
        }
    
        // 奇数日だけ色を縫る
        int aday = StrToIntDef(str, 0);
        if (aday % 2 == 1) {
            // 背景を塗る
            StringGrid1->Canvas->Brush->Color = clOlive;
            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);
        }
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::B_copyClick(TObject *Sender)
    {
        // 1. ラベルの表記
        L_datetime->Caption = m_calendarDate.FormatString(L"yyyy/mm");
    
        // 2. カレンダー文字列のコピー
        //
        StringGrid1->RowCount = 7;
        StringGrid1->ColCount = 7;
        StringGrid1->DefaultColWidth = Calendar1->Width / 7;
        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::B_forwardClick(TObject *Sender)
    {
        m_calendarDate = IncMonth(m_calendarDate, 1);
        Calendar1->CalendarDate = m_calendarDate;
    
        B_copyClick(Sender);
    }
    
    void __fastcall TForm1::B_backwardClick(TObject *Sender)
    {
        m_calendarDate = IncMonth(m_calendarDate, -1);
        Calendar1->CalendarDate = m_calendarDate;
    
        B_copyClick(Sender);
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::StringGrid1Click(TObject *Sender)
    {
        // クリックした位置の日付を[DateTimePicker1]に設定する
    
        int arow = StringGrid1->Row;
        int acol = StringGrid1->Col;
        String str = StringGrid1->Cells[acol][arow];
        if (str.Length() == 0) {
            return;
        }
        TDate adt = VarToDateTime(L_datetime->Caption + "/" + str);
        DateTimePicker1->Date = adt;
    }
    //---------------------------------------------------------------------------
    

    동작 예



    좋은 웹페이지 즐겨찾기