티스토리 뷰
반응형
프로그래밍 하다 보면 폼의 위치를 저장하고, 다시 저장된 위치로 폼을 로딩할 때가 있다.
[ 방법 설명 ]
- 프로그램을 종료할 때 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;
반응형
'프로그래밍' 카테고리의 다른 글
[Delphi] 델파이 라이브러리 제공되지 않는 문자열 필터링 함수 (4) | 2023.02.12 |
---|---|
[Delphi] 델파이 음력 양력 변환 함수 (0) | 2023.02.07 |
[Delphi] 델파이 썸네일 등 이미지 파일크기 축소 (0) | 2023.02.06 |
[Delphi] 델파이 PC명칭 OS버젼 MAC주소 HDD볼륨 구하는 함수 (0) | 2023.02.06 |
[Delphi] 델파이 내부IP 외부IP 구하는 함수 (0) | 2023.02.06 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 문자보내기 #PC문자보내기 #콜백문자 #콜백서비스 #자동문자 #통화매니저
- 소형전기드릴
- 전화특허
- 가정용전기드라이버
- 부재중문자
- 미니드릴
- 고객이음 #문자보내기 #단체문자 #고객관리 #회원관리 #콜백문자 #콜백서비스 #자동문자
- 내부IP구하기
- 선물하기좋은차
- 통화매니저 #KT통화매니저 #발신자정보표시 #고객관리 #콜백문자 #문자보내기
- 한양도성둘레길
- 동부시립병원
- 가정용전동드라이버
- 폼위치저장
- 델파이
- 전화번호대시넣기
- 성북동부자마을
- 소형전동드릴
- 명륜유치원
- 유기농건강차
- 통화매니저
- 도메인IP변환
- Hexa문자열변환
- 외부IP구하기
- 그레이엄빌
- 고객관리 #회원관리 #휴대폰주소록 #문자보내기 #콜백문자 #콜백서비스 #자동문자 #통화매니저
- 와룡공원드라마촬영장소
- 가정용전기드릴
- 델파이날짜연산 #델파이날짜함수 #날짜연산 #날짜함수 #델파이
- 성북동판자촌
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함