• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi–拖放时移动图像

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

我在TPanel上有一个T Image,另一个(空)TPanels.我想拖 
图像从第一个到第二个面板使用拖放.

我真的想看到图像从一个面板移动到
其他(半透明).

我想我应该使用TDragObject.GetDragImages,但我不知道如何构建整个魔术.

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X,Y: Integer);
begin
  Image1.ControlStyle := Image1.ControlStyle + [csDisplayDragImage]; // ???
  TImage(Sender).BeginDrag(False);
end;
 
procedure TForm1.Image1StartDrag(Sender: TObject; var DragObject: TDragObject);
begin
  // ???
end;
 
procedure TForm1.Panel1DragOver(Sender,Source: TObject; X,Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if (Source is TImage) then
    Accept := TImage(Source).Parent <> Sender;
end;
 
procedure TForm1.Panel1DragDrop(Sender,Y: Integer);
begin
  if (Source is TImage) then
  begin
    TImage(Source).Parent := TPanel(Sender);
    TImage(Source).Align := alClient;
  end;
end;

更新 – 我发现一个有用的文章:Implementing Professional Drag & Drop In VCL/CLX Applications

解决方法

unit Unit3;
 
interface
// 2012 Thomas Wassermann - demo
uses
  Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,ExtCtrls;
 
type
 
 
  TMyDragObject = class(TDragControlObject)
  private
    FImageList:TImageList;
    FDragSource:TControl;
  protected
    function GetDragImages: TDragImageList; override;
  public
    Procedure StartDrag(G:TGraphic;p:TPoint;DragSource:TControl);
    Constructor Create(AControl: TControl); override;
    Destructor Destroy;override;
    Property DragSource:TControl read FDragSource;
  end;
 
 
 
  TForm3 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Image1: TImage;
    procedure Image1StartDrag(Sender: TObject; var DragObject: TDragObject);
    procedure FormCreate(Sender: TObject);
    procedure Panel1DragOver(Sender,Y: Integer; State: TDragState; var Accept: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure Panel1DragDrop(Sender,Y: Integer);
  private
    { Private-Deklarationen }
    FDragObject:TMyDragObject;
  public
    { Public-Deklarationen }
  end;
 
var
  Form3: TForm3;
 
implementation
 
constructor TMyDragObject.Create(AControl: TControl);
begin
  inherited;
  FImageList:=TImageList.Create(nil);
end;
 
destructor TMyDragObject.Destroy;
begin
  FImageList.Free;
  inherited;
end;
 
function TMyDragObject.GetDragImages: TDragImageList;
begin
   Result := FImageList;
end;
 
{$R *.dfm}
 
procedure TMyDragObject.StartDrag(G: TGraphic;p:TPoint;DragSource:TControl);
var
 bmp:TBitMap;
begin
   FDragSource := DragSource;
   bmp:=TBitMap.Create;
   try
   FImageList.Width := g.Width;
   FImageList.Height := g.Height;
   bmp.Width := g.Width;
   bmp.Height := g.Height;
   bmp.Canvas.Draw(0,g);
   FImageList.Add(bmp,nil);
   finally
     bmp.Free;
   end;
    FImageList.SetDragImage(0,p.x,p.y)
end;
 
procedure TForm3.FormCreate(Sender: TObject);
var
 i:Integer;
begin
  ControlStyle := ControlStyle + [csDisplayDragImage];
  for I := 0 to ControlCount -1  do
      if Controls[i] is TPanel then
         TPanel(Controls[i]).ControlStyle := TPanel(Controls[i]).ControlStyle + [csDisplayDragImage];
  ReportMemoryLeaksOnShutDown := True;
end;
 
procedure TForm3.FormDestroy(Sender: TObject);
begin
  if Assigned(FDragObject) then FDragObject.Free;
end;
 
procedure TForm3.Image1StartDrag(Sender: TObject; var DragObject: TDragObject);
var
 p:TPoint;
 
begin
    p:=TImage(Sender).ScreenToClient(mouse.cursorpos);
    if Assigned(FDragObject) then FDragObject.Free;
    FDragObject := TMyDragObject.Create(TImage(Sender));
    FDragObject.StartDrag(TImage(Sender).Picture.Graphic,p,TImage(Sender));
    DragObject := FDragObject;
end;
 
procedure TForm3.Panel1DragDrop(Sender,Y: Integer);
begin
  if FDragObject.DragSource is TImage then
    TImage(FDragObject.DragSource).Parent := TPanel(Sender);
end;
 
procedure TForm3.Panel1DragOver(Sender,Y: Integer; State: TDragState; var Accept: Boolean);
begin
    Accept := Source is TMyDragObject;
end;
 
end.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab中的try...catch...end发布时间:2022-07-18
下一篇:
MATLAB建立图表的基础发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap