티스토리 뷰

반응형

프로그래밍 하다 보면 폼의 위치를 저장하고, 다시 저장된 위치로 폼을 로딩할 때가 있다.

 

[ 방법 설명 ]

  • 프로그램을 종료할 때 Ini 파일에 폼의 위치를 저장하고
  • 로딩할 때 Ini 파일에서 폼의 위치를 읽어서 로딩하면 된다.

 

[ 구축 함수]

  • 다음과 같은 3개의 함수를 만들어 놓으면 사용하기 편할 것 같다.
// Ini 파일은 Main Application에서 공용으로 사용하는 파일 사용.

// 폼을 Ini 파일에 저장된 위치정보로 로딩
procedure LoadFormPosition(aForm: TForm; aDefaultPos: TDefaultFormPosition = dfpMainFormCenter);

// 폼의 위치정보를 Ini파일에 기록
procedure SaveFormPosition(aForm: TForm);

// 폼위치정보 저장 초기화
procedure InitFormPosition(aForm: TForm);

 

[ 폼을 Ini 파일에 저장된 위치정보로 로딩 ]

//
// 저장된 폼 위치 셋팅
//
procedure LoadFormPosition(aForm: TForm; aDefaultPos: TDefaultFormPosition = dfpMainFormCenter);
var
  ConfigIni: TIniFile;
  nTop: Integer;
  nLeft: Integer;
  nWidth: Integer;
  nHeight: Integer;
  nWorkWidth: Integer;
  nWorkHeight: Integer;

begin

  try
    try

      // USER_INI_FILE은 사용자 정의 상수 파일명  예> config.ini
      ConfigIni := TIniFile.Create(USER_INI_FILE);

      nWorkWidth := Screen.WorkAreaWidth;
      nWorkHeight := Screen.WorkAreaHeight;

      // INI 에 ScreenMax가 1로 정의 되어 있으면,  wsMaximized 로 설정
      if ConfigIni.ReadInteger(aForm.Name, 'ScreenMax', 0) = 1 then
      begin
        aForm.WindowState := wsMaximized;
      end
      else
      // INI 에 해당폼의 Size가 지정되어 있지 않으면 디폴트 위치로 뜨게 한다.
      if not ConfigIni.ValueExists(aForm.Name, 'top') then
      begin
        if aDefaultPos = dfpMainFormCenter then
        begin
          application.MainForm.Height   := MAINFORM_HEIGHT;
          application.MainForm.Width    := MAINFORM_WIDTH;
          aForm.Left := (nWorkWidth div 2)  - (aForm.Width div 2);
          aForm.Top  := (nWorkHeight div 2) - (aForm.Height div 2);
        end
        else if aDefaultPos = dfpLeftTop then
        begin
          aForm.Left := 0 + 2;
          aForm.Top := 0 + 2;
        end
        else if aDefaultPos = dfpLeftBottom then
        begin
          aForm.Left := 0 + 2;
          aForm.Top := nWorkHeight - aForm.Height - 2;
        end
        else if aDefaultPos = dfpRightTop then
        begin
          aForm.Left := nWorkWidth - aForm.Width - 2;
          aForm.Top := 0 + 2;
        end
        else if aDefaultPos = dfpRightBottom then
        begin
          aForm.Left := nWorkWidth - aForm.Width - 2;
          aForm.Top := nWorkHeight - aForm.Height - 2;
        end;

      end
      else
      with ConfigIni do
      begin
        if ValueExists(aForm.Name, 'top') then
        begin
          nTop := ReadInteger(aForm.Name, 'top', aForm.Top);
          aForm.Top := IfThen(nTop < 0, 0, nTop);
        end;

        if ValueExists(aForm.Name, 'left') then
        begin
          nLeft := ReadInteger(aForm.Name, 'left', aForm.Left);
          aForm.Left := IfThen(nLeft < 0, 0, nLeft);
        end;

        if ValueExists(aForm.Name, 'height') then
        begin
          nHeight := ReadInteger(aForm.Name, 'height', aForm.Height);
          aForm.Height := IfThen(nHeight > nWorkHeight, nWorkHeight, nHeight);
        end;

        if ValueExists(aForm.Name, 'width') then
        begin
          nWidth := ReadInteger(aForm.Name, 'width', aForm.Width);
          aForm.Width := IfThen(nWidth > nWorkWidth, nWorkWidth, nWidth);
        end;

      end;

    except
      on e: exception do
      begin

        // 오류가 나면 가운데 배치.
        aForm.Position := poScreenCenter;
        Exit;
      end
    end;
  finally

    ConfigIni.Free;
  end;

end;

 

[ 폼 위치 저장 ]

//
// 폼 위치 저장
//
procedure SaveFormPosition(aForm: TForm);
var
  ConfigIni: TIniFile;
begin

  try
    try
      // USER_INI_FILE은 사용자 정의 상수 파일명  예> config.ini
      ConfigIni := TIniFile.Create(USER_INI_FILE);

      ConfigIni.WriteInteger(aForm.Name, 'top', aForm.Top);
      ConfigIni.WriteInteger(aForm.Name, 'left', aForm.Left);
      ConfigIni.WriteInteger(aForm.Name, 'height', aForm.Height);
      ConfigIni.WriteInteger(aForm.Name, 'width', aForm.Width);
      if aForm.WindowState = wsMaximized then
        ConfigIni.WriteInteger(aForm.Name, 'ScreenMax', 1)
      else
        ConfigIni.WriteInteger(aForm.Name, 'ScreenMax', 0);

    except
    end;
  finally
    ConfigIni.Free;
  end;

end;

 

 

[ 폼 위치 초기화 ]

//
// 폼 위치 초기화
//
procedure InitFormPosition(aForm: TForm);
var
  ConfigIni: TIniFile;
begin

  try
    try
      // USER_INI_FILE은 사용자 정의 상수 파일명  예> config.ini
      ConfigIni := TIniFile.Create(USER_INI_FILE);

      ConfigIni.DeleteKey(aForm.Name, 'top');
      ConfigIni.DeleteKey(aForm.Name, 'left');
      ConfigIni.DeleteKey(aForm.Name, 'height');
      ConfigIni.DeleteKey(aForm.Name, 'width');
      ConfigIni.DeleteKey(aForm.Name, 'ScreenMax');

    except
    end;
  finally
    ConfigIni.Free;

    // 다시 로딩
    LoadFormPosition(aForm);

  end;

end;




반응형
댓글