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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
unit UTstGrid;
{$Define Chrono} // mise en oeuvre du calcul de temps CPU
{$Define ChronoVerbose} // affichage détaillé des opérations (génère la définition de Chrono)
{$Define StringGridTest} // remplissage automatique des cellules
{$IfDef ChronoVerbose}
// par sécurité, si Chrono n'est pas défini (mise en commentaire de la ligne de définition par exemple), on le définit ici
{$IfnDef Chrono}
{$Define Chrono}
{$EndIf}
{$EndIf}
{
La form contient :
un stringgrid nommé : StringGrid1,
les options de StringGrid1 :
GoRangeSelect = True
GoRowSelect = True
GoThumbTracking = True
les évènements attachés à StringGrid1 :
OnDrawCell traité avec la procédure : StringGrid1DrawCell
OnKeyDown traité avec la procédure : StringGrid1KeyDown
OnKeyUp traité avec la procédure : StringGrid1KeyUp
OnMouseDown traité avec la procédure : StringGrid1MouseDown
OnMouseUp traité avec la procédure : StringGrid1MouseUp
un menu principal nommé : MainMenu1, le sous-menu de MainMenu1 :
MnuEdit qui est décomposé en sous-menus :
MnuSelectAll qui traite l'évènement OnClick avec la procédure : MnuSelectAllClick
MnuUnSelect qui traite l'évènement OnClick avec la procédure : MnuUnselectAllClick
MnuInvertSelect qui traite l'évènement OnClick avec la procédure : MnuInvertSelectClick
// plus d'infos sur Grids : http://wiki.lazarus.freepascal.org/Grids_Reference_Page
}
{$mode objfpc}{$H+}
interface
uses
Math, lclintf, StrUtils,
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
StdCtrls, Menus;
type
{$IfDef Chrono}
EPiDException = class(Exception);
{$EndIf}
{ TForm1 }
TForm1 = class(TForm)
BtnTester: TButton;
BtnClearButton3: TButton;
MainMenu1: TMainMenu;
Memo1: TMemo;
MnuUnSelect: TMenuItem;
MnuInvertSelect: TMenuItem;
MnuselectAll: TMenuItem;
MnuEdit: TMenuItem;
StringGrid1: TStringGrid;
procedure BtnClearButton3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BtnTesterClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MnuSelectAllClick(Sender: TObject);
procedure MnuInvertSelectClick(Sender: TObject);
procedure MnuUnSelectClick(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StringGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ private declarations }
procedure RowSelect;
procedure DragnDrop;
procedure DragnDropSimple;
procedure DragnDropMultiple;
public
{ public declarations }
// type
end;
{$IfDef Chrono}
type
TaCPU = array [1..4] of longint;
{$EndIf}
var
Form1: TForm1;
implementation
var
{$IfDef Chrono}
sPId : string;
aCPU : TaCPU;
CPUFile : TextFile;
ZCPUFile : string;
CPU : float;
{$EndIf}
FirstRow : longint; // nombre de lignes de titre dans le stringgrid
mIsRowSelected : integer; // numéro de la colonne ajoutée dans le stringgrid pour y inscrire un '*' si la ligne est sélectionnée
MultiSelect, // témoin de sélection de plusieurs zone dans le stringgrid
KeyShiftFlag, // témoin de la touche Shift/Maj, reste à true tant que la touche est pressée
KeyControlFlag : boolean; // témoin de la touche Control/Ctrl, reste à true tant que la touche est pressée
CurrentColumn, CurrentRow, // pointe sur la cellule qui vient d'être 'cliquée'
SelectedRowFirst, // première ligne sélectionnée dans le stringgrid (*)
SelectedRowLast, // dernière ligne sélectionnée dans le stringgrid (*)
// (*) ATTENTION il peut y avoir plus d'un bloc dans la plage SelectedRowFirst <---> SelectedRowLast
SelectedRowPrevious, // ligne sélectionnée au traitement précédent (sert pour la sélection de lignes)
SelectedRowsCount : longint; // nombre de lignes sélectionnées dans le stringgrid
ZRowCount,
ZColCount : longint;
ZTarget : longint;
{$R *.lfm}
{ TForm1 }
{$IfDef Chrono}
procedure GetPId;
begin
sPId := '/proc/' + InttoStr(GetProcessID) + '/stat';
if not (FileExists(sPId)) then begin
raise EPIdException.Create('le fichier ' + sPId + ' n''existe pas');
Halt(1);
end;
end;
function GetCPU(const aOldCPU : TaCPU; Delta : boolean) : TaCPU;
var
aDeltaCPU : TaCPU;
i : integer;
begin
try
AssignFile(CPUFile, sPId);
Reset(CPUFile);
ReadLn(CPUFile, ZCPUFile);
if (Delta) then
for i := 1 to 4 do
aDeltaCPU := aOldCPU
else
for i := 1 to 4 do
aDeltaCPU[i] := 0;
for i := 1 to 4 do
Result[i] := StrtoInt(ExtractWord(13 + i,ZCPUFile, [' '])) - aDeltaCPU[i];
finally
CloseFile(CPUFile);
end;
end;
{$EndIf}
procedure TForm1.BtnTesterClick(Sender: TObject);
var
fTstGridIni : TextFile;
ZCmdFile,
wCmdFile : string;
iPos : integer;
aSelect : array of record
BeginSel,
EndSel : longint;
end;
procedure CmdError(Msg : string);
begin
ShowMessage(Msg);
Halt(1);
end;
procedure GetCmd;
var
Cmd : string;
begin
iPos := pos(':', wCmdFile);
if (iPos = 0) then
CmdError('Il manque le '':''' + chr(13) + 'dans ''' + ZCmdFile +'''' + chr(13) + 'abandon du traitement');
Cmd := Copy(wCmdFile, 1, iPos - 1);
Delete(wCmdFile, 1 , iPos);
wCmdFile := trim(wCmdFile);
end;
procedure StringGridProceed;
var
i, j : integer;
begin
{$IfDef Chrono}
aCPU := GetCpu(aCPU, false);
{$IfDef ChronoVerbose}
memo1.lines.add('----------------------------------------------------------------');
{$EndIf} // ChronoVerbose
{$EndIf} // Chrono
// creation du stringgrid selon les consignes du fichier ini
StringGrid1.free;
StringGrid1 := TStringGrid.Create(Form1);
with StringGrid1 do begin
Parent := Form1;
Left := 728;
Height := 808;
Top := 48;
Width := 264;
ColCount := 14;
FixedCols := 0;
Options := [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSelect, goThumbTracking, goSmoothScroll];
RowCount := 3;
TabOrder := 0;
OnDrawCell := @StringGrid1DrawCell;
OnKeyDown := @StringGrid1KeyDown;
OnKeyUp := @StringGrid1KeyUp;
OnMouseDown := @StringGrid1MouseDown;
OnMouseUp := @StringGrid1MouseUp;
for i := 1 to ZColCount do
Columns.Add;
StringGrid1.RowCount := ZRowCount + 1;
visible := false;
end;
FirstRow := StringGrid1.FixedRows; // nombre de ligne(s) de titre
// ajout de la colonne d'indicateur de sélection
StringGrid1.Columns.Add;
mIsRowSelected := StringGrid1.ColCount - 1;
StringGrid1.Columns[mIsRowSelected].Width := 26;
{$IfDef StringGridTest} // pour tester le mécanisme on présente différement le stringgrid
// remplissage des titres
if (FirstRow > 0) then
for i := 0 to StringGrid1.colcount - 2 do begin
StringGrid1.Cells[i , 0] := 'titre ' + inttostr(i);
end;
// remplissage des cellules pour mieux voir le traitement ensuite
for i := FirstRow to StringGrid1.rowcount - 1 do // remplissage des cellules pour mieux voir le traitement ensuite
for j := 0 to StringGrid1.colcount - 2 do begin
StringGrid1.Cells[j , i] := ' * ' + inttostr(i) + ' * ' + inttostr(j);
end;
StringGrid1.Cells[mIsRowSelected , 0] := '- * -';
{$Else}
StringGrid1.Columns[mIsRowSelected].Visible := false; // on cache la colonne ajoutée
{$EndIf}
// initalisation des témoins
MultiSelect := false;
KeyControlFlag := false;
KeyShiftFlag := false;
SelectedRowFirst := FirstRow;
SelectedRowLast := StringGrid1.rowcount - 1;
{$IfDef Chrono}
aCPU := GetCpu(aCPU, true);
{$IfDef ChronoVerbose}
CPU := (aCPU[1] + aCPU[2] + aCPU[3] + aCPU[4]) / 100;
memo1.lines.add(format('Création d''un tableau de %D lignes par %D colonnes (utiles), soit %D cellules en %.2F seconde(s) soit une moyenne de %.0F Cellules par seconde',
[ZRowCount,
ZColCount,
ZRowCount * ZColCount,
CPU,
ZRowCount * ZColCount / CPU]));
memo1.lines.add('----------------------------------------------------------------');
{$EndIf} // ChronoVerbose
aCPU := GetCpu(aCPU, false);
{$EndIf} // Chrono
// sélection des lignes
for i := 0 to Length(aSelect) - 1 do begin
{$IfDef ChronoVerbose}
memo1.lines.add('selection de(s) ligne(s) ' + inttostr(aSelect[i].BeginSel) + ' à ' + inttostr(aSelect[i].EndSel));
{$EndIf} // ChronoVerbose
for j := aSelect[i].BeginSel to aSelect[i].EndSel do
StringGrid1.Cells[mIsRowSelected , j] := '*';
end;
{$IfDef Chrono}
aCPU := GetCpu(aCPU, true);
{$IfDef ChronoVerbose}
memo1.lines.add('----------------------------------------------------------------');
memo1.lines.add('Sélection des lignes en ' +
format('%F seconde(s)', [(aCPU[1] + aCPU[2] + aCPU[3] + aCPU[4]) / 100]));
memo1.lines.add('----------------------------------------------------------------');
{$EndIf} // ChronoVerbose
{$EndIf} // Chrono
// preparation et appel au drag and drop
if (length(aSelect) < 2) then
MultiSelect := false
else
MultiSelect := true;
CurrentRow := ZTarget;
SelectedRowsCount := 0;
SelectedRowFirst := StringGrid1.rowcount;
SelectedRowLast := 1;
for i := 0 to length(aSelect) - 1 do begin
SelectedRowFirst := min(SelectedRowFirst, aSelect[i].BeginSel);
SelectedRowLast := max(SelectedRowLast, aSelect[i].EndSel);
SelectedRowsCount := SelectedRowsCount + aSelect[i].EndSel - aSelect[i].BeginSel + 1;
end;
{$IfDef ChronoVerbose}
memo1.lines.add('----------------------------------------------------------------');
memo1.lines.add(format('Déplacement en %D de %D ligne(s) sélectionnée(s); 1ère ligne : %D, dernière ligne : %D',
[CurrentRow,
SelectedRowsCount,
SelectedRowFirst,
SelectedRowLast]));
memo1.lines.add('----------------------------------------------------------------');
{$EndIf} // ChronoVerbose
DragnDrop;
end;
begin
// lecture du fichier ini
// memo1.Clear;
ZRowCount := 0;
if not (FileExists('TstGrid.ini')) then
CmdError('le fichier TstGrid.ini n''existe pas');
AssignFile(fTstGridIni, 'TstGrid.ini');
Reset(fTstGridIni);
{$IfDef ChronoVerbose}
memo1.lines.add('----------------------------------------------------------------');
{$EndIf} // ChronoVerbose
While not (Eof(fTstGridIni)) do begin
ReadLn(fTstGridIni, ZCmdFile);
ZCmdFile := trim(ZCmdFile);
if ((pos('#', ZCmdFile) = 1) or (ZCmdFile = '')) then begin // ligne de commentaires
{$IfDef ChronoVerbose}
memo1.lines.add(ZcmdFile);
{$EndIf} // ChronoVerbose
continue;
end;
wCmdFile := ZcmdFile + '#';
{$IfDef ChronoVerbose}
memo1.lines.add(ZcmdFile);
{$EndIf} // ChronoVerbose
wCmdFile := UpperCase(Trim(wCmdFile));
if (copy(wCmdFile, 1, 2) = 'EX') then begin // commande execute
if (ZRowcount = 0) then
CmdError('Les dimensions du tableau ne sont pas correctement définies, abandon');
if (Length(aSelect) = 0) then
CmdError('Aucune ligne sélectionnée, abandon');
StringGridProceed;
SetLength(aSelect, 0);
end
else begin
if (copy(wCmdFile, 1, 2) = 'SE') then begin // commande select
GetCmd;
SetLength(aSelect, Length(aSelect) + 1);
iPos := pos(',',wCmdFile); // extraction de la 1ère valeur (ici ligne début de zone sélectionnée)
if not (TryStrtoint(Trim(Copy(wCmdFile, 1, iPos - 1)), aSelect[Length(aSelect) - 1].BeginSel)) then
CmdError('Dans ' + ZCmdFile +', la valeur ''' + Copy(wCmdFile, 1, iPos - 1) + ''' n''est pas une valeur numérique, abandon');
Delete(wCmdFile, 1 , iPos);
iPos := pos('#',wCmdFile); // extraction de la 2ème valeur (ici ligne fin de zone sélectionnée)
if (iPos = 0) then
iPos := length(wCmdFile);
if not (TryStrtoint(Trim(Copy(wCmdFile, 1, iPos - 1)), aSelect[Length(aSelect) - 1].EndSel)) then
CmdError('Dans ' + ZCmdFile +', la valeur ''' + Copy(wCmdFile, 1, iPos - 1) + ''' n''est pas une valeur numérique, abandon');
if (aSelect[Length(aSelect) - 1].EndSel > ZRowCount) then
CmdError('Dans ''' + ZCmdFile + '''' + chr(13) + 'la dernière ligne à sélectionner' + chr(13) + '''' +
Copy(wCmdFile, 1, iPos - 1) + ''' dépasse la taille du stringgrid (' +
inttostr(ZRowCount) + '), abandon');
end
else
if (copy(wCmdFile, 1, 2) = 'DR') then begin // commande drang and drop
GetCmd;
iPos := pos('#',wCmdFile); // extraction de la 1ère valeur (ici la ligne où déplacer les zones sélectionnées)
if not (TryStrtoint(Trim(Copy(wCmdFile, 1, iPos - 1)), ZTarget)) then
CmdError('Dans ' + ZCmdFile +', la valeur ''' + Copy(wCmdFile, 1, iPos - 1) + ''' n''est pas une valeur numérique, abandon');
end
else
if (copy(wCmdFile, 1, 2) = 'ST') then begin // commande stringgrid
GetCmd;
iPos := pos(',',wCmdFile); // extraction de la 1ère valeur (ici RowCount)
if not (TryStrtoint(Trim(Copy(wCmdFile, 1, iPos - 1)), ZRowCount)) then
CmdError('Dans ' + ZCmdFile +', la valeur ''' + Copy(wCmdFile, 1, iPos - 1) + ''' n''est pas une valeur numérique, abandon');
Delete(wCmdFile, 1 , iPos);
iPos := pos('#',wCmdFile); // extraction de la 2ème valeur (ici ColCount)
if (iPos = 0) then
iPos := length(wCmdFile);
if not (TryStrtoint(Trim(Copy(wCmdFile, 1, iPos - 1)), ZColCount)) then
CmdError('Dans ' + ZCmdFile +', la valeur ''' + Copy(wCmdFile, 1, iPos - 1) + ''' n''est pas une valeur numérique, abandon');
end
else
if (copy(wCmdFile, 1, 2) = 'EN') then begin // commande end
break;
end;
end;
end;
CloseFile(fTstGridIni);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{$IfDef Chrono}
GetPId;
aCPU := GetCpu(aCPU, false);
{$IfnDef ChronoVerbose}
memo1.lines.add('lignes;colonnes;lignes déplacées;cellules déplacées;Cellules/seconde;cpu');
{$EndIf} // ChronoVerbose
{$EndIf} // Chrono
end;
procedure TForm1.BtnClearButton3Click(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
procedure TForm1.MnuSelectAllClick(Sender: TObject);
var
i : integer;
begin
SelectedRowFirst := FirstRow;
SelectedRowLast := StringGrid1.rowcount - 1;
for i := SelectedRowFirst to SelectedRowLast do
StringGrid1.Cells[mIsRowSelected , i] := '*';
SelectedRowsCount := StringGrid1.rowcount -1;
StringGrid1.Invalidate; // Rafraichissement de l'affichage du stringgrid
end;
procedure TForm1.MnuInvertSelectClick(Sender: TObject);
var
i : integer;
begin
SelectedRowFirst := FirstRow;
SelectedRowLast := StringGrid1.rowcount - 1;
for i := SelectedRowFirst to SelectedRowLast do
if (StringGrid1.Cells[mIsRowSelected , i] = '*') then begin
StringGrid1.Cells[mIsRowSelected , i] := ' ';
dec(SelectedRowsCount);
end
else begin
StringGrid1.Cells[mIsRowSelected , i] := '*';
inc(SelectedRowsCount);
end;
StringGrid1.Invalidate; // Rafraichissement de l'affichage du stringgrid
end;
procedure TForm1.MnuUnSelectClick(Sender: TObject);
var
i : integer;
begin
SelectedRowFirst := FirstRow;
SelectedRowLast := StringGrid1.rowcount - 1;
for i := SelectedRowFirst to SelectedRowLast do // on désélectionne tout
StringGrid1.Cells[mIsRowSelected , i] := ' ';
SelectedRowsCount := 0;
StringGrid1.Invalidate; // Rafraichissement de l'affichage du stringgrid
end;
procedure TForm1.DragnDropSimple;
var
i : integer;
begin
if (SelectedRowLast < CurrentRow) then begin
for i:= SelectedRowFirst to SelectedRowLast do begin // la zone sélectionnée est en aval de la ligne destinataire
StringGrid1.Cells[mIsRowSelected , SelectedRowFirst] := ' ';
StringGrid1.MoveColRow(false, SelectedRowFirst, CurrentRow - 1);
end;
end
else begin
for i:= SelectedRowFirst to SelectedRowLast do begin // la zone sélectionnée est en amont de la ligne destinataire
StringGrid1.Cells[mIsRowSelected , SelectedRowLast] := ' ';
StringGrid1.MoveColRow(false, SelectedRowLast, CurrentRow);
end;
end;
end;
procedure TForm1.DragnDropMultiple;
// kaf var
// kaf i : integer;
begin
if (SelectedRowFirst > CurrentRow) then begin // l'ensemble des zones sélectionnées se situe en aval de la ligne destinataire
while not (SelectedRowLast < SelectedRowFirst) do begin // boucle sur l'ensemble des zones sélectionnées
if (StringGrid1.Cells[mIsRowSelected , SelectedRowLast] = '*') then begin
StringGrid1.Cells[mIsRowSelected , SelectedRowLast] := ' ';
StringGrid1.MoveColRow(false, SelectedRowLast, CurrentRow);
end
else
dec(SelectedRowLast);
end;
end
else begin
if (SelectedRowLast < CurrentRow) then begin // l'ensemble des zones sélectionnées se situe en amont de la ligne destinataire
while not (SelectedRowFirst = SelectedRowLast) do begin // boucle sur l'ensemble des zones sélectionnées
if (StringGrid1.Cells[mIsRowSelected , SelectedRowFirst] = '*') then begin
StringGrid1.Cells[mIsRowSelected , SelectedRowFirst] := ' ';
StringGrid1.MoveColRow(false, SelectedRowFirst, CurrentRow -1);
end
else
inc(SelectedRowFirst);
end;
end
else begin // l'ensemble des zones sélectionnées se situe de part et d'autre de la zone destinataire
// on traite d'abord les lignes situées en aval de la ligne destinataire
while not (SelectedRowLast < CurrentRow) do begin // boucle sur l'ensemble des zones sélectionnées
if (StringGrid1.Cells[mIsRowSelected , SelectedRowLast] = '*') then begin
StringGrid1.Cells[mIsRowSelected , SelectedRowLast] := ' ';
StringGrid1.MoveColRow(false, SelectedRowLast, CurrentRow);
end
else
dec(SelectedRowLast);
end;
// puis on traite les lignes situées en amont de la ligne destinataire
while (SelectedRowFirst < CurrentRow) do begin // boucle sur les zones sélectionnées en amont de la ligne destinataire
if (StringGrid1.Cells[mIsRowSelected , SelectedRowFirst] = '*') then begin
StringGrid1.Cells[mIsRowSelected , SelectedRowFirst] := ' ';
StringGrid1.MoveColRow(false, SelectedRowFirst, CurrentRow - 1);
end
else
inc(SelectedRowFirst);
end;
end;
end
end;
procedure TForm1.DragnDrop;
// kaf var
// kaf i : integer;
begin
StringGrid1.BeginUpdate;
{$IfDef Chrono}
aCPU := GetCpu(aCPU, false);
{$EndIf}
if MultiSelect then
DragnDropMultiple // multiples zones sélectionnées il faut balayer de la 1ère ligne sélectionnée à la dernière
else
DragnDropSimple; // une seule zone sélectionnée, on ne traite que du début à la fin de celle-ci
{$IfDef Chrono}
aCPU := GetCpu(aCPU, true);
CPU := (aCPU[1] + aCPU[2] + aCPU[3] + aCPU[4]) / 100;
{$IfDef ChronoVerbose}
memo1.lines.add('Drag and drop de ' + format('%D cellules en ', [SelectedRowsCount * ZColCount]) +
format('%.2F seconde(s) soit une moyenne de ', [CPU]) +
format('%.0F Cellules par seconde', [SelectedRowsCount * ZColCount / CPU]));
memo1.lines.add('----------------------------------------------------------------');
{$Else}
// lignes;colonnes;lignes déplacées;cellules déplacées;Cellules/seconde;cpu
memo1.lines.add(
format('%D;', [ZRowCount]) + // Lignes;
format('%D;', [ZColCount]) + // Colonnes;
format('%D;', [SelectedRowsCount]) + // Lignes déplacées;
format('%D;', [SelectedRowsCount * ZColCount]) + // cellules déplacées;
format('%.0F;', [SelectedRowsCount * ZColCount / CPU]) + // Cellules/seconde;
format('%.2F;', [CPU])); // CPU
{$EndIf} // ChronoVerbose
{$EndIf} // Chrono
SelectedRowsCount := 0;
MultiSelect := false;
StringGrid1.visible := true;
StringGrid1.EndUpdate;
end;
procedure TForm1.RowSelect;
var
i : integer;
begin
for i := FirstRow to StringGrid1.RowCount-1 do // désélectionne tout
StringGrid1.Cells[mIsRowSelected , i] := ' ';
StringGrid1.Cells[mIsRowSelected , CurrentRow] := '*'; // puis on sélectionne la ligne courante
SelectedRowFirst := CurrentRow;
SelectedRowLast := CurrentRow;
SelectedRowsCount := 1;
MultiSelect := false;
end;
procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Stringgrid1.MouseToCell(X, Y, CurrentColumn, CurrentRow); // Memorise la ligne sélectionnée, la colonne n'est pas exploitée ici
if (CurrentRow < FirstRow) then // pas de traitement sur les lignes de titre
exit;
if (KeyShiftFlag) then // Click gauche + touche Maj/Shift déjà traité dans OnMouseDown
exit;
if (KeyControlFlag) then // Click gauche + touche Control/Ctrl déjà traité dans OnMouseDown
exit;
if (SelectedRowPrevious = CurrentRow) then // il n'y a pas eu de déplacement de la souris pendant appui sur le bouton, il s'agit donc d'une nouvelle sélection
RowSelect
else
DragnDrop;
StringGrid1.Invalidate; // Rafraichissement de l'affichage du stringgrid
end;
procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i : integer;
begin
//--------------------------------------------------------------------------------------------------------------------------------------------------------*
// toutes les actions de sélection et désélection de ligne(s) se font ici sauf pour le Drag and drop qui comprend une partie de traitement dans OnMouseUp *
//--------------------------------------------------------------------------------------------------------------------------------------------------------*
Stringgrid1.MouseToCell(X, Y, CurrentColumn, CurrentRow); // Memorise la ligne sélectionnée, la colonne n'est pas exploitée ici
// ------------- début Maj/Shift + click --------------------------------------------------
if (KeyShiftFlag) then begin // Click gauche + touche Maj/Shift pour indiquer la fin d'un bloc à sélectionner
if (SelectedRowPrevious = -1) then // pas de début de bloc défini
// do nothing
else begin
for i:= min(CurrentRow, SelectedRowPrevious) to max(CurrentRow, SelectedRowPrevious) do begin // sélection des lignes du bloc
if not (StringGrid1.Cells[mIsRowSelected , i] = '*') then begin
StringGrid1.Cells[mIsRowSelected , i] := '*';
inc(SelectedRowsCount);
end;
SelectedRowFirst := min(i, SelectedRowFirst);
SelectedRowLast := max(i, SelectedRowLast);
end;
end;
MultiSelect := false;
end
// ------------- fin Maj/Shift + click ----------------------------------------------------
else begin
// ------------- début Control + click ----------------------------------------------------
if (KeyControlFlag) then begin // Click gauche + touche Control/Ctrl pour inverser la sélection de la ligne courante
if (StringGrid1.Cells[mIsRowSelected , CurrentRow] = '*') then begin // inversion de la sélection de la ligne courante
StringGrid1.Cells[mIsRowSelected , CurrentRow] := ' ';
dec(SelectedRowsCount);
end
else begin
StringGrid1.Cells[mIsRowSelected , CurrentRow] := '*';
inc(SelectedRowsCount);
end;
if (StringGrid1.Cells[mIsRowSelected , CurrentRow] = '*') then begin // ligne sélectionnée
SelectedRowFirst := min(CurrentRow, SelectedRowFirst);
SelectedRowLast := max(CurrentRow, SelectedRowLast);
end
else begin // ligne déselectionnée // mise à jour de SelectedRowFirst
if (SelectedRowsCount > 0) then begin
if (CurrentRow = SelectedRowFirst) then begin
for i:= CurrentRow to SelectedRowLast do begin
if (StringGrid1.Cells[mIsRowSelected , i] = '*') then begin
SelectedRowFirst := i;
break;
end;
end;
end;
if (CurrentRow = SelectedRowLast) then begin // mise à jour de SelectedRowLast
for i:= CurrentRow downto SelectedRowFirst do begin
if (StringGrid1.Cells[mIsRowSelected , i] = '*') then begin
SelectedRowLast := i;
break;
end;
end;
end;
end;
end;
if (SelectedRowsCount > 1) then // plusieurs blocs de ligne sélectionnés
MultiSelect := true
else
MultiSelect := false;
end
// ------------- fin Control + click ------------------------------------------------------
else begin
// ------------- début click --------------------------------------------------------------
SelectedRowPrevious := CurrentRow;
end;
end;
// ------------- fin click ----------------------------------------------------------------
StringGrid1.Invalidate; // Rafraichissement de l'affichage du stringgrid
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
begin
with Sender as TStringGrid do
with Canvas do begin
if (Cells[mIsRowSelected, aRow] = '*') then begin // on colorie en rouge les lignes sélectionnées
Brush.Color := clRed;
Font.Color := clWhite;
end
else begin
Brush.Color := clWindow;
Font.Color := ClBlack;
end;
{ Dessin du fond }
FillRect(aRect);
{ Dessin du texte }
TextOut(aRect.Left,aRect.Top,Cells[aCol,aRow]);
end;
end;
procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
KeyShiftFlag := (key = 16); // on positionne KeyShiftFlag à l'appui sur la touche Maj
KeyControlFlag := (key = 17); // on positionne KeyControlFlag à l'appui sur la touche Control
end;
procedure TForm1.StringGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 16 then KeyShiftFlag := false; // on ne positionne KeyShiftFlag que tant que la touche Maj est appuyée
if key = 17 then KeyControlFlag := false; // on ne positionne KeyControlFlag que tant que la touche Control est appuyée
end;
end. |