본문 바로가기

개발/Delphi_XE2

[Delphi_XE2] 모니터 구분해서 창 띄우기

투 모니터를 많이들 사용하는 상황인데 이때 동작하는 화면도 구분돼서 뜬다면 편리하겠다는 생각이 들었습니다.

 

해당 과정을 미리 간단하게 정리하겠습니다.

  1. 모니터의 개수가 2 개 이상인지 확인
  2. 2 개 이상이라면 Position 속성을 poDesigned로 변경
  3. 원하는 모니터에 배치하기

총 세 단계만 생각하면 간단합니다. 코드와 같이 살펴보겠습니다.

 

1. 모니터의 개수가 2 개 이상인지 확인하고, 2 개 이상이라면 Position 속성을 poDesigned로 변경

// onCreate event
// check monitor count is more than 1
// and change Position property
if Screen.MonitorCount > 1 then
begin
    Self.Position := poDesigned;
end
else
begin
    Self.Position := poScreenCenter;
end;

 

2. 원하는 모니터에 배치하기(위치는 본인이 원하는 좌표로 계산해서 배치하면 됩니다.)

// onShow event
if (Screen.MonitorCount > 1) and (Self.Position = poDesigned) then
begin
    // choose monitor to show that window
    TargetMonitor := Screen.Monitors[0];
    
    // change position with Left, Top
    Self.Left := TargetMonitor.Left + (TargetMonitor.Width - Self.Width) div 2;
    Self.Top := TargetMonitor.Top + (TargetMonitor.Height - Self.Height) div 2;
end;

 

끝입니다.

 

주의할 점은 Position 속성을 onShow에서 진행하면 안 됩니다.
왜냐하면 onShow 또는 onHide 이벤트에서는 visible에 대한 것을 수정할 수 없습니다.

만약 수정하게 된다면 다음과 같은 에러 메시지를 마주할 수 있습니다.