TStringGrid + TScrollBox : petit pb de décalage

Résolu
delphi5user Messages postés 9 Date d'inscription jeudi 14 octobre 2004 Statut Membre Dernière intervention 20 juillet 2006 - 3 nov. 2004 à 17:42
cs_grandvizir Messages postés 1106 Date d'inscription samedi 8 novembre 2003 Statut Membre Dernière intervention 3 septembre 2006 - 6 nov. 2004 à 20:13
Bonjour à vous.

Je vais essayer d'explique mon problème clairement.

J'ai fait un Form avec un TScrollBox, 2 TStringGrid (1 pour les titres, un pour les données).

Le problème :
Barre des titres : Redimensionner la colonne 5 pour qu'elle dépasse le 'clientWidth' (le ScrollBox horizontal apparait)
Ensuite redimensionner la colonne 4 vers la droite (faite attention à ce que la colonne 5 soit tjs visible)

Maintenant cliquer sur la grille des données : la grille se décalle sans que les titres suivent : PROBLEME.

Par contre ça marche si vous ne redimensionner que la colonne 5 et qu'après vous cliquer sur la grille des données : bizarre !

Je pense que cela vient de ma procedure "grResultatsSelectCell"

Merci pour votre aide.

Je vous ai recopié le code (malheureusement y a pas d'options pour joindre le fichier.)
:

Project1.pas

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, Math;

type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
grTitres: TStringGrid;
grResultats: TStringGrid;
procedure grResultatsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure grTitresMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure grTitresExit(Sender: TObject);
procedure grResultatsExit(Sender: TObject);
procedure grResultatsSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure grTitresDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Déclarations privées }
iPosition : integer ;
bChangementFocus : boolean ;
function monTrim(str: string): string ;
procedure initForm1() ;
procedure ajusteGrilles(grilleFixe, grilleMobile : TStringGrid) ;
procedure ajusteTailleColonnes(grille: TStringGrid; iNumCol, iMin : integer) ;
constructor create(AOwner: TComponent); reintroduce ; override ;
public
{ Déclarations publiques }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

constructor TForm1.create(AOwner: TComponent) ;
Begin
inherited Create(AOwner);
try
bChangementFocus := false ;
initForm1() ;
except
on E:exception do begin
MessageDlg(E.Message, mtError, [mbOK], 0) ;
self.Close ;
end ;
end ;
End;

procedure TForm1.initForm1() ;
var i, j : integer ;
Begin
grTitres.Cells[0,0] := ' Colonne 1 ' ;
grTitres.Cells[1,0] := ' Colonne 2 ' ;
grTitres.Cells[2,0] := ' Colonne 3 ' ;
grTitres.Cells[3,0] := ' Colonne 4 ' ;
grTitres.Cells[4,0] := ' Colonne 5 ' ;
with grResultats do
for i := 0 to ColCount - 1 do
for j := 0 to RowCount - 1 do
Cells[i,j] := ' Valeur colonne ' + inttostr(i+1) ;
End;

function TForm1.monTrim(str: string): string;
// enlève tous les espaces d'une chaîne
var
strTemp : string ;
i : integer ;
Begin
strTemp := trim(str) ;
i := pos(' ', strTemp) ;
while i>0 do begin
strTemp := copy(strTemp, 1,i-1)+copy(strTemp, i+1, length(strTemp)-i) ;
i := pos(' ', strTemp) ;
end ;
result := strTemp ;
End;

procedure TForm1.ajusteGrilles(grilleFixe, grilleMobile : TStringGrid) ;
// ajuste horizontalement la grilleMobile sur la grilleFixe
var
i : integer ;
total : integer ;
Begin
total := 0 ;
for i:=0 to (grilleFixe.colCount-1) do begin
grilleMobile.colWidths[i] := grilleFixe.colWidths[i] ;
total := total + grilleFixe.colWidths[i] ;
end;
ScrollBox1.HorzScrollBar.Range := total + 20 ;
End;

procedure TForm1.grResultatsMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if bChangementFocus then
ScrollBox1.HorzScrollBar.Position := iPosition ;
bChangementFocus := false ;
end;

procedure TForm1.grTitresMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
aCol, aRow : longInt ;
iLargeurMin : integer ;
Begin
if bChangementFocus then
ScrollBox1.HorzScrollBar.Position := iPosition ;
bChangementFocus := false ;
grTitres.MouseToCell(X, Y, aCol, aRow) ;
if (Button=mbRight)and(aRow=0) then begin
iLargeurMin := round(grTitres.canvas.textWidth(monTrim(grTitres.Cells[aCol,aRow]))+20) ;
ajusteTailleColonnes(grResultats, aCol, iLargeurMin) ;
ajusteGrilles(grResultats, grTitres) ;
end ;
end;

