📄 jvqyeargrid.pas
字号:
var
AYear, AMonth, ADay: Word;
WD: Integer;
begin
Result := False;
DecodeDate(ADate, AYear, AMonth, ADay);
if AYear <> Self.Year then
Exit;
WD := DayOfWeek(EncodeDate(AYear, AMonth, 1));
Inc(WD, Integer(FirstDayOfWeek));
if WD > 7 then
Dec(WD, 7);
DayMonthIndexToColRow(WD + ADay - 1, AMonth, ACol, ARow);
Result := True;
end;
function TJvYearGrid.GetDateInfo(ADate: TDate; var AText: string): Boolean;
var
Col, Row: Integer;
begin
Result := DateToCell(ADate, Col, Row);
if Result then
AText := FYearData[Col, Row].InfoText;
end;
function TJvYearGrid.SetDateInfo(ADate: TDate; AText: string): Boolean;
var
Col, Row: Integer;
begin
Result := DateToCell(ADate, Col, Row);
if Result then
FYearData[Col, Row].InfoText := AText;
end;
procedure TJvYearGrid.SetBookMarkColor(const Value: TColor);
begin
if Value <> FBookMarkColor then
begin
FBookMarkColor := Value;
Invalidate;
end;
end;
procedure TJvYearGrid.Find1Click(Sender: TObject);
var
S: string;
Col, Row: Integer;
begin
ClearBookMarks;
S := InputBox(RsYearGridFind, RsEnterSeachText, '');
if S = '' then
Exit;
S := LowerCase(S);
for Row := 0 to 12 do
for Col := 0 to 37 do
if Pos(S, LowerCase(FYearData[Col, Row].InfoText)) > 0 then
FYearData[Col, Row].BookMark := True;
Invalidate;
end;
procedure TJvYearGrid.ClearFind1Click(Sender: TObject);
begin
ClearBookMarks;
end;
procedure TJvYearGrid.Find;
begin
Find1Click(nil);
end;
procedure TJvYearGrid.SaveFound(Sender: TObject);
var
List: TStringList;
FileName: string;
begin
List := TStringList.Create;
MakeHTML(List, HTMLBorder, True);
FileName := Format(RsFounds, [ChangeFileExt(FYearFile, '.htm')]);
List.SaveToFile(FileName);
List.Free;
Launch(FileName);
end;
procedure TJvYearGrid.SetOrientation(const Value: TJvYearGridOrientation);
begin
if FOrientation <> Value then
begin
FOrientation := Value;
if FOrientation = yoHorizontal then
begin
ColCount := 38;
RowCount := 13;
end
else
begin
ColCount := 13;
RowCount := 38;
end;
AdjustBounds;
Invalidate;
end;
end;
procedure TJvYearGrid.SetFirstDayOfWeek(const Value: TJvWeekDay);
begin
if FFirstDayOfWeek <> Value then
begin
FFirstDayOfWeek := Value;
SetupYearData;
end;
end;
procedure TJvYearGrid.SetAutoSize(Value: Boolean);
begin
if Value then
begin
if (aoGrid in AutoSizeOptions) then
begin
FSavedScrollBars := ScrollBars;
ScrollBars := ssNone;
end;
end
else
ScrollBars := FSavedScrollBars;
FAutoSize := Value;
AdjustBounds;
end;
function TJvYearGrid.GetDefaultColWidth: Integer;
begin
Result := inherited DefaultColWidth;
end;
function TJvYearGrid.GetDefaultRowHeight: Integer;
begin
Result := inherited DefaultRowHeight;
end;
procedure TJvYearGrid.SetDefaultColWidth(const Value: Integer);
var
SavedFirstColWidth: Integer;
begin
SavedFirstColWidth := ColWidths[0];
inherited DefaultColWidth := Value;
ColWidths[0] := SavedFirstColWidth;
end;
procedure TJvYearGrid.SetDefaultRowHeihgt(const Value: Integer);
var
SavedFirstRowHeight: Integer;
begin
SavedFirstRowHeight := RowHeights[0];
inherited DefaultRowHeight := Value;
RowHeights[0] := SavedFirstRowHeight;
end;
procedure TJvYearGrid.SetFirstColWidth(const Value: Integer);
begin
ColWidths[0] := Value;
end;
procedure TJvYearGrid.SetFirstRowHeight(const Value: Integer);
begin
RowHeights[0] := Value;
end;
procedure TJvYearGrid.SetWeekendDays(const Value: TJvWeekDaySet);
begin
FWeekendDays := Value;
end;
procedure TJvYearGrid.SetAutoSizeOptions(const Value: TJvAutoSizeOptions);
begin
FAutoSizeOptions := Value;
end;
procedure TJvYearGrid.UpdateAllSizes;
var
I: Integer;
CurValue: Integer;
MaxValue: Integer;
function GetHighestTextInRow(Row: Integer): Integer;
var
I: Integer;
CurValue: Integer;
begin
// find the highest text in the row.
Result := 0;
for I := 0 to ColCount-1 do
begin
if Orientation = yoHorizontal then
CurValue := Canvas.TextHeight(FYearData[I,Row].DisplayText)
else
CurValue := Canvas.TextHeight(FYearData[Row,I].DisplayText);
if CurValue > Result then
Result := CurValue;
end;
end;
function GetLargestTextInColumn(Column: Integer): Integer;
var
I: Integer;
CurValue: Integer;
begin
// find the largest text in the column
Result := 0;
for I := 0 to RowCount-1 do
begin
if Orientation = yoHorizontal then
CurValue := Canvas.TextWidth(FYearData[Column,I].DisplayText)
else
CurValue := Canvas.TextWidth(FYearData[I,Column].DisplayText);
if CurValue > Result then
Result := CurValue;
end;
end;
begin
if AutoSize then
begin
if aoFirstRow in AutoSizeOptions then
RowHeights[0] := GetHighestTextInRow(0) + CellMargins.Top + CellMargins.Bottom ;
if aoFirstColumn in AutoSizeOptions then
ColWidths[0] := GetLargestTextInColumn(0) + CellMargins.Left + CellMargins.Right ;
if aoRows in AutoSizeOptions then
begin
// find the highest text in each row and only use the
// highest value among those found
MaxValue := 0;
for I := 1 to RowCount-1 do
begin
CurValue := GetHighestTextInRow(I);
if CurValue > MaxValue then
MaxValue := CurValue;
end;
for I := 1 to RowCount-1 do
RowHeights[I] := MaxValue + CellMargins.Top + CellMargins.Bottom ;
end;
if aoColumns in AutoSizeOptions then
begin
// find the largest text in each column and only use
// the highest value among those found
MaxValue := 0;
for I := 1 to ColCount-1 do
begin
CurValue := GetLargestTextInColumn(I);
if CurValue > MaxValue then
MaxValue := CurValue;
end;
for I := 1 to ColCount-1 do
ColWidths[I] := MaxValue + CellMargins.Left + CellMargins.Top ;
end;
end;
end;
procedure TJvYearGrid.SetCellMargins(const Value: TJvRect);
begin
FCellMargins.Assign(Value);
AdjustBounds;
end;
procedure TJvYearGrid.AdjustBounds;
var
I: Integer;
NewWidth, NewHeight: Integer;
begin
if not (csReading in ComponentState) and FAutoSize then
begin
UpdateAllSizes;
if aoGrid in AutoSizeOptions then
begin
NewWidth := GridLineWidth + {GetSystemMetrics(SM_CXVSCROLL) +} 4;
for I := 0 to ColCount-1 do
Inc(NewWidth, ColWidths[I]+GridLineWidth);
NewHeight := GridLineWidth + {GetSystemMetrics(SM_CYHSCROLL) +} 4;
for I := 0 to RowCount-1 do
Inc(NewHeight, RowHeights[I]+GridLineWidth);
SetBounds(Left, Top, NewWidth, NewHeight);
end;
end;
end;
procedure TJvYearGrid.Loaded;
begin
inherited Loaded;
AdjustBounds;
end;
procedure TJvYearGrid.SetParent( const AParent: TWinControl);
begin
inherited SetParent(AParent);
if Parent <> nil then
AdjustBounds;
end;
procedure TJvYearGrid.CellMarginsChange(Sender: TObject);
begin
AdjustBounds;
end;
procedure TJvYearGrid.SetDayNamesAlignment(const Value: TAlignment);
begin
if FDayNamesAlignment <> Value then
begin
FDayNamesAlignment := Value;
Invalidate;
end;
end;
procedure TJvYearGrid.SetDaysAlignment(const Value: TAlignment);
begin
if FDaysAlignment <> Value then
begin
FDaysAlignment := Value;
Invalidate;
end;
end;
procedure TJvYearGrid.SetMonthNamesAlignment(const Value: TAlignment);
begin
if FMonthNamesAlignment <> Value then
begin
FMonthNamesAlignment := Value;
Invalidate;
end;
end;
procedure TJvYearGrid.SetYearAlignment(const Value: TAlignment);
begin
if FYearAlignment <> Value then
begin
FYearAlignment := Value;
Invalidate;
end;
end;
function TJvYearGrid.GetFirstColWidth: Integer;
begin
Result := ColWidths[0];
end;
function TJvYearGrid.GetFirstRowHeight: Integer;
begin
Result := RowHeights[0];
end;
function TJvYearGrid.IsCurrentYear: Boolean;
begin
Result := Year = FCurrentYear;
end;
{$IFNDEF USECUSTOMGRID}
procedure TJvYearGrid.MouseToCell(X, Y: Integer; var ACol, ARow: Integer);
var
Coord: TGridCoord;
begin
Coord := MouseCoord(X, Y);
ACol := Coord.X;
ARow := Coord.Y;
end;
{$ENDIF !USECUSTOMGRID}
procedure TJvYearGrid.ReadGridYear(Reader: TReader);
begin
Year := Reader.ReadInteger;
end;
procedure TJvYearGrid.WriteGridYear(Writer: TWriter);
begin
// Do nothing, we only provide read support for legacy reasons
end;
procedure TJvYearGrid.DefineProperties(Filer: TFiler);
begin
inherited;
Filer.DefineProperty('GridYear', ReadGridYear, WriteGridYear, False);
end;
procedure TJvYearGrid.ColRowToDayMonthIndex(ACol, ARow: Integer;
var DayIndex, MonthIndex: Integer);
begin
if Orientation = yoHorizontal then
begin
DayIndex := ACol;
MonthIndex := ARow;
end
else
begin
DayIndex := ARow;
MonthIndex := ACol;
end;
end;
procedure TJvYearGrid.DayMonthIndexToColRow(DayIndex, MonthIndex: Integer;
var ACol, ARow: Integer);
begin
if Orientation = yoHorizontal then
begin
ACol := DayIndex;
ARow := MonthIndex;
end
else
begin
ARow := DayIndex;
ACol := MonthIndex;
end;
end;
procedure TJvYearGrid.ColRowToDayMonth(ACol, ARow: Integer; var ADay,
AMonth: Integer);
var
DayIndex, MonthIndex: Integer;
begin
ColRowToDayMonthIndex(ACol, ARow, DayIndex, MonthIndex);
AMonth := MonthIndex;
ADay := FYearData[MonthIndex, DayIndex].DayInMonth;
end;
procedure TJvYearGrid.DayMonthToColRow(ADay, AMonth: Integer; var ACol,
ARow: Integer);
begin
DayMonthIndexToColRow(ADay, AMonth, ACol, ARow);
end;
procedure TJvYearGrid.DefaultDrawCell(ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
var
S: string;
MonthIndex: Integer;
DayIndex: Integer;
SWidth: Integer;
TextLeft: Integer;
function GetTextLeft(Alignment: TAlignment): Integer;
begin
case Alignment of
taRightJustify:
Result := Rect.Right - SWidth - CellMargins.Right ;
taCenter:
Result := Rect.Left + (Rect.Right-Rect.Left - SWidth - CellMargins.Left - CellMargins.Right + 2) div 2;
else
Result := Rect.Left + CellMargins.Left ;
end;
end;
begin
ColRowToDayMonthIndex(ACol, ARow, DayIndex, MonthIndex);
S := FYearData[DayIndex, MonthIndex].DisplayText;
TextLeft := Rect.Left;
with Canvas do
begin
SWidth := TextWidth(S);
Font.Color := clBlack;
Font.Style := Font.Style - [fsBold];
if (DayIndex = 0) then
begin
Brush.Color := BorderColor;
TextLeft := GetTextLeft(MonthNamesAlignment);
end;
if (MonthIndex = 0) then
begin
if (FYearData[DayIndex, MonthIndex].DefaultColor = clWhite) then
Brush.Color := BorderColor;
if DayIndex = 0 then
TextLeft := GetTextLeft(YearAlignment)
else
TextLeft := GetTextLeft(DayNamesAlignment);
end;
if (DayIndex > 0) and (MonthIndex > 0) then
begin
TextLeft := GetTextLeft(DaysAlignment);
if IsCurrentYear and (MonthIndex = FCurrentMonth) and (S = IntToStr(FCurrentDay)) then
begin
Font.Color := TodayFontColor;
Brush.Color := TodayBrushColor;
Font.Style := Font.Style + [fsBold];
end
else
if FYearData[DayIndex, MonthIndex].Custom then
Brush.Color := FYearData[DayIndex, MonthIndex].CustomColor
else
Brush.Color := FYearData[DayIndex, MonthIndex].DefaultColor;
end;
if FYearData[DayIndex, MonthIndex].BookMark then
Brush.Color := BookMarkColor;
TextRect(Rect, TextLeft, Rect.Top, S);
end;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvQYearGrid.pas,v $';
Revision: '$Revision: 1.20 $';
Date: '$Date: 2005/02/06 14:06:32 $';
LogPath: 'JVCL\run'
);
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -