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

📄 jvstringgrid.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  Canvas.FillRect(Rect);
  S := Cells[AColumn, ARow];
  if Length(S) > 0 then
  begin
    InflateRect(Rect, -2, -2);
    DrawText(Canvas, S, Length(S), Rect,
      DT_SINGLELINE or DT_NOPREFIX or DT_VCENTER or
      Flags[GetCellAlignment(AColumn, ARow, State)]);
  end;
end;

{$IFDEF COMPILER6_UP}
function TJvStringGrid.GetEditStyle(ACol, ARow: Longint): TEditStyle; //override;
begin
   FCustomInplaceEditStyle := esSimple;
   if Assigned(FOnGetEditStyle) then
   begin
     FPickListStrings.Clear;
     FOnGetEditStyle(Self, ACol, ARow, FPickListStrings, FCustomInplaceEditStyle);
   end;
   Result := FCustomInplaceEditStyle;
end;
{$ENDIF COMPILER6_UP}

procedure TJvStringGrid.DrawCell(AColumn, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if (AColumn < FixedCols) or (ARow < FixedRows) then
    Canvas.Font := FixedFont;
  if Assigned(OnDrawCell) then
    inherited DrawCell(AColumn, ARow, Rect, State)
  else
  begin
    SetCanvasProperties(AColumn, ARow, Rect, State);
    DefaultDrawCell(AColumn, ARow, Rect, State);
    Canvas.Font := Font;
    Canvas.Brush := Brush;
  end;
end;

procedure TJvStringGrid.ExitCell(const EditText: string;
  AColumn, ARow: Integer);
begin
  if Assigned(FOnExitCell) then
    FOnExitCell(Self, AColumn, ARow, EditText);
end;

function TJvStringGrid.GetCellAlignment(AColumn, ARow: Integer;
  State: TGridDrawState): TAlignment;
begin
  Result := FAlignment;
  if Assigned(FGetCellAlignment) then
    FGetCellAlignment(Self, AColumn, ARow, State, Result);
end;

{$IFDEF VisualCLX}
function TJvStringGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
  Result := inherited SelectCell(ACol, ARow);
  if Result then
  begin
    Col := ACol;
    Row := ARow;
    EditorMode := True;
    InplaceEditor.SelectAll;
  end;
end;
{$ENDIF VisualCLX}

procedure TJvStringGrid.GMActivateCell(var Msg: TGMActivateCell);
begin
  Col := Msg.Column;
  Row := Msg.Row;
  EditorMode := True;
  InplaceEditor.SelectAll;
end;

procedure TJvStringGrid.InvalidateCell(AColumn, ARow: Integer);
begin
  inherited InvalidateCell(AColumn, ARow);
end;

procedure TJvStringGrid.InvalidateCol(AColumn: Integer);
begin
  inherited InvalidateCol(AColumn);
end;

procedure TJvStringGrid.InvalidateRow(ARow: Integer);
begin
  inherited InvalidateRow(ARow);
end;

procedure TJvStringGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
  inherited MouseDown(Button, Shift, X, Y);
  if Button = mbLeft then
    MouseToCell(X, Y, FCellOnMouseDown.X, FCellOnMouseDown.Y)
  else
    FCellOnMouseDown := TGridCoord(Point(-1, -1));
end;

procedure TJvStringGrid.MouseUp(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
var
  Cell: TGridCoord;
begin
  if Button = mbLeft then
    MouseToCell(X, Y, Cell.X, Cell.Y);
  if CompareMem(@Cell, @FCellOnMouseDown, SizeOf(Cell)) and
    ((Cell.X < FixedCols) or (Cell.Y < FixedRows)) then
    CaptionClick(Cell.X, Cell.Y);
  FCellOnMouseDown := TGridCoord(Point(-1, -1));
  inherited MouseUp(Button, Shift, X, Y);
end;

procedure TJvStringGrid.SetAlignment(const Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    Invalidate;
    if Assigned(InplaceEditor) then
      {$IFDEF VCL}
      TExInplaceEditList(InplaceEditor).RecreateWnd;
      {$ENDIF VCL}
      {$IFDEF VisualCLX}
      TExInplaceEditList(InplaceEditor).RecreateWidget;
      {$ENDIF VisualCLX}
  end;
end;

procedure TJvStringGrid.SetCanvasProperties(AColumn, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Assigned(FSetCanvasProperties) then
    FSetCanvasProperties(Self, AColumn, ARow, Rect, State);
end;

{$IFDEF VCL}
procedure TJvStringGrid.WMCommand(var Msg: TWMCommand);
begin
  if EditorMode and (Msg.Ctl = InplaceEditor.Handle) then
    inherited
  else
  if Msg.Ctl <> 0 then
    Msg.Result := SendMessage(Msg.Ctl, CN_COMMAND, TMessage(Msg).wParam, TMessage(Msg).lParam);
end;
{$ENDIF VCL}

function TJvStringGrid.InsertCol(Index: Integer): TStrings;
var
  I: Integer;
  AStr: TStrings;
begin
  ColCount := ColCount + 1;
  if Index < 0 then
    Index := 0;
  if Index >= ColCount then
    Index := ColCount - 1;
  Result := Cols[Index];
  if ColCount = 1 then
    Exit;
  for I := ColCount - 2 downto Index do
  begin
    AStr := Cols[I];
    Cols[I + 1] := AStr;
  end;
  Result := Cols[Index];
  Result.Clear;
end;

function TJvStringGrid.InsertRow(Index: Integer): TStrings;
var
  I: Integer;
  AStr: TStrings;
begin
  RowCount := RowCount + 1;
  if Index < 0 then
    Index := 0;
  if Index >= RowCount then
    Index := RowCount - 1;
  Result := Rows[Index];
  if RowCount = 1 then
    Exit;
  for I := RowCount - 2 downto Index do
  begin
    AStr := Rows[I];
    Rows[I + 1] := AStr;
  end;
  Result.Clear;
end;

procedure TJvStringGrid.RemoveCol(Index: Integer);
var
  I: Integer;
  AStr: TStrings;
begin
  if Index < 0 then
    Index := 0;
  if Index >= ColCount then
    Index := ColCount - 1;
  for I := Index + 1 to ColCount - 1 do
  begin
    AStr := Cols[I];
    Cols[I - 1] := AStr;
  end;
  if ColCount > 1 then
    ColCount := ColCount - 1;
end;

procedure TJvStringGrid.RemoveRow(Index: Integer);
var
  I: Integer;
  AStr: TStrings;
begin
  if Index < 0 then
    Index := 0;
  if Index >= RowCount then
    Index := RowCount - 1;
  for I := Index + 1 to RowCount - 1 do
  begin
    AStr := Rows[I];
    Rows[I - 1] := AStr;
  end;
  if RowCount > 1 then
    RowCount := RowCount - 1;
end;

procedure TJvStringGrid.Clear;
var
  I: Integer;
begin
  for I := 0 to ColCount - 1 do
    Cols[I].Clear;
end;

procedure TJvStringGrid.HideCol(Index: Integer);
begin
  ColWidths[Index] := -1;
end;

procedure TJvStringGrid.HideRow(Index: Integer);
begin
  RowHeights[Index] := -1;
end;

procedure TJvStringGrid.ShowCol(Index, AWidth: Integer);
begin
  if AWidth <= 0 then
    AWidth := DefaultColWidth;
  ColWidths[Index] := AWidth;
end;

procedure TJvStringGrid.ShowRow(Index, AHeight: Integer);
begin
  if AHeight <= 0 then
    AHeight := DefaultRowHeight;
  RowHeights[Index] := AHeight;

end;

procedure TJvStringGrid.SetFixedFont(const Value: TFont);
begin
  FFixedFont.Assign(Value);
end;

procedure TJvStringGrid.DoFixedFontChange(Sender: TObject);
begin
  Invalidate;
end;

{$IFDEF COMPILER6_UP}

// NEW-WP - invoked dynamically from the TInplaceEditList
procedure TJvStringGrid.ListBoxCloseUp(Sender: TObject);
begin
  if Assigned(FOnListBoxCloseUp) then
    FOnListBoxCloseUp(Self)
end;

procedure TJvStringGrid.EditButtonClick(Sender: TObject); //dynamic;
begin
  if Assigned(FOnEditButtonClick) then
    FOnEditButtonClick(Self)
end;

{$ENDIF COMPILER6_UP}

procedure TJvStringGrid.AutoSizeCol(Index, MinWidth: Integer);
var
  I, J, AColWidth: Integer;
  ASize: TSize;
begin
  if (Index >= 0) and (Index < ColCount) then
  begin
    if MinWidth < 0 then
      AColWidth := DefaultColWidth
    else
      AColWidth := MinWidth;
    for J := 0 to RowCount - 1 do
    begin
      {$IFDEF VCL}
      if GetTextExtentPoint32(Canvas.Handle, PChar(Cells[Index, J]), Length(Cells[Index, J]), ASize) then
      {$ENDIF VCL}
      {$IFDEF VisualCLX}
      if GetTextExtentPoint32W(Canvas.Handle, PWideChar(Cells[Index, J]), Length(Cells[Index, J]), ASize) then
      {$ENDIF VisualCLX}
        AColWidth := Max(AColWidth, ASize.cx + 8);
    end;
    ColWidths[Index] := AColWidth;
  end
  else
  begin
    for I := 0 to ColCount - 1 do
    begin
      if MinWidth < 0 then
        AColWidth := DefaultColWidth
      else
        AColWidth := MinWidth;
      for J := 0 to RowCount - 1 do
      begin
        {$IFDEF VCL}
        if GetTextExtentPoint32(Canvas.Handle, PChar(Cells[I, J]), Length(Cells[I, J]), ASize) then
        {$ENDIF VCL}
        {$IFDEF VisualCLX}
        if GetTextExtentPoint32W(Canvas.Handle, PWideChar(Cells[I, J]), Length(Cells[I, J]), ASize) then
        {$ENDIF VisualCLX}
          AColWidth := Max(AColWidth, ASize.cx + 8);
      end;
      ColWidths[I] := AColWidth;
    end;
  end;
end;

procedure TJvStringGrid.HideAll;
var
  I: Integer;
begin
  if ColCount < RowCount then
    for I := 0 to ColCount - 1 do
      ColWidths[I] := -1
  else
    for I := 0 to RowCount - 1 do
      RowHeights[I] := -1;
end;


procedure TJvStringGrid.ClearSelection; // Clears selection rectangle!
var
 S: TGridRect;
begin
  S.Left := -1;
  S.Top := -1;
  S.Right := -1;
  S.Bottom := -1;
  Self.Selection  := S;
  Refresh;
end;


procedure TJvStringGrid.ShowAll(AWidth, AHeight: Integer);
var
  I: Integer;
begin
  if AWidth < 0 then
    AWidth := DefaultColWidth;
  if AHeight < 0 then
    AHeight := DefaultRowHeight;
  for I := 0 to ColCount - 1 do
    if ColWidths[I] < 0 then
      ColWidths[I] := AWidth;
  for I := 0 to RowCount - 1 do
    if RowHeights[I] < 0 then
      RowHeights[I] := AHeight;
end;

function TJvStringGrid.IsHidden(ACol, ARow: Integer): Boolean;
begin
  Result := (ColWidths[ACol] < 0) or (RowHeights[ARow] < 0);
end;

procedure TJvStringGrid.HideCell(ACol, ARow: Integer);
begin
  ColWidths[ACol] := -1;
  RowHeights[ARow] := -1;
end;

procedure TJvStringGrid.ShowCell(ACol, ARow, AWidth, AHeight: Integer);
begin
  if AWidth < 0 then
    AWidth := DefaultColWidth;
  if AHeight < 0 then
    AWidth := DefaultRowHeight;
  if ColWidths[ACol] < 0 then
    ColWidths[ACol] := AWidth;
  if RowHeights[ARow] < 0 then
    RowHeights[ARow] := AHeight;
end;

procedure TJvStringGrid.DoLoadProgress(Position, Count: Integer);
begin
  if Assigned(FOnLoadProgress) then
    FOnLoadProgress(Self, Position, Count);
end;

procedure TJvStringGrid.DoSaveProgress(Position, Count: Integer);
begin
  if Assigned(FOnSaveProgress) then
    FOnSaveProgress(Self, Position, Count);
end;

procedure TJvStringGrid.SortGridByCols(ColOrder: array of Integer);
var
  I, J: Integer;
  Sorted: Boolean;

  function Sort(Row1, Row2: Integer): Integer;
  var
    C: Integer;
  begin
    C := 0;
    Result := AnsiCompareStr(Cols[ColOrder[C]][Row1], Cols[ColOrder[C]][Row2]);
    if Result = 0 then
    begin
      Inc(C);
      while (C <= High(ColOrder)) and (Result = 0) do
      begin
        Result := AnsiCompareStr(Cols[ColOrder[C]][Row1], Cols[ColOrder[C]][Row2]);
        Inc(C);
      end;
    end;
  end;

begin
  // (p3) is this really necessary? Doesn't seem so to me...
  if SizeOf(ColOrder) div SizeOf(I) <> ColCount then
    Exit;

  for I := 0 to High(ColOrder) do
    if (ColOrder[I] < 0) or (ColOrder[I] >= ColCount) then
      Exit;

  J := 0;
  Sorted := False;
  repeat
    Inc(J);
    for I := 0 to RowCount - 2 do
      if Sort(I, I + 1) > 0 then
      begin
        MoveRow(I + 1, I);
        Sorted := False;
      end;
  until Sorted or (J = 1000);
  Repaint;
end;

procedure TJvStringGrid.MoveColumn(FromIndex, ToIndex: Integer);
begin
  inherited MoveColumn(FromIndex, ToIndex);
end;

procedure TJvStringGrid.MoveRow(FromIndex, ToIndex: Integer);
begin
  inherited MoveRow(FromIndex, ToIndex);
end;

{$IFDEF UNITVERSIONING}
initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

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