procedure TForm1.grTitresExit(Sender: TObject);
begin
bChangementFocus := true ;
iPosition := ScrollBox1.HorzScrollBar.Position ;
end;

procedure TForm1.grResultatsExit(Sender: TObject);
begin
bChangementFocus := true ;
iPosition := ScrollBox1.HorzScrollBar.Position ;
end;

procedure TForm1.grResultatsSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
if not bChangementFocus then begin
while (grResultats.cellRect(aCol, aRow).left < abs(grResultats.left))
and(ScrollBox1.HorzScrollBar.Position>0) do begin
ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position-10 ;
self.repaint() ;
end ;
while (grResultats.cellRect(aCol, aRow).right > abs(grResultats.left)+ScrollBox1.clientWidth)
and(ScrollBox1.HorzScrollBar.Position < ScrollBox1.HorzScrollBar.range-ScrollBox1.clientWidth) do begin
ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position+10 ;
self.repaint() ;
end ;
iPosition := ScrollBox1.HorzScrollBar.Position ;
end ;
end;

procedure TForm1.grTitresDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
ajusteGrilles(grTitres, grResultats) ;
end;

procedure TForm1.ajusteTailleColonnes(grille: TStringGrid; iNumCol, iMin : integer) ;
// ajuste la largeur de la colonne iNumCol, avec iMin comme largeur minimun.
// si iMin=-1, la largeur minimum est la largeur en cours.
// si iNumCol=-1, ajuste les largeurs de toutes les colonnes
var
iLigne, iCol, iLargeur : integer ;
Begin
with grille do begin
if (iNumCol>colCount)or(colCount=0)or(iNumCol<-1)or(iMin<-1) then
exit ;
if iNumCol=-1 then
for iCol:=0 to colCount-1 do begin
if iMin=-1 then
iLargeur := colWidths[iCol]
else
iLargeur := iMin ;
for iLigne := 0 to rowCount-1 do
iLargeur := max(iLargeur, round(canvas.textWidth(cells[iCol, iLigne]))) ;
colWidths[iCol] := iLargeur ;
end
else begin
if iMin=-1 then
iLargeur := colWidths[iNumCol]
else
iLargeur := iMin ;
for iLigne := 0 to rowCount-1 do
iLargeur := max(iLargeur, round(canvas.textWidth(cells[iNumCol, iLigne]))) ;
colWidths[iNumCol] := iLargeur ;
end ;
end ; //with
End;

end.

Project1.dfm

object Form1: TForm1
Left = 286
Top = 121
Width = 518
Height = 403
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ScrollBox1: TScrollBox
Left = 0
Top = 0
Width = 510
Height = 369
Align = alClient
TabOrder = 0
object grTitres: TStringGrid
Left = 0
Top = 0
Width = 506
Height = 49
Align = alTop
BorderStyle = bsNone
Color = clBtnFace
FixedCols = 0
RowCount = 2
GridLineWidth = 0
Options = [goFixedVertLine, goColSizing, goEditing, goThumbTracking]
ScrollBars = ssNone
TabOrder = 0
OnDrawCell = grTitresDrawCell
OnExit = grTitresExit
OnMouseDown = grTitresMouseDown
ColWidths = (
65
64
64
64
64)
end
object grResultats: TStringGrid
Left = 0
Top = 49
Width = 506
Height = 316
Align = alClient
BorderStyle = bsNone
FixedCols = 0
FixedRows = 0
Options = [goFixedHorzLine, goVertLine, goHorzLine, goDrawFocusSelected, goColSizing, goThumbTracking]
ScrollBars = ssVertical
TabOrder = 1
OnExit = grResultatsExit
OnMouseDown = grResultatsMouseDown
OnSelectCell = grResultatsSelectCell
RowHeights = (
24
24
24
24
24)
end
end
end

3 réponses

cs_grandvizir Messages postés 1106 Date d'inscription samedi 8 novembre 2003 Statut Membre Dernière intervention 3 septembre 2006 22
5 nov. 2004 à 19:55
C'est fou comme une image simplifie les choses:

MaGrille.Align:=alClient;

C'est tout....... ;)
3
cs_grandvizir Messages postés 1106 Date d'inscription samedi 8 novembre 2003 Statut Membre Dernière intervention 3 septembre 2006 22
6 nov. 2004 à 20:13
... de mettre un tel composant dans un TScrollBox. Bizarre !

===========
Validez les réponses si ok...

3
delphi5user Messages postés 9 Date d'inscription jeudi 14 octobre 2004 Statut Membre Dernière intervention 20 juillet 2006
5 nov. 2004 à 16:55
Mon probleme en image

ici
0
Rejoignez-nous