zoukankan      html  css  js  c++  java
  • 写一个可拖动的 TShape 回复 "韦韦" 的问题


    问题来源: http://www.cnblogs.com/del/archive/2009/03/09/1234066.html#1471535

    本例效果图:



    自定义类(TMyShape)单元 :
    unit Unit2;
    
    interface
    
    uses
      Classes, Controls, ExtCtrls;
    
    type
      TMyShape = class(TShape)
      private
        fMouseFlag: Boolean;
        fx,fy: Integer;
      protected
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer;
          Y: Integer); override;
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer;
          Y: Integer); override;
        procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
      end;
    
    implementation
    
    { TMyShape }
    
    procedure TMyShape.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    begin
      inherited;
      fx := X;
      fy := Y;
      fMouseFlag := True;
    end;
    
    procedure TMyShape.MouseMove(Shift: TShiftState; X, Y: Integer);
    begin
      inherited;
      if fMouseFlag then
      begin
        Left := Left + X - fx;
        Top := Top + Y - fy;
      end;
    end;
    
    procedure TMyShape.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    begin
      inherited;
      fMouseFlag := False;
    end;
    
    end.
    

    调用测试:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses Unit2;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Randomize;
      with TMyShape.Create(Self) do begin
        Brush.Color := Random($FFFFFF);
        Parent := Self;
        Left := 10;
        Top := 10;
      end;
    end;
    
    end.
    

  • 相关阅读:
    模板---templates
    django框架基础
    文件操作
    C++异常处理基本句法测试
    模板类在包含友元情况下的分离编写
    C++中构造函数作用
    hex文件和bin文件区别
    Keil MDK 和 IAR 两款ARM开发工具区别比较
    ubuntu下安装stm32开发环境
    IAR安装破解教程
  • 原文地址:https://www.cnblogs.com/del/p/1406811.html
Copyright © 2011-2022 走看看