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
[944] [VCL] FindControl , FindVCLWindow
장성호 [nasilso] 11330 읽음    2009-12-17 11:42
[FindControl함수용도]
윈도우 핸들은 알고있는데..
그 윈도우 핸들을 가진 WinControl의 객체를 모를경우
FindControl 이라는 VCL함수를 이용하여 객체를 찾을수 있습니다.

간단히 사용예를 만들어보면..
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TWinControl *wCtrl=FindControl(Button2->Handle);
    if(wCtrl!=NULL && wCtrl->InheritsFrom(__classid(TButton)))
    {
        ((TButton *)wCtrl)->Click();
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    ShowMessage("Button2Click");   
}
//---------------------------------------------------------------------------

//Button2의 핸들로 Button2를 찾아 Button2의 OnClick이벤트 호출하는 코드입니다.
procedure TForm1.Button1Click(Sender: TObject); 
var 
  wCtrl: TWinControl; 
begin 
  wCtrl:=FindControl(Button2.Handle); 
  if (wCtrl<> nil) and (wCtrl is TButton) then 
  begin 
    TButton(wCtrl).Click; 
  end; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 
begin 
  ShowMessage('Button2Click'); 
end; 



위 함수가 어디 얼마나  쓰일데 있을까요?
[VCL에서 FindControl 이용은?]
VCL에서는 Drag-Drop 기능이 FindControl 함수를 이용하여 구현되어있습니다.

Drag-Drop기능을 구현할때 마우스 커서 아래의 Control의 인스턴스를 찾아야 하는데..
WindowFromPoint 로 마우스 커서 아래의 Window-Handle을 찾을수 있지만
객체의 인스턴스를 그냥 찾아지는것은 아니죠
이때 FindVCLWindow 함수를 이용하는데
FindVCLWindow  함수 내부에서 FindControl을 사용합니다.

[FindVCLWindow 소스]

function FindVCLWindow(const Pos: TPoint): TWinControl;
var
  Handle: HWND;
begin
  Handle := WindowFromPoint(Pos);
  Result := nil;
  while Handle <> 0 do
  begin
    Result := FindControl(Handle);
    if Result <> nil then Exit;
    Handle := GetParent(Handle);
  end;
end;

//vcl의 pas소스를 C++Builder 작성하면..
TWinControl* __fastcall FindVCLWindow(const TPoint Pos)
{
    HWND Handle = WindowFromPoint(Pos);
    TWinCotnrol *Result=NULL;
    while( Handle)
    {
        Result= FindControl(Handle);
        if(Result)return Result;
        Handle = GetParent(Handle);
    }
    return NULL;
}
//---------------------------------------------------------------------------


[FindVCLWindow함수에 왜 GetParent가 쓰일까?]

Handle을 가지고 VCL객체를 찾는데 외 GetParent를 통해 Parent-Handle을 가지고
FindControl을 반복할까요?

음..
이유는?
많은 VCL의 WinControl에서 내부적으로 그냥 Win32-control을 가지고 있는경우가 많이 있습니다.

예를들어
   TComboBox 내부에 editing기능은 VCL컨트롤인 아닌 win32-컨트롤인 "EDIT"를 쓰고 있구요
   TTreeView에서 Node를 editing할때도 ,
   TListView에서 Item을 editing할때도..
   
   이런 컨트롤은 내부적으로 editing용 win32컨트롤인 "edit"를 가지고 있어서
  TComboBox 위에 마우스가 간 상태에서
   WindowFromPoint 로 찾은 윈도우 핸들이  ComboBox->Handle이 아니라
   ComboBox안에 있는 edit의 Handle이 되는경우가 있기 때문입니다.


위 함수가 어떤 업무에 어떻게 쓰일지 잘모르겠으나...
이런 함수가 있다는것.. 알아서 나쁠건 없겠죠..


그럼..

+ -

관련 글 리스트
944 [VCL] FindControl , FindVCLWindow 장성호 11330 2009/12/17
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.