⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cellprop.pas

📁 一个非常好用的中国式表格控件(源码),对需在程序中插入格式复杂的表格非常有用
💻 PAS
📖 第 1 页 / 共 2 页
字号:
              FontStyleVary := True;
          end;
        if (not FontSizeVary) and (FontSize <> StrToInt(EditFontSize.Text)) then
          FontSizeVary := True;
        if (not FontColorVary) and (FontColor <> PanelFontColor.Color) then
          FontColorVary := True;
        if (not UnderLineVary) and ((fsUnderLine in FontStyle) xor ChkBoxUnderLine.Checked) then
          UnderLineVary := True;
        // 第四页上的控件
        if (not DrawLeftVary) and (DrawLeft <> ChkBoxDrawLeft.Checked) then
          DrawLeftVary := True;
        if (not DrawTopVary) and (DrawTop <> ChkBoxDrawTop.Checked) then
          DrawTopVary := True;
        if (not DrawRightVary) and (DrawRight <> ChkBoxDrawRight.Checked) then
          DrawRightVary := True;
        if (not DrawBottomVary) and (DrawBottom <> ChkBoxDrawBottom.Checked) then
          DrawBottomVary := True;
        if (not FrameSizeVary) and (LineWidth <> StrToInt(EditFrameSize.Text)) then
          FrameSizeVary := True;
        NewPenStyleIndex := Ord(PenStyle);
        if (not PenStyleVary) and (NewPenStyleIndex <> CbxPenStyle.ItemIndex) then
          PenStyleVary := True;
        if (not BackColorVary) and (Color <> PanelBackColor.Color) then
          BackColorVary := True;
        if (not ReadOnlyVary) and (ReadOnly <> ChkBoxReadOnly.Checked) then
          ReadOnlyVary := True;
      end;
    end;
  // 第一页上的控件
  if CellTypeVary then
    LbxCellType.ItemIndex := -1;

  if MaxLengthVary then
    EditMaxLength.Text := '';
  if IntLengthVary then
    EditIntLength.Text := '';
  if DecLengthVary then
    EditDecLength.Text := '';
  ChkBoxAllowNeg.AllowGrayed := False;
  if AllowNegVary then
    begin
      ChkBoxAllowNeg.AllowGrayed := True;
      ChkBoxAllowNeg.State := cbGrayed;
    end;
  ChkBoxThousandSep.AllowGrayed := False;
  if ThousandSepVary then
    begin
      ChkBoxThousandSep.AllowGrayed := True;
      ChkBoxThousandSep.State := cbGrayed;
    end;
  ChkBoxTrailingZero.AllowGrayed := False;
  if TrailingZeroVary then
    begin
      ChkBoxTrailingZero.AllowGrayed := True;
      ChkBoxTrailingZero.State := cbGrayed;
    end;
  ChkBoxZeroNull.AllowGrayed := False;
  if ZeroNullVary then
    begin
      ChkBoxZeroNull.AllowGrayed := True;
      ChkBoxZeroNull.State := cbGrayed;
    end;

  // 第二页上的控件
  if HAlignVary then
    CbxHAlign.ItemIndex := -1;
  if VAlignVary then
    CbxVAlign.ItemIndex := -1;
  ChkBoxAutoWordBreak.AllowGrayed := False;
  if AutoWordBreakVary then
    begin
      ChkBoxAutoWordBreak.AllowGrayed := True;
      ChkBoxAutoWordBreak.State := cbGrayed;
    end;
  ChkBoxMerge.AllowGrayed := False;
  if MergeVary then
    begin
      ChkBoxMerge.AllowGrayed := True;
      ChkBoxMerge.State := cbGrayed;
    end;

  // 第三页上的控件
  if FontNameVary then
    LbxFontName.ItemIndex := -1;
  if FontStyleVary then
    LbxFontStyle.ItemIndex := -1;
  if FontSizeVary then
  begin
    LbxFontSize.ItemIndex := -1;
    EditFontSize.Text := '';
  end;
  if FontColorVary then
    PanelFontColor.Visible := False;
  ChkBoxUnderLine.AllowGrayed := False;
  if UnderLineVary then
    begin
      ChkBoxUnderLine.AllowGrayed := True;
      ChkBoxUnderLine.State := cbGrayed;
    end;
    
  // 第四页上的控件
  ChkBoxDrawLeft.AllowGrayed := False;
  if DrawLeftVary then
    begin
      ChkBoxDrawLeft.AllowGrayed := True;
      ChkBoxDrawLeft.State := cbGrayed;
    end;
  ChkBoxDrawTop.AllowGrayed := False;
  if DrawTopVary then
    begin
      ChkBoxDrawTop.AllowGrayed := True;
      ChkBoxDrawTop.State := cbGrayed;
    end;
  ChkBoxDrawRight.AllowGrayed := False;
  if DrawRightVary then
    begin
      ChkBoxDrawRight.AllowGrayed := True;
      ChkBoxDrawRight.State := cbGrayed;
    end;
  ChkBoxDrawBottom.AllowGrayed := False;
  if DrawBottomVary then
    begin
      ChkBoxDrawBottom.AllowGrayed := True;
      ChkBoxDrawBottom.State := cbGrayed;
    end;
  if FrameSizeVary then
    EditFrameSize.Text := '';
  if PenStyleVary then
    CbxPenStyle.ItemIndex := -1;
  if BackColorVary then
    PanelBackColor.Visible := False;
  ChkBoxReadOnly.AllowGrayed := False;
  if ReadOnlyVary then
    begin
      ChkBoxReadOnly.AllowGrayed := True;
      ChkBoxReadOnly.State := cbGrayed;
    end;
