📄 grids.pas
字号:
{*******************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 1999-2002 Borland Software Corporation }
{ }
{*******************************************************}
unit Grids;
{$R-,T-,H+,X+}
interface
uses Messages, {$IFDEF LINUX} WinUtils, {$ENDIF} Windows, SysUtils, Classes,
Variants, Graphics, Menus, Controls, Forms, StdCtrls, Mask;
const
MaxCustomExtents = MaxListSize;
MaxShortInt = High(ShortInt);
type
EInvalidGridOperation = class(Exception);
{ Internal grid types }
TGetExtentsFunc = function(Index: Longint): Integer of object;
TGridAxisDrawInfo = record
EffectiveLineWidth: Integer;
FixedBoundary: Integer;
GridBoundary: Integer;
GridExtent: Integer;
LastFullVisibleCell: Longint;
FullVisBoundary: Integer;
FixedCellCount: Integer;
FirstGridCell: Integer;
GridCellCount: Integer;
GetExtent: TGetExtentsFunc;
end;
TGridDrawInfo = record
Horz, Vert: TGridAxisDrawInfo;
end;
TGridState = (gsNormal, gsSelecting, gsRowSizing, gsColSizing,
gsRowMoving, gsColMoving);
TGridMovement = gsRowMoving..gsColMoving;
{ TInplaceEdit }
{ The inplace editor is not intended to be used outside the grid }
TCustomGrid = class;
TInplaceEdit = class(TCustomMaskEdit)
private
FGrid: TCustomGrid;
FClickTime: Longint;
procedure InternalMove(const Loc: TRect; Redraw: Boolean);
procedure SetGrid(Value: TCustomGrid);
procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
procedure WMPaste(var Message); message WM_PASTE;
procedure WMCut(var Message); message WM_CUT;
procedure WMClear(var Message); message WM_CLEAR;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure DblClick; override;
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
MousePos: TPoint): Boolean; override;
function EditCanModify: Boolean; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
procedure BoundsChanged; virtual;
procedure UpdateContents; virtual;
procedure WndProc(var Message: TMessage); override;
property Grid: TCustomGrid read FGrid;
public
constructor Create(AOwner: TComponent); override;
procedure Deselect;
procedure Hide;
procedure Invalidate; reintroduce;
procedure Move(const Loc: TRect);
function PosEqual(const Rect: TRect): Boolean;
procedure SetFocus; reintroduce;
procedure UpdateLoc(const Loc: TRect);
function Visible: Boolean;
end;
{ TCustomGrid }
{ TCustomGrid is an abstract base class that can be used to implement
general purpose grid style controls. The control will call DrawCell for
each of the cells allowing the derived class to fill in the contents of
the cell. The base class handles scrolling, selection, cursor keys, and
scrollbars.
DrawCell
Called by Paint. If DefaultDrawing is true the font and brush are
intialized to the control font and cell color. The cell is prepainted
in the cell color and a focus rect is drawn in the focused cell after
DrawCell returns. The state passed will reflect whether the cell is
a fixed cell, the focused cell or in the selection.
SizeChanged
Called when the size of the grid has changed.
BorderStyle
Allows a single line border to be drawn around the control.
Col
The current column of the focused cell (runtime only).
ColCount
The number of columns in the grid.
ColWidths
The width of each column (up to a maximum MaxCustomExtents, runtime
only).
DefaultColWidth
The default column width. Changing this value will throw away any
customization done either visually or through ColWidths.
DefaultDrawing
Indicates whether the Paint should do the drawing talked about above in
DrawCell.
DefaultRowHeight
The default row height. Changing this value will throw away any
customization done either visually or through RowHeights.
FixedCols
The number of non-scrolling columns. This value must be at least one
below ColCount.
FixedRows
The number of non-scrolling rows. This value must be at least one
below RowCount.
GridLineWidth
The width of the lines drawn between the cells.
LeftCol
The index of the left most displayed column (runtime only).
Options
The following options are available:
goFixedHorzLine: Draw horizontal grid lines in the fixed cell area.
goFixedVertLine: Draw veritical grid lines in the fixed cell area.
goHorzLine: Draw horizontal lines between cells.
goVertLine: Draw vertical lines between cells.
goRangeSelect: Allow a range of cells to be selected.
goDrawFocusSelected: Draw the focused cell in the selected color.
goRowSizing: Allows rows to be individually resized.
goColSizing: Allows columns to be individually resized.
goRowMoving: Allows rows to be moved with the mouse
goColMoving: Allows columns to be moved with the mouse.
goEditing: Places an edit control over the focused cell.
goAlwaysShowEditor: Always shows the editor in place instead of
waiting for a keypress or F2 to display it.
goTabs: Enables the tabbing between columns.
goRowSelect: Selection and movement is done a row at a time.
Row
The row of the focused cell (runtime only).
RowCount
The number of rows in the grid.
RowHeights
The hieght of each row (up to a maximum MaxCustomExtents, runtime
only).
ScrollBars
Determines whether the control has scrollbars.
Selection
A TGridRect of the current selection.
TopLeftChanged
Called when the TopRow or LeftCol change.
TopRow
The index of the top most row displayed (runtime only)
VisibleColCount
The number of columns fully displayed. There could be one more column
partially displayed.
VisibleRowCount
The number of rows fully displayed. There could be one more row
partially displayed.
Protected members, for implementors of TCustomGrid descendents
DesignOptionBoost
Options mixed in only at design time to aid design-time editing.
Default = [goColSizing, goRowSizing], which makes grid cols and rows
resizeable at design time, regardless of the Options settings.
VirtualView
Controls the use of maximum screen clipping optimizations when the
grid window changes size. Default = False, which means only the
area exposed by the size change will be redrawn, for less flicker.
VirtualView = True means the entire data area of the grid is redrawn
when the size changes. This is required when the data displayed in
the grid is not bound to the number of rows or columns in the grid,
such as the dbgrid (a few grid rows displaying a view onto a million
row table).
}
TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
goColMoving, goEditing, goTabs, goRowSelect,
goAlwaysShowEditor, goThumbTracking);
TGridOptions = set of TGridOption;
TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
TGridCoord = record
X: Longint;
Y: Longint;
end;
TGridRect = record
case Integer of
0: (Left, Top, Right, Bottom: Longint);
1: (TopLeft, BottomRight: TGridCoord);
end;
TEditStyle = (esSimple, esEllipsis, esPickList);
TSelectCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean) of object;
TDrawCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
Rect: TRect; State: TGridDrawState) of object;
TCustomGrid = class(TCustomControl)
private
FAnchor: TGridCoord;
FBorderStyle: TBorderStyle;
FCanEditModify: Boolean;
FColCount: Longint;
FColWidths: Pointer;
FTabStops: Pointer;
FCurrent: TGridCoord;
FDefaultColWidth: Integer;
FDefaultRowHeight: Integer;
FFixedCols: Integer;
FFixedRows: Integer;
FFixedColor: TColor;
FGridLineWidth: Integer;
FOptions: TGridOptions;
FRowCount: Longint;
FRowHeights: Pointer;
FScrollBars: TScrollStyle;
FTopLeft: TGridCoord;
FSizingIndex: Longint;
FSizingPos, FSizingOfs: Integer;
FMoveIndex, FMovePos: Longint;
FHitTest: TPoint;
FInplaceEdit: TInplaceEdit;
FInplaceCol, FInplaceRow: Longint;
FColOffset: Integer;
FDefaultDrawing: Boolean;
FEditorMode: Boolean;
function CalcCoordFromPoint(X, Y: Integer;
const DrawInfo: TGridDrawInfo): TGridCoord;
procedure CalcDrawInfoXY(var DrawInfo: TGridDrawInfo;
UseWidth, UseHeight: Integer);
function CalcMaxTopLeft(const Coord: TGridCoord;
const DrawInfo: TGridDrawInfo): TGridCoord;
procedure CancelMode;
procedure ChangeSize(NewColCount, NewRowCount: Longint);
procedure ClampInView(const Coord: TGridCoord);
procedure DrawSizingLine(const DrawInfo: TGridDrawInfo);
procedure DrawMove;
procedure GridRectToScreenRect(GridRect: TGridRect;
var ScreenRect: TRect; IncludeLine: Boolean);
procedure Initialize;
procedure InvalidateRect(ARect: TGridRect);
procedure ModifyScrollBar(ScrollBar, ScrollCode, Pos: Cardinal;
UseRightToLeft: Boolean);
procedure MoveAdjust(var CellPos: Longint; FromIndex, ToIndex: Longint);
procedure MoveAnchor(const NewAnchor: TGridCoord);
procedure MoveAndScroll(Mouse, CellHit: Integer; var DrawInfo: TGridDrawInfo;
var Axis: TGridAxisDrawInfo; Scrollbar: Integer; const MousePt: TPoint);
procedure MoveCurrent(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
procedure MoveTopLeft(ALeft, ATop: Longint);
procedure ResizeCol(Index: Longint; OldSize, NewSize: Integer);
procedure ResizeRow(Index: Longint; OldSize, NewSize: Integer);
procedure SelectionMoved(const OldSel: TGridRect);
procedure ScrollDataInfo(DX, DY: Integer; var DrawInfo: TGridDrawInfo);
procedure TopLeftMoved(const OldTopLeft: TGridCoord);
procedure UpdateScrollPos;
procedure UpdateScrollRange;
function GetColWidths(Index: Longint): Integer;
function GetRowHeights(Index: Longint): Integer;
function GetSelection: TGridRect;
function GetTabStops(Index: Longint): Boolean;
function GetVisibleColCount: Integer;
function GetVisibleRowCount: Integer;
function IsActiveControl: Boolean;
procedure ReadColWidths(Reader: TReader);
procedure ReadRowHeights(Reader: TReader);
procedure SetBorderStyle(Value: TBorderStyle);
procedure SetCol(Value: Longint);
procedure SetColCount(Value: Longint);
procedure SetColWidths(Index: Longint; Value: Integer);
procedure SetDefaultColWidth(Value: Integer);
procedure SetDefaultRowHeight(Value: Integer);
procedure SetEditorMode(Value: Boolean);
procedure SetFixedColor(Value: TColor);
procedure SetFixedCols(Value: Integer);
procedure SetFixedRows(Value: Integer);
procedure SetGridLineWidth(Value: Integer);
procedure SetLeftCol(Value: Longint);
procedure SetOptions(Value: TGridOptions);
procedure SetRow(Value: Longint);
procedure SetRowCount(Value: Longint);
procedure SetRowHeights(Index: Longint; Value: Integer);
procedure SetScrollBars(Value: TScrollStyle);
procedure SetSelection(Value: TGridRect);
procedure SetTabStops(Index: Longint; Value: Boolean);
procedure SetTopRow(Value: Longint);
procedure UpdateEdit;
procedure UpdateText;
procedure WriteColWidths(Writer: TWriter);
procedure WriteRowHeights(Writer: TWriter);
procedure CMCancelMode(var Msg: TMessage); message CM_CANCELMODE;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
procedure CMDesignHitTest(var Msg: TCMDesignHitTest); message CM_DESIGNHITTEST;
procedure CMWantSpecialKey(var Msg: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
procedure WMChar(var Msg: TWMChar); message WM_CHAR;
procedure WMCancelMode(var Msg: TWMCancelMode); message WM_CANCELMODE;
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
procedure WMKillFocus(var Msg: TWMKillFocus); message WM_KILLFOCUS;
procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
procedure WMSetCursor(var Msg: TWMSetCursor); message WM_SETCURSOR;
procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
procedure WMSize(var Msg: TWMSize); message WM_SIZE;
procedure WMTimer(var Msg: TWMTimer); message WM_TIMER;
procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
protected
FGridState: TGridState;
FSaveCellExtents: Boolean;
DesignOptionsBoost: TGridOptions;
VirtualView: Boolean;
procedure CalcDrawInfo(var DrawInfo: TGridDrawInfo);
procedure CalcFixedInfo(var DrawInfo: TGridDrawInfo);
procedure CalcSizingState(X, Y: Integer; var State: TGridState;
var Index: Longint; var SizingPos, SizingOfs: Integer;
var FixedInfo: TGridDrawInfo); virtual;
procedure ChangeGridOrientation(RightToLeftOrientation: Boolean);
function CreateEditor: TInplaceEdit; virtual;
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); reintroduce; dynamic;
function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
procedure DoExit; override;
function CellRect(ACol, ARow: Longint): TRect;
function CanEditAcceptKey(Key: Char): Boolean; dynamic;
function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
function CanEditModify: Boolean; dynamic;
function CanEditShow: Boolean; virtual;
function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override;
function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override;
procedure FocusCell(ACol, ARow: Longint; MoveAnchor: Boolean);
function GetEditText(ACol, ARow: Longint): string; dynamic;
procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
function GetEditLimit: Integer; dynamic;
function GetEditMask(ACol, ARow: Longint): string; dynamic;
function GetEditStyle(ACol, ARow: Longint): TEditStyle; dynamic;
function GetGridWidth: Integer;
function GetGridHeight: Integer;
procedure HideEdit;
procedure HideEditor;
procedure ShowEditor;
procedure ShowEditorChar(Ch: Char);
procedure InvalidateEditor;
procedure InvalidateGrid;
procedure MoveColumn(FromIndex, ToIndex: Longint);
procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
procedure MoveRow(FromIndex, ToIndex: Longint);
procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
AState: TGridDrawState); virtual; abstract;
procedure DefineProperties(Filer: TFiler); override;
procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
function SelectCell(ACol, ARow: Longint): Boolean; virtual;
procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
function Sizing(X, Y: Integer): Boolean;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -