📄 newdbgrids.~pas
字号:
procedure TDBGridColumns.LoadFromFile(const Filename: string);
var
S: TFileStream;
begin
S := TFileStream.Create(Filename, fmOpenRead);
try
LoadFromStream(S);
finally
S.Free;
end;
end;
type
TColumnsWrapper = class(TComponent)
private
FColumns: TDBGridColumns;
published
property Columns: TDBGridColumns read FColumns write FColumns;
end;
procedure TDBGridColumns.LoadFromStream(S: TStream);
var
Wrapper: TColumnsWrapper;
begin
Wrapper := TColumnsWrapper.Create(nil);
try
Wrapper.Columns := FGrid.CreateColumns;
S.ReadComponent(Wrapper);
Assign(Wrapper.Columns);
finally
Wrapper.Columns.Free;
Wrapper.Free;
end;
end;
procedure TDBGridColumns.RestoreDefaults;
var
I: Integer;
begin
BeginUpdate;
try
for I := 0 to Count-1 do
Items[I].RestoreDefaults;
finally
EndUpdate;
end;
end;
procedure TDBGridColumns.RebuildColumns;
procedure AddFields(Fields: TFields; Depth: Integer);
var
I: Integer;
begin
Inc(Depth);
for I := 0 to Fields.Count-1 do
begin
Add.FieldName := Fields[I].FullName;
if Fields[I].DataType in [ftADT, ftArray] then
AddFields((Fields[I] as TObjectField).Fields, Depth);
end;
end;
begin
if Assigned(FGrid) and Assigned(FGrid.DataSource) and
Assigned(FGrid.Datasource.Dataset) then
begin
FGrid.BeginLayout;
try
Clear;
AddFields(FGrid.Datasource.Dataset.Fields, 0);
finally
FGrid.EndLayout;
end
end
else
Clear;
end;
procedure TDBGridColumns.SaveToFile(const Filename: string);
var
S: TStream;
begin
S := TFileStream.Create(Filename, fmCreate);
try
SaveToStream(S);
finally
S.Free;
end;
end;
procedure TDBGridColumns.SaveToStream(S: TStream);
var
Wrapper: TColumnsWrapper;
begin
Wrapper := TColumnsWrapper.Create(nil);
try
Wrapper.Columns := Self;
S.WriteComponent(Wrapper);
finally
Wrapper.Free;
end;
end;
procedure TDBGridColumns.SetColumn(Index: Integer; Value: TColumn);
begin
Items[Index].Assign(Value);
end;
procedure TDBGridColumns.SetState(NewState: TDBGridColumnsState);
begin
if NewState = State then Exit;
if NewState = csDefault then
Clear
else
RebuildColumns;
end;
procedure TDBGridColumns.Update(Item: TCollectionItem);
var
Raw: Integer;
begin
if (FGrid = nil) or (csLoading in FGrid.ComponentState) then Exit;
if Item = nil then
begin
FGrid.LayoutChanged;
end
else
begin
Raw := FGrid.DataToRawColumn(Item.Index);
FGrid.InvalidateCol(Raw);
FGrid.ColWidths[Raw] := TColumn(Item).Width;
end;
end;
function TDBGridColumns.InternalAdd: TColumn;
begin
Result := Add;
Result.IsStored := False;
end;
function TDBGridColumns.GetState: TDBGridColumnsState;
begin
Result := TDBGridColumnsState((Count > 0) and Items[0].IsStored);
end;
{ TBookmarkList }
constructor TBookmarkList.Create(AGrid: TCustomDBGrid0);
begin
inherited Create;
FList := TStringList.Create;
FList.OnChange := StringsChanged;
FGrid := AGrid;
end;
destructor TBookmarkList.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
procedure TBookmarkList.Clear;
begin
if FList.Count = 0 then Exit;
FList.Clear;
FGrid.Invalidate;
end;
function TBookmarkList.Compare(const Item1, Item2: TBookmarkStr): Integer;
begin
with FGrid.Datalink.Datasource.Dataset do
Result := CompareBookmarks(TBookmark(Item1), TBookmark(Item2));
end;
function TBookmarkList.CurrentRow: TBookmarkStr;
begin
if not FLinkActive then RaiseGridError(sDataSetClosed);
Result := FGrid.Datalink.Datasource.Dataset.Bookmark;
end;
function TBookmarkList.GetCurrentRowSelected: Boolean;
var
Index: Integer;
begin
Result := Find(CurrentRow, Index);
end;
function TBookmarkList.Find(const Item: TBookmarkStr; var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
if (Item = FCache) and (FCacheIndex >= 0) then
begin
Index := FCacheIndex;
Result := FCacheFind;
Exit;
end;
Result := False;
L := 0;
H := FList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := Compare(FList[I], Item);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
FCache := Item;
FCacheIndex := Index;
FCacheFind := Result;
end;
function TBookmarkList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TBookmarkList.GetItem(Index: Integer): TBookmarkStr;
begin
Result := FList[Index];
end;
function TBookmarkList.IndexOf(const Item: TBookmarkStr): Integer;
begin
if not Find(Item, Result) then
Result := -1;
end;
procedure TBookmarkList.LinkActive(Value: Boolean);
begin
Clear;
FLinkActive := Value;
end;
procedure TBookmarkList.Delete;
var
I: Integer;
begin
with FGrid.Datalink.Datasource.Dataset do
begin
DisableControls;
try
for I := FList.Count-1 downto 0 do
begin
Bookmark := FList[I];
Delete;
FList.Delete(I);
end;
finally
EnableControls;
end;
end;
end;
function TBookmarkList.Refresh: Boolean;
var
I: Integer;
begin
Result := False;
with FGrid.DataLink.Datasource.Dataset do
try
CheckBrowseMode;
for I := FList.Count - 1 downto 0 do
if not BookmarkValid(TBookmark(FList[I])) then
begin
Result := True;
FList.Delete(I);
end;
finally
UpdateCursorPos;
if Result then FGrid.Invalidate;
end;
end;
procedure TBookmarkList.SetCurrentRowSelected(Value: Boolean);
var
Index: Integer;
Current: TBookmarkStr;
begin
Current := CurrentRow;
if (Length(Current) = 0) or (Find(Current, Index) = Value) then Exit;
if Value then
FList.Insert(Index, Current)
else
FList.Delete(Index);
FGrid.InvalidateRow(FGrid.Row);
end;
procedure TBookmarkList.StringsChanged(Sender: TObject);
begin
FCache := '';
FCacheIndex := -1;
end;
{ TCustomDBGrid }
var
DrawBitmap: TBitmap;
UserCount: Integer;
procedure UsesBitmap;
begin
if UserCount = 0 then
DrawBitmap := TBitmap.Create;
Inc(UserCount);
end;
procedure ReleaseBitmap;
begin
Dec(UserCount);
if UserCount = 0 then DrawBitmap.Free;
end;
procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
const Text: string; Alignment: TAlignment; ARightToLeft: Boolean);
const
AlignFlags : array [TAlignment] of Integer =
( DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_RIGHT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
RTL: array [Boolean] of Integer = (0, DT_RTLREADING);
var
B, R: TRect;
Hold, Left: Integer;
I: TColorRef;
begin
I := ColorToRGB(ACanvas.Brush.Color);
if GetNearestColor(ACanvas.Handle, I) = I then
begin { Use ExtTextOut for solid colors }
{ In BiDi, because we changed the window origin, the text that does not
change alignment, actually gets its alignment changed. }
if (ACanvas.CanvasOrientation = coRightToLeft) and (not ARightToLeft) then
ChangeBiDiModeAlignment(Alignment);
case Alignment of
taLeftJustify:
Left := ARect.Left + DX;
taRightJustify:
Left := ARect.Right - ACanvas.TextWidth(Text) - 3;
else { taCenter }
Left := ARect.Left + (ARect.Right - ARect.Left) shr 1
- (ACanvas.TextWidth(Text) shr 1);
end;
ACanvas.TextRect(ARect, Left, ARect.Top + DY, Text);
end
else begin { Use FillRect and Drawtext for dithered colors }
DrawBitmap.Canvas.Lock;
try
with DrawBitmap, ARect do { Use offscreen bitmap to eliminate flicker and }
begin { brush origin tics in painting / scrolling. }
Width := Max(Width, Right - Left);
Height := Max(Height, Bottom - Top);
R := Rect(DX, DY, Right - Left - 1, Bottom - Top - 1);
B := Rect(0, 0, Right - Left, Bottom - Top);
end;
with DrawBitmap.Canvas do
begin
Font := ACanvas.Font;
Font.Color := ACanvas.Font.Color;
Brush := ACanvas.Brush;
Brush.Style := bsSolid;
FillRect(B);
SetBkMode(Handle, TRANSPARENT);
if (ACanvas.CanvasOrientation = coRightToLeft) then
ChangeBiDiModeAlignment(Alignment);
DrawText(Handle, PChar(Text), Length(Text), R,
AlignFlags[Alignment] or RTL[ARightToLeft]);
end;
if (ACanvas.CanvasOrientation = coRightToLeft) then
begin
Hold := ARect.Left;
ARect.Left := ARect.Right;
ARect.Right := Hold;
end;
ACanvas.CopyRect(ARect, DrawBitmap.Canvas, B);
finally
DrawBitmap.Canvas.Unlock;
end;
end;
end;
constructor TCustomDBGrid0.Create(AOwner: TComponent);
var
Bmp: TBitmap;
begin
inherited Create(AOwner);
inherited DefaultDrawing := False;
FAcquireFocus := True;
Bmp := TBitmap.Create;
try
Bmp.LoadFromResourceName(HInstance, bmArrow);
FIndicators := TImageList.CreateSize(Bmp.Width, Bmp.Height);
FIndicators.AddMasked(Bmp, clWhite);
Bmp.LoadFromResourceName(HInstance, bmEdit);
FIndicators.AddMasked(Bmp, clWhite);
Bmp.LoadFromResourceName(HInstance, bmInsert);
FIndicators.AddMasked(Bmp, clWhite);
Bmp.LoadFromResourceName(HInstance, bmMultiDot);
FIndicators.AddMasked(Bmp, clWhite);
Bmp.LoadFromResourceName(HInstance, bmMultiArrow);
FIndicators.AddMasked(Bmp, clWhite);
finally
Bmp.Free;
end;
FTitleOffset := 1;
FIndicatorOffset := 1;
FUpdateFields := True;
FOptions := [dgEditing, dgTitles, dgIndicator, dgColumnResize,
dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit];
if SysLocale.PriLangID = LANG_KOREAN then
Include(FOptions, dgAlwaysShowEditor);
DesignOptionsBoost := [goColSizing];
VirtualView := True;
UsesBitmap;
ScrollBars := ssHorizontal;
inherited Options := [goFixedHorzLine, goFixedVertLine, goHorzLine,
goVertLine, goColSizing, goColMoving, goTabs, goEditing];
FColumns := CreateColumns;
FVisibleColumns := TList.Create;
inherited RowCount := 2;
inherited ColCount := 2;
FDataLink := CreateDataLink;
Color := clWindow;
ParentColor := False;
FTitleFont := TFont.Create;
FTitleFont.OnChange := TitleFontChanged;
FSaveCellExtents := False;
FUserChange := True;
FDefaultDrawing := True;
FBookmarks := TBookmarkList.Create(Self);
HideEditor;
end;
destructor TCustomDBGrid0.Destroy;
begin
FColumns.Free;
FColumns := nil;
FVisibleColumns.Free;
FVisibleColumns := nil;
FDataLink.Free;
FDataLink := nil;
FIndicators.Free;
FTitleFont.Free;
FTitleFont := nil;
FBookmarks.Free;
FBookmarks
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -