2014-04-24 1 views
-1

사용자 지정 구성 요소가 있습니다. 현재 폼에 TScrollbox을 배치하고 사용자 지정 구성 요소를 스크롤 상자에 추가/끌어 놓아야합니다.
폼에 배치/드래그 할 때 자동으로 스크롤 상자 안에 배치되도록 구성 요소를 변경하는 방법은 무엇입니까?스크롤 상자 안에 구성 요소 추가

맞춤 구성 요소는 TGraphicControl입니다.

+1

확실히 자신의 스크롤바를 호스팅하기 위해 구성 요소를 가져와야합니다 –

+0

네,하지만 스크롤 상자를 만들어서 desgintime 중에 드래그/드롭 할 때이 컴포넌트 부모를 어떻게 설정합니까? –

+0

그렇지 않습니다. 당신은 정말로 그런 식으로하고 싶지 않습니다. 그게 네가 물었던 것이지만 잘못된 해결책이다. 자체 스크롤 막대가있는 단일 컨트롤 인 컨트롤을 만듭니다. 스크롤 상자가 아닙니다. –

답변

3

사용자 지정 구성 요소가 있고 항상 스크롤 상자 안에 있으면 원하는 경우 가장 깨끗한 솔루션은 해당 구성 요소를 업데이트하거나 확장하여 자체 스크롤 상자를 갖게하는 것입니다. 다음은 TLabel을 사용하는 예이지만 맞춤 구성 요소가 무엇이든이를 바꿀 수 있습니다.

unit MyScrollBox; 

interface 

uses 
    System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls; 

type 
    TMyScrollComponent = class(TScrollBox) 
    private 
    FLabel : TLabel; 
    procedure SetLabelText(AText : string); 
    function GetLabelText : string; 
    protected 
    constructor Create(AOwner : TComponent); override; 
    published  
    property LabelText : string read GetLabelText write SetLabelText; 
    end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('Samples', [TMyScrollComponent]); 
end; 

constructor TMyScrollComponent.Create(AOwner : TComponent); 
begin 
    inherited; 
    FLabel := TLabel.Create(self); 
    FLabel.Parent := self; 
    FLabel.Caption := 'Hello From Scrollbox!'; 
end; 

procedure TMyScrollComponent.SetLabelText(AText : string); 
begin 
    FLabel.Caption := AText; 
end; 

function TMyScrollComponent.GetLabelText : string; 
begin 
    result := FLabel.Caption; 
end; 

end. 

이것은 TLabel을 포함, TScrollBox에서 상속되는 사용자 지정 구성 요소를 보여줍니다 및 사용자 지정 게시 속성을 통해 TLabelCaption 속성을 노출합니다. 이 패턴에 따라 사용자가 필요로하는 다른 컨트롤의 속성을 노출 할 수도 있습니다.

또는 스크롤 상자 내에서 레이아웃을 변경하는 기능을 유지하려는 경우 다른 해결책으로는 맞춤 TFrame. 그냥 add one to your project과 빌드 후, 팔레트 도구 Standard -> Frames에서 사용할 수있게됩니다.

+0

내가 이것을 할 때,이'create'는'map : = ThexMap.Create'를 호출 할 것입니다. 언제 THexMap을위한 create를 실행해야합니까? correct ... 실제로지도가없는 scrollbars를 얻고 있습니다. desgin 시간에'create'를 실행합니다. 하지만 내가 여기서 정확한지 확인하고 싶었어. –

+0

@GlenMorse 내가 볼 수없는 코드에 대한 참조를 만드는 것 같습니다. 나는 그것이 옳은지 아닌지 말할 수 없다. –

+0

좋아요,이 작품은 부모를 지정하는 것을 잊어 버렸습니다 : D이 대답을 받아 들일 것이지만 다른 질문으로 연결됩니다. (이 예제에서) Flabel OnMouseDown에 액세스하는 방법은 무엇입니까? 왜냐하면 구성 요소를 클릭하고 객체 검사기를 보면 스크롤 상자에 대한 OnMouseDown 만 있습니다. –