1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//dessin d'une cellule
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var i,j,x,y:Integer;
R:TRect;
begin
With Sender As TStringGrid Do With Canvas Do
Begin
InflateRect(Rect,1,1);
if Odd(ARow) then OffsetRect(Rect,DefaultColWidth div 2,0);
Brush.Style := bsSolid;
//TextRect(Rect, Rect.Left+2, Rect.Top+2, Cells[ACol, ARow]);
If gdFixed in State
{ Les cellules fixes sont toujours dessinées en gris }
Then Brush.Color := clBtnFace
Else Begin
{ Les cellules sélectionnées sont en bleue }
If gdSelected In State
Then Brush.Color := clNavy
{ sinon, c'est une cellule normale}
Else Brush.Color := Color;
End;
Canvas.FillRect(Rect);
Canvas.Brush.Color:=clDkGray;
Canvas.FrameRect(Rect);
InflateRect(Rect,-3,-3);
{ Sélection de la couleur de texte }
If gdSelected In State Then
SetTextColor(Canvas.Handle,clWhite)
Else SetTextColor(Canvas.Handle,clBlack);
{ Dessin du texte en utilisant la fonction API }
DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,DT_NOPREFIX );
End;
end;
//selection d'une cellule
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var xx,ccol,rrow:Integer;
begin
With (Sender As TStringGrid) Do
begin
xx:=ScreenToClient(Mouse.CursorPos).X;
if Odd(ARow) then
begin
if xx<(DefaultColWidth div 2)
then CanSelect:=False
else begin
if ACol>1 then
if (xx<(CellRect(ACol-1,ARow).Right+(DefaultColWidth div 2)))
then begin
CanSelect:=False;
StringGrid1.Col:=ACol-1;
StringGrid1.Row:=ARow;
end;
end;
end;
Invalidate;
end;
end;
end. |
Partager