end;

procedure TFormCellProp.FormShow(Sender: TObject);
begin
  GetFirstCellProp;
  CellRange := TRect(TEasyGrid(ParentGrid).Selection);
  SetControlState;
end;

procedure TFormCellProp.FormCreate(Sender: TObject);
begin
  LbxFontName.Items.Assign(Screen.Fonts);
end;

procedure TFormCellProp.LbxFontNameClick(Sender: TObject);
begin
  with LbxFontName do
    PanelFontPreview1.Font.Name := Items[ItemIndex];
end;

procedure TFormCellProp.LbxFontStyleClick(Sender: TObject);
begin
  case LbxFontStyle.ItemIndex of
    0 : PanelFontPreview1.Font.Style := [];
    1 : PanelFontPreview1.Font.Style := [fsItalic];
    2 : PanelFontPreview1.Font.Style := [fsBold];
    3 : PanelFontPreview1.Font.Style := [fsBold, fsItalic];
  end;
end;

procedure TFormCellProp.LbxFontSizeClick(Sender: TObject);
begin
  with LbxFontSize do
  begin
    EditFontSize.Text := Items[ItemIndex];
    PanelFontPreview1.Font.Size := StrToInt(EditFontSize.Text);
  end;
end;

procedure TFormCellProp.UpDownClick(Sender: TObject; Button: TUDBtnType);
begin
  TEdit((Sender as TUpDown).Associate).Enabled := True;
end;

procedure TFormCellProp.ChkBoxUnderLineClick(Sender: TObject);
begin
  if ChkBoxUnderLine.Checked then
    PanelFontPreview1.Font.Style := PanelFontPreview1.Font.Style + [fsUnderLine]
  else
    PanelFontPreview1.Font.Style := PanelFontPreview1.Font.Style - [fsUnderLine];
end;

procedure TFormCellProp.EditFontSizeKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (integer(Key) in [$30..$39,VK_BACK,VK_INSERT,VK_END,VK_HOME]) then
    Key := #0;
end;

procedure TFormCellProp.EditFontSizeChange(Sender: TObject);
begin
  if (EditFontSize.Text <> '') and (StrToInt(EditFontSize.Text) > 409) then
    EditFontSize.Text := '409';
  if EditFontSize.Text = '' then
    PanelFontPreview1.Font.Size := 1
  else
    PanelFontPreview1.Font.Size := StrToInt(EditFontSize.Text);
