C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[1095] [Controls] Control의 위치나 크기를 조절할때는..
장성호 [nasilso] 8311 읽음    2012-06-20 17:30
초간단 팁 하나 올립니다.

[개요]

혹시 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;



[사족..]

뭐 아주 사소한 것이지만..
사소한 것이 성능을 좌우하는 경우가 왕왕 있더군요

그럼..
현도 [st8123]   2012-07-04 17:51 X
간과하고 넘어갈 수 있었던 것을 알게해준 좋은팁이군요.
감사합니다~~ ^^
두루마루 [abysmaleye]   2013-03-19 15:42 X
좋은 팁이네요...
leo21c [leo21c]   2014-01-21 17:36 X
모르고 있는 부분이네요. 감사합니다.

+ -

관련 글 리스트
1095 [Controls] Control의 위치나 크기를 조절할때는.. 장성호 8311 2012/06/20
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.