我自己创建一个控件,原理是把一个image控件放在一个panel控件之上,这样在对这个新控件操作时,可以实现对图片在form中任意拖动的功能。同时在用户点击图片内的任意位置时,返回控件的两个新属性,是鼠标的x和y的坐标,我写了以下代码,但不知道该怎么继续:
 1、就是怎么把image控件的onmousedown方法中的x,y传给我的新属性mousex:mouseY: 
 2、就是我应该怎么写才能实现,一旦激发image控件的onmousedown方法,就连带处动
panel控件的onmousedown方法。
  
 第一次做控件,请高手指教。
 
 unit jlhImage;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;type
  TjlhEmptyPanel = class(Tpanel)
  private
     //
  public
    constructor create(AOwner : Tcomponent);override;
  protected
  end;  TjlhImage = class(TImage)
  private
   Xmouse : integer;
   Ymouse : integer;
   jlhEmptyPanel:TjlhEmptyPanel;
    { Private declarations }
  protected
    { Protected declarations }
  public
    constructor create(AOwner : Tcomponent);override;
 { Public declarations }
  published
    property mouseX: integer read Xmouse;
    property mouseY: integer read Ymouse;
    { Published declarations }
  end;procedure Register;implementationprocedure Register;
begin
  RegisterComponents('SamplesImage', [TjlhImage,TjlhEmptyPanel]);
end;{ TjlhEmptyPanel }constructor TjlhEmptyPanel.create(AOwner: Tcomponent);
begin
  inherited create(AOwner);
  caption:='';
  height:=500;
  width:=500;
  left:=10;
  top:=10;
end;{ TjlhImage }constructor TjlhImage.create(AOwner: Tcomponent);
begin
  inherited create(AOwner);
  jlhEmptyPanel:=TjlhEmptyPanel.create(nil);
  parent:=jlhEmptyPanel;
end;
end.

解决方案 »

  1.   

    在你的private或protected块中添加:
    procedure DoMouseDown(...);//参数列表可以参考标准的OnMouseDown事件
    在Create实现中添加:
    OnMouseDown := DoMouseDown;
    在DoMouseDown实现中添加
    MouseX := ...
    MouseY := ...
    如果是定制含有一个Image的Panel控件,建议如下:
    type
      TImagePanel = class(TCustomPanel)
      private
        FImage: TImage;
      ...
      protected
      ...
      public
      ...
      published
      ...
      end;
    ...constructor TImagePanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FImage := TImage.Create(Self);
      ...
    end;destructor TImagePanel.Destroy;
    begin
      ...
      FImage.Destroy;
      inherited Destroy;  
    end;
      

  2.   

    type
      TImagePanel = class(TCustomPanel)
      private
        FImage: TImage;
        Xmouse : integer;
        Ymouse : integer;
      ...
      protected
        procedure MyMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      ...
      public
      ...
      published
        property mouseX: integer read Xmouse;
        property mouseY: integer read Ymouse;
      ...
      end;procedure TImagePanel.MyMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      //当MouseDown时设置Xmouse,Ymouse;
      Xmouse := X;
      Ymouse := Y;
      //如果你的Panel被设置了MouseDown事件,则再调用它。
      if Assign(OnMouseDown) then
        OnMouseDown(Self,Button,Shift,X,Y);
    end;constructor TImagePanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FImage := TImage.Create(Self);
      FImage.OnMouseDown := MyOnMouseDown;//给你的Image设置MouseDown事件
      ...
    end;destructor TImagePanel.Destroy;
    begin
      ...
      FImage.Destroy;
      inherited Destroy;  
    end;
      

  3.   

    用TCustomPanel作为祖先继承时它的一些属性要你自己去引出。
      

  4.   

    呵呵,要这么麻烦吗?在组件中直接用API确定X,Y的值,再用一个自定义的属性(你也可以继承相应的方法)来返回X,Y不可以吗???
    还有,你完全可以有Panel组件创建组件,加一个fimage:timage;再加上相应的graphics单元做起来不是更快吗?