end;

procedure TFormCellProp.BtnSetFontColorClick(Sender: TObject);
begin
  if ColorDialogCellProp.Execute then
  begin
    PanelFontPreview1.Font.Color := ColorDialogCellProp.Color;
    PanelFontColor.Color := ColorDialogCellProp.Color;
    PanelFontColor.Visible := True;
  end;
end;

procedure TFormCellProp.BtnSetBackColorClick(Sender: TObject);
begin
  if ColorDialogCellProp.Execute then
  begin
    PanelBackColor.Color := ColorDialogCellProp.Color;
    PanelBackColor.Visible := True;
  end;
end;

procedure TFormCellProp.CbxHAlignClick(Sender: TObject);
begin
  if CbxVAlign.ItemIndex < 0 then
    CbxVAlign.ItemIndex := 0;
end;

procedure TFormCellProp.CbxVAlignClick(Sender: TObject);
begin
  if CbxHAlign.ItemIndex < 0 then
    CbxHAlign.ItemIndex := 0;
end;

procedure TFormCellProp.BtnOkClick(Sender: TObject);
var
  NewAlignMode: TAlignMode;
  NewFontStyle: TFontStyles;
  i, j: Integer;
  AText: string;
  CanSetCellProp: Boolean;
begin
  CanSetCellProp := True;
  if Assigned(TEasyGrid(ParentGrid).BeforeSetCellProp) then
    TEasyGrid(ParentGrid).BeforeSetCellProp(ParentGrid, CellRange, CanSetCellProp);
  if not CanSetCellProp then Exit;
  // 暂时禁止刷新
  TEasyGrid(ParentGrid).AutoUpdate := False;
  // 第一页上的控件
  if EditMaxLength.Text <> '' then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnMaxLength, Pointer(StrToInt(EditMaxLength.Text)));
  if EditIntLength.Text <> '' then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnIntLength, Pointer(StrToInt(EditIntLength.Text)));
  if EditDecLength.Text <> '' then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDecLength, Pointer(StrToInt(EditDecLength.Text)));
  if not (ChkBoxAllowNeg.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnAllowNegative, Pointer(ChkBoxAllowNeg.Checked));
  if not (ChkBoxThousandSep.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnThousandSep, Pointer(ChkBoxThousandSep.Checked));
  if not (ChkBoxTrailingZero.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnTrailingZero, Pointer(ChkBoxTrailingZero.Checked));
  if not (ChkBoxZeroNull.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnZeroNull, Pointer(ChkBoxZeroNull.Checked));
  if LbxCellType.ItemIndex >= 0 then
  begin
    // 改变网格类型时要清空原来的内容,否则会引起数据类型转换错误
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDataStyle, Pointer(LbxCellType.ItemIndex));
    with TEasyGrid(ParentGrid) do
    for i:=CellRange.Left to CellRange.Right do
      for j:=CellRange.Top to CellRange.Bottom do
      begin
        AText := Cells[i, j].ForeText;
        Cells[i, j].ForeText := '';
        ForeTexts[i, j] := AText;
      end;
  end;
  // 第二页上的控件
  if (CbxHAlign.ItemIndex >= 0) and (CbxVAlign.ItemIndex >= 0) then
  begin
    NewAlignMode := TEasyGrid(ParentGrid).CellAlignMode;
    case CbxHAlign.ItemIndex of
      0 :
        case CbxVAlign.ItemIndex of
          0 :
            NewAlignMode := taTopLeft;
          1 :
            NewAlignMode := taLeft;
          2 :
            NewAlignMode := taBottomLeft;
        end;
      1 :
        case CbxVAlign.ItemIndex of
          0 :
            NewAlignMode := taTop;
          1 :
            NewAlignMode := taCenter;
          2 :
            NewAlignMode := taBottom;
        end;
      2 :
        case CbxVAlign.ItemIndex of
          0 :
            NewAlignMode := taTopRight;
          1 :
            NewAlignMode := taRight;
          2 :
            NewAlignMode := taBottomRight;
        end;
    end;
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnAlignMode, Pointer(NewAlignMode));
  end;
  if not (ChkBoxAutoWordBreak.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnAutoWordBreak, Pointer(ChkBoxAutoWordBreak.Checked));
  if not (ChkBoxMerge.State = cbGrayed) then
    if ChkBoxMerge.Checked then
      TEasyGrid(ParentGrid).SetMerges(CellRange, True)
    else
      TEasyGrid(ParentGrid).DeleteMerges(CellRange);
  // 第三页上的控件
  with LbxFontName do
  if (ItemIndex >= 0) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnFontName, Pointer(Items[ItemIndex]));
  with LbxFontStyle do
  if (ItemIndex >= 0) then
  begin
    NewFontStyle := [];
    case ItemIndex of
      1 : NewFontStyle := [fsItalic];
      2 : NewFontStyle := [fsBold];
      3 : NewFontStyle := [fsBold, fsItalic];
    end;
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnFontStyle, @NewFontStyle);
  end;
  if (ChkBoxUnderLine.State <> cbGrayed) then
  with TEasyGrid(ParentGrid) do
  for i:=CellRange.Left to CellRange.Right do
    for j:=CellRange.Top to CellRange.Bottom do
      if ChkBoxUnderLine.Checked then
        Cells[i ,j].FontStyle := Cells[i ,j].FontStyle + [fsUnderLine]
      else
        Cells[i ,j].FontStyle := Cells[i ,j].FontStyle - [fsUnderLine];
  if EditFontSize.Text <> '' then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnFontSize, Pointer(StrToInt(EditFontSize.Text)));
  if PanelFontColor.Visible then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnFontColor, Pointer(PanelFontColor.Color));
  // 第四页上的控件
  if not (ChkBoxDrawLeft.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDrawLeft, Pointer(ChkBoxDrawLeft.Checked));
  if not (ChkBoxDrawTop.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDrawTop, Pointer(ChkBoxDrawTop.Checked));
  if not (ChkBoxDrawRight.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDrawRight, Pointer(ChkBoxDrawRight.Checked));
  if not (ChkBoxDrawBottom.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnDrawBottom, Pointer(ChkBoxDrawBottom.Checked));
  if EditFrameSize.Text <> '' then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnLineWidth, Pointer(StrToInt(EditFrameSize.Text)));
  with CbxPenStyle do
  if ItemIndex >= 0 then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnPenStyle, Pointer(ItemIndex));
  if PanelBackColor.Visible then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnColor, Pointer(PanelBackColor.Color));
  if not (ChkBoxReadOnly.State = cbGrayed) then
    TEasyGrid(ParentGrid).SetRangeProperty(CellRange, pnReadOnly, Pointer(ChkBoxReadOnly.Checked));
  // 刷新网格
  TEasyGrid(ParentGrid).AutoUpdate := True;
end;

procedure TFormCellProp.EditFrameSizeChange(Sender: TObject);
begin
  if (EditFrameSize.Text <> '') and (StrToInt(EditFrameSize.Text) > 5) then
    EditFrameSize.Text := '5';
end;

procedure TFormCellProp.EditMaxLengthChange(Sender: TObject);
begin
  if (EditMaxLength.Text <> '') and (StrToInt(EditMaxLength.Text) > 255) then
    EditMaxLength.Text := '255';
end;

procedure TFormCellProp.EditIntLengthChange(Sender: TObject);
begin
  if (EditIntLength.Text <> '') and (StrToInt(EditIntLength.Text) > 255) then
    EditIntLength.Text := '255';
end;

procedure TFormCellProp.EditDecLengthChange(Sender: TObject);
begin
  if (EditDecLength.Text <> '') and (StrToInt(EditDecLength.Text) > 255) then
    EditDecLength.Text := '255';
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -