📄 unitasgrids.pas
字号:
{*******************************************************
仿速达DBGrid制作的CustonNewGrid
By wr960204 王锐
2004/8/8
*******************************************************}
unit UnitASGrids;
{$R-,T-,H+,X+}
interface
uses
UnitASBase, Dialogs, UnitASInplaceEditBase,
Messages, Windows, SysUtils, Classes, UnitASUtils,
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 }
TCustomASGrid = class;
TInplaceEdit = class(TCustomASInplaceEditBase)
private
FGrid: TCustomASGrid;
FClickTime: Longint;
procedure InternalMove(const Loc: TRect; Redraw: Boolean);
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;
procedure SetGrid(Value: TCustomASGrid);
property Grid: TCustomASGrid 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; override; //reintroduce;
procedure UpdateLoc(const Loc: TRect);
function Visible: Boolean;
property ChineseCurrency;
end;
{ TCustomASGrid }
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;
TCustomASGrid = class(TASBase)
private
FAnchor: TGridCoord;
FBorderStyle: TBorderStyle;
FColCount: Longint;
FColWidths: Pointer;
FColFixed: 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;
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;
procedure UpdateText;
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 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
FCanEditModify: Boolean;
FGridState: TGridState;
FInplaceCol, FInplaceRow: Longint;
FInplaceEdit: TInplaceEdit;
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); virtual;
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; virtual;
procedure HideEditor; virtual;
procedure ShowEditor; virtual;
procedure ShowEditorChar(Ch: Char); virtual;
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;
procedure ScrollData(DX, DY: Integer);
procedure InvalidateCell(ACol, ARow: Longint);
procedure InvalidateCol(ACol: Longint);
procedure InvalidateRow(ARow: Longint);
procedure TopLeftChanged; dynamic;
procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
procedure Paint; override;
procedure ColWidthsChanged; dynamic;
procedure RowHeightsChanged; dynamic;
procedure DeleteColumn(ACol: Longint); virtual;
procedure DeleteRow(ARow: Longint); virtual;
procedure UpdateDesigner;
procedure UpdateEdit; virtual;
function BeginColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
function BeginRowDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
function CheckColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
function CheckRowDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
function EndColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
function EndRowDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; dynamic;
property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle
default bsSingle;
property Col: Longint read FCurrent.X write SetCol;
property Color default clWindow;
property ColCount: Longint read FColCount write SetColCount default 5;
property ColWidths[Index: Longint]: Integer read GetColWidths write
SetColWidths;
property DefaultColWidth: Integer read FDefaultColWidth write
SetDefaultColWidth default 64;
property DefaultDrawing: Boolean read FDefaultDrawing write FDefaultDrawing
default True;
property DefaultRowHeight: Integer read FDefaultRowHeight write
SetDefaultRowHeight default 24;
property EditorMode: Boolean read FEditorMode write SetEditorMode;
property FixedColor: TColor read FFixedColor write SetFixedColor default
clBtnFace;
property FixedCols: Integer read FFixedCols write SetFixedCols default 1;
property FixedRows: Integer read FFixedRows write SetFixedRows default 1;
property GridHeight: Integer read GetGridHeight;
property GridLineWidth: Integer read FGridLineWidth write SetGridLineWidth
default 1;
property GridWidth: Integer read GetGridWidth;
property HitTest: TPoint read FHitTest;
property InplaceEditor: TInplaceEdit read FInplaceEdit;
property LeftCol: Longint read FTopLeft.X write SetLeftCol;
property Options: TGridOptions read FOptions write SetOptions
default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
goRangeSelect];
property ParentColor default False;
property Row: Longint read FCurrent.Y write SetRow;
property RowCount: Longint read FRowCount write SetRowCount default 5;
property RowHeights[Index: Longint]: Integer read GetRowHeights write
SetRowHeights;
property ScrollBars: TScrollStyle read FScrollBars write SetScrollBars
default ssBoth;
property Selection: TGridRect read GetSelection write SetSelection;
property TabStops[Index: Longint]: Boolean read GetTabStops write
SetTabStops;
property TopRow: Longint read FTopLeft.Y write SetTopRow;
property VisibleColCount: Integer read GetVisibleColCount;
property VisibleRowCount: Integer read GetVisibleRowCount;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function MouseCoord(X, Y: Integer): TGridCoord;
published
property TabStop default True;
end;
TGetEditEvent = procedure(Sender: TObject; ACol, ARow: Longint; var Value:
string) of object;
TSetEditEvent = procedure(Sender: TObject; ACol, ARow: Longint; const Value:
string) of object;
TMovedEvent = procedure(Sender: TObject; FromIndex, ToIndex: Longint) of
object;
TOnGetPickListItems = procedure(ACol, ARow: Integer; Items: TStrings) of
object;
TInplaceEditList = class(TInPlaceEdit)
private
FButtonWidth: Integer;
FPickList: TCustomListbox;
FActiveList: TWinControl;
FEditStyle: TEditStyle;
FDropDownRows: Integer;
FListVisible: Boolean;
FTracking: Boolean;
FPressed: Boolean;
FPickListLoaded: Boolean;
FOnGetPickListitems: TOnGetPickListItems;
FOnEditButtonClick: TNotifyEvent;
FMouseInControl: Boolean;
function GetPickList: TCustomListbox;
procedure CMCancelMode(var Message: TCMCancelMode); message CM_CancelMode;
procedure WMCancelMode(var Message: TMessage); message WM_CancelMode;
procedure WMKillFocus(var Message: TMessage); message WM_KillFocus;
procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message
wm_LButtonDblClk;
procedure WMPaint(var Message: TWMPaint); message wm_Paint;
procedure WMSetCursor(var Message: TWMSetCursor); message WM_SetCursor;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
protected
procedure BoundsChanged; override;
function ButtonRect: TRect;
procedure CloseUp(Accept: Boolean); dynamic;
procedure DblClick; override;
procedure DoDropDownKeys(var Key: Word; Shift: TShiftState); virtual;
procedure DoEditButtonClick; virtual;
procedure DoGetPickListItems; dynamic;
procedure DropDown; dynamic;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure ListMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
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;
function OverButton(const P: TPoint): Boolean;
procedure PaintWindow(DC: HDC); override;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -