代码:
unit UCust;interfaceuses
   Classes,Graphics,Controls;type
  TCustPanel = class(TGraphicControl)
  private
  public
    procedure Paint;override;
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
  end;implementation{ TCustPanel }constructor TCustPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 600;
  Height := 400;
end;destructor TCustPanel.Destroy;
begin
  inherited Destroy;
end;procedure TCustPanel.Paint;
var
  i : integer;
begin
  Canvas.Pen.Color := clBlue;
  Canvas.Rectangle(Left+20,Top+20,Width-20,Height-20);
  Canvas.FillRect(Rect(Left+22,Top+22,Width-22,Height-22));
  with Canvas do
  begin
    for i := 1 to 20 do
    begin
      MoveTo(Left+20,(Top+20)+i*40);
      LineTo(Width-20,(Top+20)+i*40);
    end;
  end;
end;end.
///////////////////////////
引用:
procedure TForm1.FormCreate(Sender: TObject);
begin
  with TCustPanel.Create(ScrollBox1) do
  begin
    Parent := ScrollBox1;
    Left := 0;
    Top := 0;
  end;
end;

解决方案 »

  1.   

    肯定乱,,因为你的代码逻辑错误;Left+20,Top+20,Width-20,Height-20);
    假设 Width=20;,Height=20;
    left=0.top=0;
    现在画的是一个Rectangle(20,20,0,0);
    现在滚动SCROOLBOX.
    left=20,top=20;那么现在画的东西变成Rectangle(40,40,0,0);呵呵,,问题出现了。两个矩形不一样大了。
      

  2.   

    to SydPink(希望不再敲键盘!) :
    我没有假设Width = 20; height = 20;
    Create()函数中我设定 Width := 600; Height := 400;
    滚动Scollbox,Left,top会改变吗?
    希望得到祥细的指点,谢谢!
      

  3.   

    我试了一下滚动Scollbox,Left,top是会改变,那我应该怎么办?
    最好有个例子.
      

  4.   

    问题已基本得到解决,只可惜拿分的人太少了。多谢SydPink(希望不再敲键盘!)的指点与Tensionli() 的光顾,再问一个问题:我上面的TCustPanel类怎么隐藏它的Width与Height属性?因为我不想使用者改变这两个属性,谢谢.