📄 ehgrids.pas
字号:
{*******************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 1995,99 Inprise Corporation }
{ }
{*******************************************************}
unit EHGrids;
{$R-,T-,H+,X+}
interface
uses Messages, Windows, SysUtils, Classes, Graphics, Menus, Controls, Forms,
StdCtrls, Mask,Grids;
type
TCustomGridEh = class;
TInplaceEditEh = class(TCustomMaskEdit)
private
FGrid: TCustomGridEh;
FClickTime: Longint;
procedure InternalMove(const Loc: TRect; Redraw: Boolean);
procedure SetGrid(Value: TCustomGridEh);
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: TCustomGridEh 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;
TEditorChangedEvent = procedure (Sender: TObject; isShowing:Boolean; Const Bounds:TRect;
ACol,ARow:Longint) of object;
TCustomGridEh = 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: TInplaceEditEh;
FInplaceCol, FInplaceRow: Longint;
FColOffset: Integer;
FDefaultDrawing: Boolean;
FEditorMode: Boolean;
//Dennis Added start
FLineColor: TColor;
FSelectedColor: TColor;
FSelectedFontColor: TColor;
FOnEditorChanged:TEditorChangedEvent;
//Dennis Added end
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 ChangeGridOrientation(RightToLeftOrientation: Boolean);
procedure ChangeSize(NewColCount, NewRowCount: Longint);
procedure ClampInView(const Coord: TGridCoord);
procedure DrawSizingLine(const DrawInfo: TGridDrawInfo);
procedure DrawMove;
procedure FocusCell(ACol, ARow: Longint; MoveAnchor: Boolean);
procedure GridRectToScreenRect(GridRect: TGridRect;
var ScreenRect: TRect; IncludeLine: Boolean);
procedure HideEdit;
procedure Initialize;
procedure InvalidateGrid;
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;
procedure SetLineColor(Value: TColor);
function GetLineColor: TColor;
procedure SetSelectedColor(Value: TColor);
function GetSelectedColor: TColor;
procedure SetSelectedFontColor(Value: TColor);
function GetSelectedFontColor: TColor;
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;
function CreateEditor: TInplaceEditEh; 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;
function GetEditText(ACol, ARow: Longint): string; dynamic;
procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
function GetEditMask(ACol, ARow: Longint): string; dynamic;
function GetEditLimit: Integer; dynamic;
function GetGridWidth: Integer;
function GetGridHeight: Integer;
procedure HideEditor;
procedure ShowEditor;
procedure ShowEditorChar(Ch: Char);
procedure InvalidateEditor;
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;
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;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function MouseCoord(X, Y: Integer): TGridCoord;
property OnEditorChanged:TEditorChangedEvent Read FOnEditorChanged write FOnEditorChanged;
property Col: Longint read FCurrent.X write SetCol;
property Row: Longint read FCurrent.Y write SetRow;
property TopRow: Longint read FTopLeft.Y write SetTopRow;
property VisibleColCount: Integer read GetVisibleColCount;
property VisibleRowCount: Integer read GetVisibleRowCount;
property ColWidths[Index: Longint]: Integer read GetColWidths write SetColWidths;
property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
property Color default clWindow;
property ColCount: Longint read FColCount write SetColCount default 5;
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: TInplaceEditEh 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 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;
//Dennis Added
procedure EditorChanged(Sender:TObject; isShowing: Boolean; const NewBounds: TRect; ACol: Longint; ARow: Longint); dynamic;
//Dennis Added end;
published
property TabStop default True;
property LineColor: TColor read GetLineColor write SetLineColor;
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clHighLight;
property SelectedFontColor: TColor read GetSelectedFontColor write SetSelectedFontColor;
end;
implementation
uses Math, Consts;
type
PIntArray = ^TIntArray;
TIntArray = array[0..MaxCustomExtents] of Integer;
procedure InvalidOp(const id: string);
begin
raise EInvalidGridOperation.Create(id);
end;
function GridRect(Coord1, Coord2: TGridCoord): TGridRect;
begin
with Result do
begin
Left := Coord2.X;
if Coord1.X < Coord2.X then Left := Coord1.X;
Right := Coord1.X;
if Coord1.X < Coord2.X then Right := Coord2.X;
Top := Coord2.Y;
if Coord1.Y < Coord2.Y then Top := Coord1.Y;
Bottom := Coord1.Y;
if Coord1.Y < Coord2.Y then Bottom := Coord2.Y;
end;
end;
function PointInGridRect(Col, Row: Longint; const Rect: TGridRect): Boolean;
begin
Result := (Col >= Rect.Left) and (Col <= Rect.Right) and (Row >= Rect.Top)
and (Row <= Rect.Bottom);
end;
type
TXorRects = array[0..3] of TRect;
procedure XorRects(const R1, R2: TRect; var XorRects: TXorRects);
var
Intersect, Union: TRect;
function PtInRect(X, Y: Integer; const Rect: TRect): Boolean;
begin
with Rect do Result := (X >= Left) and (X <= Right) and (Y >= Top) and
(Y <= Bottom);
end;
function Includes(const P1: TPoint; var P2: TPoint): Boolean;
begin
with P1 do
begin
Result := PtInRect(X, Y, R1) or PtInRect(X, Y, R2);
if Result then P2 := P1;
end;
end;
function Build(var R: TRect; const P1, P2, P3: TPoint): Boolean;
begin
Build := True;
with R do
if Includes(P1, TopLeft) then
begin
if not Includes(P3, BottomRight) then BottomRight := P2;
end
else if Includes(P2, TopLeft) then BottomRight := P3
else Build := False;
end;
begin
FillChar(XorRects, SizeOf(XorRects), 0);
if not Bool(IntersectRect(Intersect, R1, R2)) then
begin
{ Don't intersect so its simple }
XorRects[0] := R1;
XorRects[1] := R2;
end
else
begin
UnionRect(Union, R1, R2);
if Build(XorRects[0],
Point(Union.Left, Union.Top),
Point(Union.Left, Intersect.Top),
Point(Union.Left, Intersect.Bottom)) then
XorRects[0].Right := Intersect.Left;
if Build(XorRects[1],
Point(Intersect.Left, Union.Top),
Point(Intersect.Right, Union.Top),
Point(Union.Right, Union.Top)) then
XorRects[1].Bottom := Intersect.Top;
if Build(XorRects[2],
Point(Union.Right, Intersect.Top),
Point(Union.Right, Intersect.Bottom),
Point(Union.Right, Union.Bottom)) then
XorRects[2].Left := Intersect.Right;
if Build(XorRects[3],
Point(Union.Left, Union.Bottom),
Point(Intersect.Left, Union.Bottom),
Point(Intersect.Right, Union.Bottom)) then
XorRects[3].Top := Intersect.Bottom;
end;
end;
procedure ModifyExtents(var Extents: Pointer; Index, Amount: Longint;
Default: Integer);
var
LongSize, OldSize: LongInt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -