delphi5user
Messages postés9Date d'inscriptionjeudi 14 octobre 2004StatutMembreDernière intervention20 juillet 2006
-
3 nov. 2004 à 17:42
cs_grandvizir
Messages postés1106Date d'inscriptionsamedi 8 novembre 2003StatutMembreDerniè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.)
:
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.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.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;