wyn兄的的swDBGrid只能在delphi6里使用。为什么你不继承一个???
看看我写的吧!
{********************************************}
{ }
{ ZebraDBGrid version 1.2 }
{ }
{ ELIESER MORAIS DOS REIS }
{ Copyright(C) 1999. All rights reserved. }
{ }
{ Please send bugs, comments and }
{ sugestions to: }
{ }
{ elieser@startrekmail.com }
{ }
{********************************************}
{############################################
# #
# THIS COMPONENT IS DEDICATED TO MY FRIEND #
# C A R L O S A U G U S T O #
# #
############################################}
unit zebdbgrid;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, DB;
type
TDrawFieldCellEvent = procedure (Sender: TObject; Field : TField;
Var Color : TColor; Var Font : TFont; Row: longint) of object;
Tzebdbgrid = class(TCustomDBGrid)
private
OldColumn:TColumn;
OldColor:TColor;
FZebra: Boolean;
FZebraColor, FZebraFontColor: TColor;
FSelectColor: TColor;
FDrawFieldCellEvent : TDrawFieldCellEvent;
procedure SetZebra(Value: Boolean);
procedure SetZebraColor(Value: TColor);
procedure SetZebraFontColor(Value: TColor);
procedure SetSelectColor(Value: TColor);
protected
procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
AState: TGridDrawState); override;
procedure Scroll(Distance: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
property Canvas;
property SelectedRows;
published
property Align;
property BorderStyle;
property Color;
property Columns stored False;
property Ctl3D;
property DataSource;
property DefaultDrawing;
property DragCursor;
property DragMode;
property Enabled;
property FixedColor;
property Font;
property ImeMode;
property ImeName;
property Options;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property TitleFont;
property Visible;
property Zebra: Boolean read FZebra write SetZebra default False;
property ZebraColor: TCOlor read FZebraColor write SetZebraColor
default clWindowText;
property ZebraFontColor: TColor read FZebraFontColor write SetZebraFontColor
default clAqua;
property SelectColor: TCOlor read FSelectColor write SetSelectColor
default clBlue;
property OnCellClick;
property OnColEnter;
property OnColExit;
property OnColumnMoved;
property OnDrawDataCell;
property OnDrawColumnCell;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEditButtonClick;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnStartDrag;
property OnTitleClick;
property OnMouseDown;
property OnMOuseMove;
property OnMouseUp;
property OnEndDock;
end;
procedure Register;
implementation
{
var
DrawBitmap: TBitmap;
UserCount: Integer;
procedure UsesBitmap;
begin
if UserCount = 0 then
DrawBitmap := TBitmap.Create;
Inc(UserCount);
end;
procedure ReleaseBitmap;
begin
Dec(UserCount);
end;
function Max(X, Y: Integer): Integer;
begin
Result := Y;
if X > Y then Result := X;
end;
procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
const Text: string; Alignment: TAlignment);
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 );
var
B, R: TRect;
I, Left: Integer;
begin
I := ColorToRGB(ACanvas.Brush.Color);
if GetNearestColor(ACanvas.Handle, I) = I then
begin
case Alignment of
taLeftJustify:
Left := ARect.Left + DX;
taRightJustify:
Left := ARect.Right - ACanvas.TextWidth(Text) - 3;
else
Left := ARect.Left + (ARect.Right - ARect.Left) shr 1
- (ACanvas.TextWidth(Text) shr 1);
end;
ExtTextOut(ACanvas.Handle, Left, ARect.Top + DY, ETO_OPAQUE or
ETO_CLIPPED, @ARect, PChar(Text), Length(Text), nil);
end else
begin
with DrawBitmap, ARect do
begin
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);
DrawText(Handle, PChar(Text), Length(Text), R, AlignFlags[Alignment]);
end;
ACanvas.CopyRect(ARect, DrawBitmap.Canvas, B);
end;
end;
}
constructor Tzebdbgrid.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FZebra := True;
FZebraColor := clAqua;
FZebraFontColor := clWindowText;
FSelectColor := clYellow;
Options:=Options+[dgRowSelect]-[dgIndicator];
// UsesBitmap;
end;
destructor Tzebdbgrid.Destroy;
begin
// ReleaseBitmap;
inherited Destroy;
end;
procedure Tzebdbgrid.SetZebra(Value: Boolean);
begin
FZebra := Value;
Refresh;
end;
procedure Tzebdbgrid.SetZebraColor(Value: TColor);
begin
FZebraColor := Value;
Refresh;
end;
procedure Tzebdbgrid.SetZebraFontColor(Value: TColor);
begin
FZebraFontColor := Value;
Refresh;
end;
procedure Tzebdbgrid.SetSelectColor(Value: TColor);
begin
if not (dgRowSelect in Options) then
exit;
FSelectColor := Value;
Refresh;
end;
procedure Tzebdbgrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
AState: TGridDrawState);
var
OldActive: Integer;
// Indicator: Integer;
Highlight: Boolean;
Value: string;
DrawColumn: TColumn;
// FrameOffs: Byte;
cl: TColor;
fn: TFont;
begin
if csLoading in ComponentState then
begin
Canvas.Brush.Color := Color;
Canvas.FillRect(ARect);
Exit;
end;
if (gdFixed in AState) and (ACol - IndicatorOffset < 0) then begin
inherited DrawCell(ACol, ARow, ARect, AState);
exit;
end;
if (dgTitles in Options) And (ARow = 0) then begin
inherited DrawCell(ACol, ARow, ARect, AState);
exit;
end;
if(dgTitles in Options) then
Dec(ARow);
Dec(ACol, IndicatorOffset);
if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
[dgRowLines, dgColLines]) then
begin
InflateRect(ARect, -1, -1);
// FrameOffs := 1;
end;
// else
// FrameOffs := 2;
with Canvas do
begin
DrawColumn := Columns[ACol];
Font := DrawColumn.Font;
Brush.Color := DrawColumn.Color;
Font.Color := DrawColumn.Font.Color;
if FZebra and Odd(ARow) then
begin
Brush.Color := FZebraColor;
Font.Color := FZebraFontColor;
end;
//---
if (dgRowSelect in Options)and(gdSelected in AState)then
begin
Brush.color:=FSelectColor;
end;
//---
if (DataLink = nil) or not DataLink.Active then
FillRect(ARect) else
begin
Value := '';
OldActive := DataLink.ActiveRecord;
try
DataLink.ActiveRecord := ARow;
if Assigned(DrawColumn.Field) then
begin
Value := DrawColumn.Field.DisplayText;
if Assigned(FDrawFieldCellEvent) then
begin
cl := Brush.Color;
fn := Font;
FDrawFieldCellEvent(self, DrawColumn.Field, cl, fn, ARow);
Brush.Color := cl;
Font := fn;
end;
end;
Highlight := HighlightCell(ACol, ARow, Value, AState);
if Highlight and (not FZebra) then
begin
Brush.Color := clHighlight;
Font.Color := clHighlightText;
end;
if DefaultDrawing then
DefaultDrawColumnCell(ARect, ACol, DrawColumn, AState);
if Columns.State = csDefault then
DrawDataCell(ARect, DrawColumn.Field, AState);
DrawColumnCell(ARect, ACol, DrawColumn, AState);
finally
DataLink.ActiveRecord := OldActive;
end;
if DefaultDrawing and (gdSelected in AState)
and ((dgAlwaysShowSelection in Options) or Focused)
and not (csDesigning in ComponentState)
and not (dgRowSelect in Options)
and (UpdateLock = 0)
and (ValidParentForm(Self).ActiveControl = Self) then
Windows.DrawFocusRect(Handle, ARect);
end;
end;
if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
[dgRowLines, dgColLines]) then
begin
InflateRect(ARect, 1, 1);
DrawEdge(Canvas.Handle, ARect, BDR_RAISEDINNER, BF_BOTTOMRIGHT);
DrawEdge(Canvas.Handle, ARect, BDR_RAISEDINNER, BF_TOPLEFT);
end;
end;
procedure Tzebdbgrid.Scroll(Distance: Integer);
begin
inherited Scroll(Distance);
if FZebra then Refresh;
end;
procedure Tzebdbgrid.MouseMove(Shift: TShiftState;X, Y: Integer);
var
Cell: TGridCoord;
DrawColumn: TColumn;
begin
inherited MouseMove(Shift, X, Y);
if csDesigning in ComponentState then
exit;
Cell := MouseCoord(X, Y);
if (Cell.X < 0) and (Cell.Y < 0) then
begin
Exit;
end;
DrawColumn :=Columns[Cell.x];
if OldColumn=nil then
begin
OldColumn:=DrawColumn;
OldColor:=DrawColumn.Title.Font.Color ;
Exit;
end;
if (OldColumn=DrawColumn) then
exit;
try
OldColumn.Title.Font.Color :=OldColor;
OldColor:=DrawColumn.Title.Font.Color ;
OldColumn:=DrawColumn;
DrawColumn.Title.Font.Color :=clBlue;
except
OldColumn:=DrawColumn;
DrawColumn.Title.Font.Color :=clBlue;
end;
end;
procedure Register;
begin
RegisterComponents('Yyy', [Tzebdbgrid]);
end;
end.