0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder XE4 > TStringGrid: カレンダー表示 (色つき) > 月変更可能 | クリックした時にその日付を[DateTimePicker1]に設定する

Last updated at Posted at 2018-11-28
動作環境
C++ Builder XE4

前回

処理概要

  • 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;
}
//---------------------------------------------------------------------------

動作例

2018-11-28_19h44_08.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?