📄 unitasdbgrids.pas
字号:
procedure LinkActive(Value: Boolean); virtual;
procedure Loaded; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
procedure Scroll(Distance: Integer); virtual;
procedure SetColumnAttributes; virtual;
procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
function StoreColumns: Boolean;
procedure TimedScroll(Direction: TGridScrollDirection); override;
procedure TitleClick(Column: TColumn); dynamic;
procedure TopLeftChanged; override;
function UseRightToLeftAlignmentForField(const AField: TField;
Alignment: TAlignment): Boolean;
function BeginColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; override;
function CheckColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; override;
function EndColumnDrag(var Origin, Destination: Integer;
const MousePt: TPoint): Boolean; override;
property Columns: TASDBGridColumns read FColumns write SetColumns;
property DefaultDrawing: Boolean read FDefaultDrawing write FDefaultDrawing
default True;
property DataLink: TGridDataLink read FDataLink;
property Fixed3D: Boolean read FFixed3D write SetFixed3D;
property IndicatorOffset: Byte read FIndicatorOffset;
property LayoutLock: Byte read FLayoutLock;
property Options: TASDBGridOptions read FOptions write SetOptions
default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines,
dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit];
property MultiTitile: Boolean read FMultiTitile write SetMultiTitile;
property ParentColor default False;
property ReadOnly: Boolean read FReadOnly write FReadOnly default False;
property SelectedRows: TBookmarkList read FBookmarks;
property TitleFont: TFont read FTitleFont write SetTitleFont;
property UpdateLock: Byte read FUpdateLock;
property OnColEnter: TNotifyEvent read FOnColEnter write FOnColEnter;
property OnColExit: TNotifyEvent read FOnColExit write FOnColExit;
property OnDrawDataCell: TDrawDataCellEvent read FOnDrawDataCell
write FOnDrawDataCell; { obsolete }
property OnDrawColumnCell: TDrawColumnCellEvent read FOnDrawColumnCell
write FOnDrawColumnCell;
property OnEditButtonClick: TColumnButtonClick read FOnEditButtonClick
write FOnEditButtonClick;
property OnColumnMoved: TMovedEvent read FOnColumnMoved write
FOnColumnMoved;
property OnCellClick: TASDBGridClickEvent read FOnCellClick write
FOnCellClick;
property OnTitleClick: TASDBGridClickEvent read FOnTitleClick write
FOnTitleClick;
procedure ShowEditor; override;
procedure ShowEditorChar(Ch: Char); override;
procedure Paint; override;
procedure UpdateEdit; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DefaultDrawDataCell(const Rect: TRect; Field: TField;
State: TGridDrawState); { obsolete }
procedure DefaultDrawColumnCell(const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState);
procedure DefaultHandler(var Msg); override;
function ExecuteAction(Action: TBasicAction): Boolean; override;
procedure ShowPopupEditor(Column: TColumn; X: Integer = Low(Integer);
Y: Integer = Low(Integer)); dynamic;
function UpdateAction(Action: TBasicAction): Boolean; override;
function ValidFieldIndex(FieldIndex: Integer): Boolean;
property EditorMode;
property FieldCount: Integer read GetFieldCount;
property Fields[FieldIndex: Integer]: TField read GetFields;
property SelectedField: TField read GetSelectedField write SetSelectedField;
property SelectedIndex: Integer read GetSelectedIndex write
SetSelectedIndex;
property DataSource: TDataSource read GetDataSource write SetDataSource;
end;
TASDBGrid = class(TCustomASDBGrid)
public
property Canvas;
property SelectedRows;
published
property Align;
property Anchors;
property BiDiMode;
property BorderStyle;
property Color;
property Columns stored False; //StoreColumns;
property Constraints;
property Ctl3D;
property DataSource;
property DefaultDrawing;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FixedColor;
property Fixed3D;
property Font;
property ImeMode;
property ImeName;
property MultiTitile;
property Options;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property TitleFont;
property Visible;
property OnCellClick;
property OnColEnter;
property OnColExit;
property OnColumnMoved;
property OnDrawDataCell; { obsolete }
property OnDrawColumnCell;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEditButtonClick;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
property OnTitleClick;
end;
//procedure DrawCurrencyFrame(ACanvas: TCanvas; ARect: TRect; Value:
// Currency);
const
IndicatorWidth = 11;
implementation
uses Math, DBConsts, VDBConsts, Dialogs;
{$R ASDBGrids.res}
const
bmArrow = 'NEWDBGARROW';
bmEdit = 'NEWDBEDIT';
bmInsert = 'NEWDBINSERT';
bmMultiDot = 'NEWDBMULTIDOT';
bmMultiArrow = 'NEWDBMULTIARROW';
MaxMapSize = (MaxInt div 2) div SizeOf(Integer); { 250 million }
{ Error reporting }
procedure RaiseGridError(const S: string);
begin
raise EInvalidGridOperation.Create(S);
end;
procedure KillMessage(Wnd: HWnd; Msg: Integer);
// Delete the requested message from the queue, but throw back
// any WM_QUIT msgs that PeekMessage may also return
var
M : TMsg;
begin
M.Message := 0;
if PeekMessage(M, Wnd, Msg, Msg, pm_Remove) and (M.Message = WM_QUIT) then
PostQuitMessage(M.wparam);
end;
type
TASDBGridInplaceEdit = class(TInplaceEditList)
private
FDataList: TDBLookupListBox;
FUseDataList: Boolean;
FLookupSource: TDatasource;
protected
procedure CloseUp(Accept: Boolean); override;
procedure DoEditButtonClick; override;
procedure DropDown; override;
procedure UpdateContents; override;
public
constructor Create(AOwner: TComponent); override;
property DataList: TDBLookupListBox read FDataList;
end;
constructor TASDBGridInplaceEdit.Create(AOwner: TComponent);
var
CCValue : Currency;
begin
inherited Create(Owner);
FLookupSource := TDataSource.Create(Self);
SetGrid(TCustomASDBGrid(AOwner));
{with TCustomASDBGrid(AOwner) do
begin
FChineseCurrency := Columns[selectedIndex].ChineseCurrencyStyle;
end;
}
end;
procedure TASDBGridInplaceEdit.CloseUp(Accept: Boolean);
var
MasterField : TField;
ListValue : Variant;
begin
if ListVisible then
begin
if GetCapture <> 0 then
SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
if ActiveList = DataList then
ListValue := DataList.KeyValue
else
if PickList.ItemIndex <> -1 then
ListValue := PickList.Items[Picklist.ItemIndex];
SetWindowPos(ActiveList.Handle, 0, 0, 0, 0, 0, SWP_NOZORDER or
SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_HIDEWINDOW);
ListVisible := False;
if Assigned(FDataList) then
FDataList.ListSource := nil;
FLookupSource.Dataset := nil;
Invalidate;
if Accept then
if ActiveList = DataList then
with TCustomASDBGrid(Grid), Columns[SelectedIndex].Field do
begin
MasterField := DataSet.FieldByName(KeyFields);
if MasterField.CanModify and FDataLink.Edit then
MasterField.Value := ListValue;
end
else
if (not VarIsNull(ListValue)) and EditCanModify then
with TCustomASDBGrid(Grid), Columns[SelectedIndex].Field do
Text := ListValue;
end;
end;
procedure TASDBGridInplaceEdit.DoEditButtonClick;
begin
with TCustomASDBGrid(Grid) do
begin
if Assigned(Columns[SelectedIndex].FonButtonClick) then
Columns[SelectedIndex].FonButtonClick(Grid, Columns[SelectedIndex])
else
TCustomASDBGrid(Grid).EditButtonClick;
end;
end;
procedure TASDBGridInplaceEdit.DropDown;
var
Column : TColumn;
begin
if not ListVisible then
begin
with TCustomASDBGrid(Grid) do
Column := Columns[SelectedIndex];
if ActiveList = FDataList then
with Column.Field do
begin
FDataList.Color := Color;
FDataList.Font := Font;
FDataList.RowCount := Column.DropDownRows;
FLookupSource.DataSet := LookupDataSet;
FDataList.KeyField := LookupKeyFields;
FDataList.ListField := LookupResultField;
FDataList.ListSource := FLookupSource;
FDataList.KeyValue := DataSet.FieldByName(KeyFields).Value;
end
else
if ActiveList = PickList then
begin
PickList.Items.Assign(Column.PickList);
DropDownRows := Column.DropDownRows;
end;
end;
inherited DropDown;
end;
procedure TASDBGridInplaceEdit.UpdateContents;
var
Column : TColumn;
begin
inherited UpdateContents;
if FUseDataList then
begin
if FDataList = nil then
begin
FDataList := TPopupDataList.Create(Self);
FDataList.Visible := False;
FDataList.Parent := Self;
FDataList.OnMouseUp := ListMouseUp;
end;
ActiveList := FDataList;
end;
with TCustomASDBGrid(Grid) do
Column := Columns[SelectedIndex];
Self.ReadOnly := Column.ReadOnly;
Font.Assign(Column.Font);
ImeMode := Column.ImeMode;
ImeName := Column.ImeName;
end;
{ TGridDataLink }
type
TIntArray = array[0..MaxMapSize] of Integer;
PIntArray = ^TIntArray;
constructor TGridDataLink.Create(AGrid: TCustomASDBGrid);
begin
inherited Create;
FGrid := AGrid;
VisualControl := True;
end;
destructor TGridDataLink.Destroy;
begin
ClearMapping;
inherited Destroy;
end;
function TGridDataLink.GetDefaultFields: Boolean;
var
I : Integer;
begin
Result := True;
if DataSet <> nil then
Result := DataSet.DefaultFields;
if Result and SparseMap then
for I := 0 to FFieldCount - 1 do
if FFieldMap[I] < 0 then
begin
Result := False;
Exit;
end;
end;
function TGridDataLink.GetFields(I: Integer): TField;
begin
if (0 <= I) and (I < FFieldCount) and (FFieldMap[I] >= 0) then
Result := DataSet.FieldList[FFieldMap[I]]
else
Result := nil;
end;
function TGridDataLink.AddMapping(const FieldName: string): Boolean;
var
Field : TField;
NewSize : Integer;
begin
Result := True;
if FFieldCount >= MaxMapSize then
RaiseGridError(STooManyColumns);
if SparseMap then
Field := DataSet.FindField(FieldName)
else
Field := DataSet.FieldByName(FieldName);
if FFieldCount = Length(FFieldMap) then
begin
NewSize := Length(FFieldMap);
if NewSize = 0 then
NewSize := 8
else
Inc(NewSize, NewSize);
if (NewSize < FFieldCount) then
NewSize := FFieldCount + 1;
if (NewSize > MaxMapSize) then
NewSize := MaxMapSize;
SetLength(FFieldMap, NewSize);
end;
if Assigned(Field) then
begin
FFieldMap[FFieldCount] := Dataset.FieldList.IndexOfObject(Field);
Field.FreeNotification(FGrid);
end
else
FFieldMap[FFieldCount] := -1;
Inc(FFieldCount);
end;
procedure TGridDataLink.ActiveChanged;
begin
if Active and Assigned(DataSource) then
if Assigned(DataSource.DataSet) then
if DataSource.DataSet.IsUnidirectional then
DatabaseError(SDataSetUnidirectional);
FGrid.LinkActive(Active);
FModified := False;
end;
procedure TGridDataLink.ClearMapping;
begin
FFieldMap := nil;
FFieldCount := 0;
end;
procedure TGridDataLink.Modified;
begin
FModified := True;
end;
procedure TGridDataLink.DataSetChanged;
begin
FGrid.DataChanged;
FModified := False;
end;
procedure TGridDataLink.DataSetScrolled(Distance: Integer);
begin
FGrid.Scroll(Distance);
end;
procedure TGridDataLink.LayoutChanged;
var
SaveState : Boolean;
begin
{ FLayoutFromDataset determines whether default column width is forced to
be at least wide enough for the column title. }
SaveState := FGrid.FLayoutFromDataset;
FGrid.FLayoutFromDataset := True;
try
FGrid.LayoutChanged;
finally
FGrid.FLayoutFromDataset := SaveState;
end;
inherited LayoutChanged;
end;
procedure TGridDataLink.FocusControl(Field: TFieldRef);
begin
if Assigned(Field) and Assigned(Field^) then
begin
FGrid.SelectedField := Field^;
if (FGrid.SelectedField = Field^) and FGrid.AcquireFocus then
begin
Field^ := nil;
FGrid.ShowEditor;
end;
end;
end;
procedure TGridDataLink.EditingChanged;
begin
FGrid.EditingChanged;
end;
procedure TGridDataLink.RecordChanged(Field: TField);
begin
FGrid.RecordChanged(Field);
FModified := False;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -