초간단 팁 하나 올립니다.
[개요]
혹시 Control위 위치나 크기를 조절할때 어떻게 코딩하십니까?
Control->Left = 10;
Control->Top = 10;
Control->Width = 300;
Control->Height = 200;
혹시 위와같은 식으로 코딩하시는 분 안계신가요?
대게의 경우에 별 문제가 없는 코드라 할수 있습니다.
[위 코드의 문제점]
음..
그런데 Control의 Left,Top,Width,Height 등을 수정하면
SetBounds 함수가 호출됩니다.
procedure TControl.SetLeft(Value: Integer);
begin
SetBounds(Value, FTop, FWidth, FHeight);
//생략
end;
procedure TControl.SetTop(Value: Integer);
begin
SetBounds(FLeft, Value, FWidth, FHeight);
//생략
end;
procedure TControl.SetWidth(Value: Integer);
begin
SetBounds(FLeft, FTop, Value, FHeight);
//생략
end;
procedure TControl.SetHeight(Value: Integer);
begin
SetBounds(FLeft, FTop, FWidth, Value);
//생략ㅋ
end;
그런데 SetBounds 함수를 보면
호출할때마다 화면갱신 및 이벤트 처리를 하고 있습니다.
procedure TControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if CheckNewSize(AWidth, AHeight) and
((ALeft <> FLeft) or (ATop <> FTop) or
(AWidth <> FWidth) or (AHeight <> FHeight)) then
begin
InvalidateControl(Visible, False);
FLeft := ALeft;
FTop := ATop;
FWidth := AWidth;
FHeight := AHeight;
UpdateAnchorRules;
UpdateExplicitBounds;
Invalidate; // 화면 갱신
Perform(WM_WINDOWPOSCHANGED, 0, 0);
RequestAlign;
if not (csLoading in ComponentState) then Resize;
end;
end;
즉 위와같이 Control의 크기 위치를 수정하기 위해
Left , Top , Width , Height 프로퍼티를 access해서 수정하는 경우에
SetBounds를 여러번 호출하게 되고, 화면갱신이 여러번 발생하게 되는것입니다.
[Control의 위치및 크기 조절은?]
결론 적으로 Control의 위치 및 크기 조절은
SetBounds 함수를 사용하라는 것입니다.
또는
BoundsRect 프로퍼티 이용 하는것이 대게 좋을듯 합니다.
//샘플1
Control->SetBounds(10,10,width,height);
//샘플2
TRect rc(10,10,10+width,20+height);
Control->BoundsRect = rc ;
// BoundsRect 프로퍼티도 내부적으로 SetBounds를 호출합니다.
procedure TControl.SetBoundsRect(const Rect: TRect);
begin
with Rect do SetBounds(Left, Top, Right - Left, Bottom - Top);
end;
[사족..]
뭐 아주 사소한 것이지만..
사소한 것이 성능을 좌우하는 경우가 왕왕 있더군요
그럼..
감사합니다~~ ^^