2017-03-16 3 views
1

J'utilise le code suivant pour afficher une case à cocher dans un DBGrid. Il fonctionne (au moins était prévu) à la fois sur le thème et non sur le thème de style VCL (testé dans XE2, XE7 et Berlin):Delphi: Cochez une case dans un DBGrid (sur le thème)

uses Vcl.Themes; 

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
const 
    CtrlState: Array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED); 
    CtrlStateXP: Array[Boolean] of TThemedButton = (tbCheckBoxUncheckedNormal, tbCheckBoxCheckedNormal); 
var 
    CheckBoxRectangle : TRect; 
    Details: TThemedElementDetails; 
begin 
    if Column.Field.DataType = ftBoolean then 
    begin 
    Column.Title.Alignment := taCenter; 
    Column.Alignment := taCenter; 
    TDBGrid(Sender).Canvas.FillRect(Rect); 

    if ThemeServices.ThemesEnabled then 
    begin 
     Details := ThemeServices.GetElementDetails(CtrlStateXP[Column.Field.AsBoolean]); 
     ThemeServices.DrawElement(TDBGrid(Sender).Canvas.Handle, Details, Rect); 
    end 
    else 
    begin 
     CheckBoxRectangle.Left := Rect.Left + 2; 
     CheckBoxRectangle.Right := Rect.Right - 2; 
     CheckBoxRectangle.Top := Rect.Top + 2; 
     CheckBoxRectangle.Bottom := Rect.Bottom - 2; 
     DrawFrameControl(TDBGrid(Sender).Canvas.Handle, CheckBoxRectangle, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]); 
    end; 
    end; 
end; 

Il fonctionne très bien, sauf quand je me déplace sur les lignes, le style devient 'étrange', comme ci-dessous:

themed issue

Toute aide? Merci.

+1

Le FillRect ne semble pas correct. Ne devrait-il pas y avoir des services thématiques pour dessiner les antécédents des parents? –

Répondre

1

Après quelques tentatives et recherches que j'ai réussi à adapter la méthode par moi-même, tout en ajoutant les lignes suivantes:

if not (gdFocused in State) then 
    Grid.Canvas.Brush.Color := StyleServices.GetSystemColor(clHighlight); 

Ici je partage la méthode complète. Il pourrait être utile à quelqu'un :-)

uses Vcl.Themes; 

procedure CheckBoxGrid(FieldName: String; ValueCheck: Variant; Grid: TDBGrid; const Rect: TRect; Column: TColumn; State: TGridDrawState); 
const 
    CtrlState: Array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED); 
    CtrlStateXP: Array[Boolean] of TThemedButton = (tbCheckBoxUncheckedNormal, tbCheckBoxCheckedNormal); 
var 
    R: TRect; 
    Details: TThemedElementDetails; 
begin 
    if Column.FieldName = FieldName then 
    begin 
    Column.Title.Alignment := taCenter; 
    Column.Alignment := taCenter; 
    Grid.Canvas.FillRect(Rect); 

    if StyleServices.Enabled then 
    begin 
     Details := StyleServices.GetElementDetails(CtrlStateXP[Column.Field.Value = ValueCheck]); 
     StyleServices.DrawElement(Grid.Canvas.Handle, Details, Rect); 

     if not (gdFocused in State) then 
     Grid.Canvas.Brush.Color := StyleServices.GetSystemColor(clHighlight); 
    end 
    else 
    begin 
     R := Rect; 
     InflateRect(R, -2, -2); 
     DrawFrameControl(Grid.Canvas.Handle, R, DFC_BUTTON, CtrlState[Column.Field.Value = ValueCheck]); 
    end; 
    end; 
end;​