C++ Builder XE4 > TStringGrid: 달력 표시(색상) > 월 변경 가능 | 클릭할 때 해당 날짜를 [DateTimePicker1]로 설정
C++ Builder XE4
지난번
처리 개요
구현
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;
}
//---------------------------------------------------------------------------
동작 예
Reference
이 문제에 관하여(C++ Builder XE4 > TStringGrid: 달력 표시(색상) > 월 변경 가능 | 클릭할 때 해당 날짜를 [DateTimePicker1]로 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/7of9/items/5af79507ac96bad9f59d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)