你这个问题比较麻烦,
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
ARect1: TRect;
CheckState: TCheckBoxState;
function iif(Condition: Boolean; V1, V2: Integer): Integer;
begin
if (Condition) then Result := V1 else Result := V2;
end;
function RectSize(ARect: TRect): TSize;
begin
Result.cx := ARect.Right - ARect.Left;
Result.cy := ARect.Bottom - ARect.Top;
end;
procedure DrawCheck(DC: HDC; R: TRect; AState: TCheckBoxState; AEnabled, AFlat, ADown, AActive: Boolean);
var
DrawState, oldRgn: Integer;
DrawRect: TRect;
Rgn, SaveRgn: HRgn;
begin
SaveRgn := 0;
oldRgn := 0;
DrawRect := R;
with DrawRect do
if (Right - Left) > (Bottom - Top) then
begin
Left := Left + ((Right - Left) - (Bottom - Top)) div 2;
Right := Left + (Bottom - Top);
end else if (Right - Left) < (Bottom - Top) then
begin
Top := Top + ((Bottom - Top) - (Right - Left)) div 2;
Bottom := Top + (Right - Left);
end;
case AState of
cbChecked:
DrawState := DFCS_BUTTONCHECK or DFCS_CHECKED;
cbUnchecked:
DrawState := DFCS_BUTTONCHECK;
else
DrawState := DFCS_BUTTON3STATE or DFCS_CHECKED;
end;
if not AEnabled then
DrawState := DrawState or DFCS_INACTIVE;
if ADown then
DrawState := DrawState or DFCS_PUSHED;
if AFlat then
begin
SaveRgn := CreateRectRgn(0, 0, 0, 0);
oldRgn := GetClipRgn(DC, SaveRgn);
with DrawRect do
Rgn := CreateRectRgn(Left + 1, Top + 1, Right - 1, Bottom - 1);
SelectClipRgn(DC, Rgn);
DeleteObject(Rgn);
end;
if AFlat then InflateRect(DrawRect, 1, 1);
DrawFrameControl(DC, DrawRect, DFC_BUTTON, DrawState);
if AFlat then
begin
if oldRgn = 0 then
SelectClipRgn(DC, 0)
else
SelectClipRgn(DC, SaveRgn);
DeleteObject(SaveRgn);
InflateRect(DrawRect, -1, -1);
if AActive
then FrameRect(DC, DrawRect, GetSysColorBrush(COLOR_BTNFACE))
else FrameRect(DC, DrawRect, GetSysColorBrush(COLOR_BTNSHADOW));
end;
end;
procedure PaintCheckBox(DC: HDC; ARect: TRect; ParentColor: TColor;
Flat, Active, Enabled: Boolean; State: TCheckBoxState);
var
ASize: TSize;
begin
ASize := RectSize(ARect);
if ASize.cx < ASize.cy then
begin
ARect.Top := ARect.Top + (ASize.cy - ASize.cx) div 2;
ARect.Bottom := ARect.Bottom - (ASize.cy - ASize.cx) div 2 - (ASize.cy - ASize.cx) mod 2;
end else if ASize.cx > ASize.cy then
begin
ARect.Left := ARect.Left + (ASize.cx - ASize.cy) div 2;
ARect.Right := ARect.Right - (ASize.cx - ASize.cy) div 2 - (ASize.cx - ASize.cy) mod 2;
end;
DrawCheck(DC, ARect, State, Enabled, Flat, False, Active);
end;
begin
with DBGrid1 do
if Column.Field.DataType = ftBoolean then
begin
if Column.Field.AsBoolean then
CheckState := cbChecked
else
CheckState := cbUnchecked;
Canvas.FillRect(Rect);
ARect1.Left := Rect.Left + iif(Rect.Right - Rect.Left < DefaultCheckBoxWidth, 0,
(Rect.Right - Rect.Left) shr 1 - DefaultCheckBoxWidth shr 1);
ARect1.Right := iif(Rect.Right - Rect.Left < DefaultCheckBoxWidth, Rect.Right,
ARect1.Left + DefaultCheckBoxWidth);
ARect1.Top := Rect.Top + iif(Rect.Bottom - Rect.Top < DefaultCheckBoxHeight, 0,
(Rect.Bottom - Rect.Top) shr 1 - DefaultCheckBoxHeight shr 1);
ARect1.Bottom := iif(Rect.Bottom - Rect.Top < DefaultCheckBoxHeight, Rect.Bottom,
ARect1.Top + DefaultCheckBoxHeight);
PaintCheckBox(Canvas.Handle, ARect1,
Canvas.Brush.Color, False, False, True, CheckState);
end;
end;