Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#7

AW: Breite aller Spalten (ListView) auf ein Schlag speichern?

  Alt 9. Okt 2014, 15:41
Delphi-Quellcode:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IniFiles;

{$R *.DFM}

const
  SColumnSectionFmt = 'ListView.Column.Index=%d';
  SColumnId = 'ID';
  SColumnWidth = 'Width';

procedure SaveColumnsOrder(LV: TListView; const FileName: string);
var
  Ini: TIniFile;
  I: Integer;
  Section: string;
begin
  Ini := TIniFile.Create(FileName);
  try
    for I := 0 to LV.Columns.Count - 1 do
      with Ini, LV.Columns[I] do
      begin
        Section := Format(SColumnSectionFmt, [I]);
        WriteInteger(Section, SColumnId, ID);
        WriteInteger(Section, SColumnWidth, Width);
      end;
  finally
    Ini.Free;
  end;
end;

procedure LoadColumnsOrder(LV: TListView; const FileName: string);
var
  Ini: TIniFile;
  I: Integer;
  Section: string;
  Column: TListColumn;
begin
  Ini := TIniFile.Create(FileName);
  try
    LV.Items.BeginUpdate;
    try
      for I := 0 to LV.Columns.Count - 1 do
      begin
        Section := Format(SColumnSectionFmt, [I]);
        Column := TListColumn(LV.Columns.FindItemID(
          Ini.ReadInteger(Section, SColumnId, I)));
        if Assigned(Column) then with Column do
        begin
          Index := I;
          Width := Ini.ReadInteger(Section, SColumnWidth, Width);
        end;
      end;
    finally
      LV.Items.EndUpdate;
    end;
  finally
    Ini.Free;
  end;
end;

const SIniFileName = '\LVColumnOrder.cfg';

procedure TForm1.FormCreate(Sender: TObject);
begin
  LoadColumnsOrder(ListView1, ExtractFilePath(Application.ExeName) + SIniFileName);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SaveColumnsOrder(ListView1, ExtractFilePath(Application.ExeName) + SIniFileName);
end;

end.
  Mit Zitat antworten Zitat