Показать сообщение отдельно
  #3  
Старый 06.04.2013, 16:36
MDM MDM вне форума
Прохожий
 
Регистрация: 06.04.2013
Сообщения: 3
Версия Delphi: D7
Репутация: 10
По умолчанию

Я понимаю, что нужно делать как вы говорите, но топчусь на месте...
Посмотрите код, где я наврал?
Также, наверняка у меня будут проблемы с событиями leave и move, до них я еще не добрался...

Код:
unit gui;

interface

uses Graphics, Classes;

type
  TBaseObject = class;

  TMouseState = (ms_MouseOut, ms_MouseIn);
  TOnMouseClick = Procedure (aSender: TBaseObject) of object;
  TOnMouseMove = Procedure (aSender: TBaseObject) of object;

  TBaseObject = class (TObject)
    private
      X,Y,Width,Height: integer;
      Down, Focus, Visible: boolean;

      function IsHit(aMouseX, aMouseY: Single): boolean;
    protected
      fMouseState: TMouseState;
      fOnMouseClick: TOnMouseClick;
      fOnMouseMove: TOnMouseMove;
      FOnClick: TNotifyEvent;
    public
      Name : string;
      SpritePathEnadle,
      SpritePathDisable,
      SpritePathFocuse: string;
      Constructor Create(AOwner: TComponent); //override;
      property OnMouseClick: TOnMouseClick read fOnMouseClick write fOnMouseClick;
      property OnMouseMove: TOnMouseMove read fOnMouseMove write fOnMouseMove;
      property OnClick: TNotifyEvent read FOnClick write FOnClick;

      Procedure SetMouseState(aMouseX, aMouseY: Single; aIsButtonDown: Boolean);
      procedure SetMouseMove(aMouseX, aMouseY: Single);
      procedure Draw(Canvas: TCanvas);
  end;

  TMyButton = class (TBaseObject)
    private
      procedure ButtonClick(sender: TObject);
    public
      procedure SetPos(aX,aY, aW,aH:integer); virtual;
  end;


implementation


constructor TBaseObject.Create(AOwner: TComponent);
begin
  fMouseState := ms_MouseOut;
  Visible:=true;
  Down:=false;
  Focus:=false;
  fOnMouseClick := nil;
  fOnMouseMove:= nil;
end;

procedure TBaseObject.Draw(Canvas: TCanvas);
var
  bmp: TBitmap;
  path: string;
begin
  bmp:= TBitmap.Create;
  if not Down then
  begin
    if Focus then
      path:=SpritePathFocuse
    else
      path:=SpritePathDisable;
  end
  else
    path:=SpritePathEnadle;
    
  bmp.LoadFromFile(path);
  Canvas.Draw(X,Y,bmp);
  bmp.Free;
end;

function TBaseObject.IsHit(aMouseX, aMouseY: Single): boolean;
begin
  result := (aMouseX >= X) and (aMouseX <= Width) and (aMouseY >= Y) and (aMouseY <= Height);
end;

procedure TBaseObject.SetMouseMove(aMouseX, aMouseY: Single);
begin
  Focus:=IsHit(aMouseX,aMouseY);
  if Focus and Assigned(fOnMouseMove) then
    fOnMouseMove(self);
end;

procedure TBaseObject.SetMouseState(aMouseX, aMouseY: Single;  aIsButtonDown: Boolean);
begin
  Down:=aIsButtonDown;
  if IsHit(aMouseX, aMouseY) then
  begin
    if Assigned(fOnMouseClick) then
      fOnMouseClick(self);
  end;
end;


{ TMyButton }


procedure TMyButton.ButtonClick(sender: TObject);
begin
  inherited;
  Name:=Name+'Hi';

end;

procedure TMyButton.SetPos(aX, aY, aW,aH: integer);
begin
  X:=aX;  Y:=aY;
  Width:=aW;  Height:=aH;
end;


end.

маин
Код:
unit Unit1;

interface

uses
  ...,
  gui;

type
  TForm1 = class(TForm)
  //...
  private
    { Private declarations }
  public
    procedure ButtonClick;
  end;

var
  Form1: TForm1;
  btn1: TMyButton;
  MousLeftDown: boolean=false;

implementation

{$R *.dfm}


procedure TForm1.ButtonClick;
begin
   ShowMessage('hello world');
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled:=true;
  btn1 := TMyButton.Create(Self);
  btn1.SetPos(20,20,128,32);
  btn1.Name:='Button1';
  btn1.SpritePathEnadle:='..\mygui\textures\down.bmp';
  btn1.SpritePathDisable:='..\mygui\textures\up.bmp';
  btn1.SpritePathFocuse:='..\mygui\textures\focus.bmp';
  btn1.Draw(image1.Picture.Bitmap.Canvas);
  //btn1.OnClick:=ButtonClick;//ошибка
//[Error] Unit1.pas(57): Incompatible types: 'Parameter lists differ'
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then MousLeftDown:=true;
  btn1.SetMouseState(x, y, MousLeftDown);
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
    MousLeftDown:=false;
  btn1.SetMouseState(x, y, MousLeftDown);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  pos:TPoint;
begin
  btn1.Draw(Image1.Canvas);
  GetCursorPos(pos);
  Label1.Caption:='X= '+IntToStr(pos.X)+' Y= '+IntToStr(pos.Y);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);
begin
  btn1.SetMouseMove(x,y);
end;

end.
Ответить с цитированием