调整窗体中StringGrid控件的列的宽度,关闭该窗体,重新进入这个窗体使StringGrid的宽度恢复原状,咋做

解决方案 »

  1.   

    用一個INI文件保存各列的寬度即可
      

  2.   

    剛剛寫了一個
    窗體上放了一個TStringGrid以及兩個TButton,其中Button1為恢復,Button2為將大小保存unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses
      IniFiles;procedure TForm1.Button1Click(Sender: TObject);
    var
      IniFile: TIniFile;
      I: Integer;
      strTemp: string;
      SL: TStringList;
    begin
      if FileExists(ExtractFilePath(Application.ExeName) + 'SGSetup.INI') then
      begin
        IniFile := TIniFile.Create(ExtractFilePath(application.ExeName) + 'SGSetup.INI');
        SL := TStringList.Create;
        try
          strTemp := IniFile.ReadString('SGSETUP', 'RowSize', '');
          if strTemp <> '' then
          begin
            SL.CommaText := strTemp;
            for I := 0 to SL.Count - 1 do
              StringGrid1.RowHeights[I] := StrToIntDef(SL[I], 10);
          end;      strTemp := IniFile.ReadString('SGSETUP', 'ColSize', '');
          if strTemp <> '' then
          begin
            SL.CommaText := strTemp;
            for I := 0 to SL.Count - 1 do
              StringGrid1.ColWidths[I] := StrToIntDef(SL[I], 10);
          end;
        finally
          SL.Free;
          IniFile.Free;
        end;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      IniFile: TIniFile;
      I: Integer;
      SL: TStringList;
    begin
      IniFile := TIniFile.Create(ExtractFilePath(application.ExeName) + 'SGSetup.INI');
      SL := TStringList.Create;
      try
        SL.CommaText := '';
        for I := 0 to StringGrid1.RowCount - 1 do
          SL.Add(IntToStr(StringGrid1.RowHeights[I]));
        IniFile.WriteString('SGSETUP', 'RowSize',SL.CommaText);    SL.CommaText := '';
        for I := 0 to StringGrid1.ColCount - 1 do
          SL.Add(IntToStr(StringGrid1.ColWidths[I]));
        IniFile.WriteString('SGSETUP', 'ColSize',SL.CommaText);  finally
        SL.Free;
        IniFile.Free;
      end;
    end;end.
      

  3.   

    谢谢   duanhai(段海)
    不光列的宽度要还原, 我用的是第三方控件TAdvStringGrid  (http://www.tmssoftware.com/)
    可 按住“Shift”键  点 鼠标左键 后 多列复合排序  排序的列头会出现黄色的三角 ,
     用Close关闭窗体后, 重新进入该窗体还会出现  。为什么会保留 ?