[Delphi] Save the form position and properties at registry and load it

    {$region 'Registry'}
    procedure TfrmMain.LoadState;
    var
      R: TRegistry;
      iMonitorSearch, iMonitorCount: Integer;
      iScreenWidth, iScreenHeight: Integer;
      i: Integer;
    begin
      R := TRegistry.Create(KEY_READ);
      try
        try
          R.RootKey := HKEY_CURRENT_USER;
          if R.KeyExists(REG_KEY+TPath.GetFileNameWithoutExtension(Application.ExeName)+'\') then
          begin
            if R.OpenKey(REG_KEY+TPath.GetFileNameWithoutExtension(Application.ExeName)+'\', False) then
            begin
              try
                if R.ValueExists('MainFormState') then
                  MainFormState := R.ReadInteger('MainFormState')
                else
                  MainFormState := 0;
    
                if R.ValueExists('Left') then
                  Left := R.ReadInteger('Left')
                else
                  Left := (Screen.monitors[0].Width div 2) - (ClientWidth div 2);
    
                if R.ValueExists('Top') then
                begin
                  Top := R.ReadInteger('Top');
                  if Top < 0 then
                    Top := 0;
                end
                else
                  Top := (Screen.monitors[0].Height div 2) - (ClientHeight div 2);
    
                if R.ValueExists('ClientHeight') then
                  ClientHeight := R.ReadInteger('ClientHeight')
                else
                  ClientHeight := 620;
    
                if R.ValueExists('ClientWidth') then
                  ClientWidth := R.ReadInteger('ClientWidth')
                else
                  ClientWidth := 1024;
    
                // event 오류 방지를 위해 전역변수 먼저 로드
                if R.ValueExists('edtLprCount') then
                  _preLprCount := R.ReadInteger('edtLprCount')
                else
                  _preLprCount := 0;
    
                if R.ValueExists('edtFramesInARow') then
                  _FramesInARow := R.ReadInteger('edtFramesInARow')
                else
                  _FramesInARow := 4;
    
                if R.ValueExists('edtGridFontSize') then
                  _GridFontSize := R.ReadInteger('edtGridFontSize')
                else
                  _GridFontSize := 12;
    
                for i := 0 to ComponentCount-1 do
                begin
                  if Components[i].ClassType = TscGPSpinEdit then
                  begin
                    if R.ValueExists(TscGPSpinEdit(Components[i]).Name) then
                      TscGPSpinEdit(Components[i]).Value := R.ReadInteger(TscGPSpinEdit(Components[i]).Name);
                  end
                  else if Components[i].ClassType = TPanel then
                  begin
                    if (Pos('pnlIOList', TPanel(Components[i]).Name) > 0) and (R.ValueExists(TPanel(Components[i]).Name))  then
                    begin
                      TPanel(Components[i]).Align := alBottom;
                      TPanel(Components[i]).Height := R.ReadInteger(TPanel(Components[i]).Name);
                      pnlFormOption.Align := alBottom;
                      pnlFormOption.Top := TPanel(Components[i]).Top + TPanel(Components[i]).Height + 1;
                      Splitter1.Align := alBottom;
                      Splitter1.Top := TPanel(Components[i]).Top - 1;
                    end;
                  end;
                end;
              finally
                R.CloseKey;
              end;
            end
            else
            begin
              //Failed to open registry key
            end;
          end
          else
          begin
            //Registry key does not exist
            Left := (Screen.Width div 2) - (ClientWidth div 2);
            Top := (Screen.Height div 2) - (ClientHeight div 2);
            _preLprCount := 0;
            _FramesInARow := 4;
            _GridFontSize := 12;
          end;
        except
    
        end;
      finally
        R.Free;
      end;
    end;
    
    procedure TfrmMain.SaveState;
    var
      R: TRegistry;
      i: Integer;
    begin
      R := TRegistry.Create(KEY_READ or KEY_WRITE);
      try
        try
          R.RootKey := HKEY_CURRENT_USER;
          if R.OpenKey(REG_KEY+TPath.GetFileNameWithoutExtension(Application.ExeName)+'\', True) then
          begin
            try
              if MainFormState = 0 then
              begin
                R.WriteInteger('Left', Left);
                if Top < 0 then
                  R.WriteInteger('Top', 0)
                else
                  R.WriteInteger('Top', Top);
                R.WriteInteger('ClientHeight', ClientHeight);
                R.WriteInteger('ClientWidth', ClientWidth);
              end;
    
              // 화면최대화 여부확인(0 : 기본, 1: 최소화(바모드), 2 : 최대화)
              R.WriteInteger('MainFormState', MainFormState);
    
              for i := 0 to ComponentCount-1 do
              begin
                if Components[i].ClassType = TscGPSpinEdit then
                begin
                  R.WriteInteger(TscGPSpinEdit(Components[i]).Name, TscGPSpinEdit(Components[i]).ValueAsInt);
                end
                else if Components[i].ClassType = TPanel then
                begin
                  if TPanel(Components[i]).Name = 'pnlIOList' then
                  begin
                    R.WriteInteger(TPanel(Components[i]).Name, TPanel(Components[i]).Height);
                  end;
                end;
              end;
            finally
              R.CloseKey;
            end;
          end
          else
          begin
            //Failed to open registry key
          end;
        except
    
        end;
      finally
        R.Free;
      end;
    end;
    {$endregion}

    댓글