2011-08-19 62 views
2

我正在編寫一個WYSIWYG類型的編輯器程序,用戶可以將圖像縮略圖拖動到編輯器表面(TPanel)上,然後通過將編輯器表面渲染到PDF上來創建PDF。如何從JvtThumbview中拖動縮略圖?

在我的TPanel上,我有一個TImage,用戶可以調整和移動。我正在使用TSizeCtrl。

我有一個TJvThumbview正在從磁盤文件夾加載圖像。

我想完成從JvThumbview到TImage的拖放 - 但不能這樣做。

請有人能詳細說明我該如何做到這一點?

非常感謝。

+2

這是相當普遍的。請詳細說明您的要求以及您遇到的問題。 –

+1

請參閱[Brian Long](http://www.blong.com/Conferences/BorCon2001/DragAndDrop/4114.htm)的優秀拖放教程。 – NGLN

回答

4

我無法抗拒。

我的演示項目包括:

  • 一個TJvThumbView
  • 一個TImage

拖動實現:

  • 開始拖動操作,當用戶下鼠標拇指視圖上的起伏,
  • 管理拖動圖像的TDragObject衍生物,
  • 當拖動對象表示拖動操作在TImage上結束時繪製拖動的圖像。

這是怎麼可能看起來像:

unit Unit1; 

interface 

uses 
    Classes, Graphics, Controls, Forms, JvExForms, JvBaseThumbnail, JvThumbViews, 
    ExtCtrls; 

type 
    TMyDragObject = class(TDragControlObjectEx) 
    private 
    FDragImages: TDragImageList; 
    FPictureToDrag: TPicture; 
    protected 
    function GetDragImages: TDragImageList; override; 
    procedure Finished(Target: TObject; X, Y: Integer; 
     Accepted: Boolean); override; 
    public 
    constructor CreateFromThumbView(ThumbView: TJvThumbView); 
    destructor Destroy; override; 
    end; 

    TForm1 = class(TForm) 
    JvThumbView1: TJvThumbView; 
    Image1: TImage; 
    procedure FormCreate(Sender: TObject); 
    procedure JvThumbView1MouseDown(Sender: TObject; Button: TMouseButton; 
     Shift: TShiftState; X, Y: Integer); 
    procedure JvThumbView1StartDrag(Sender: TObject; 
     var DragObject: TDragObject); 
    procedure Image1DragOver(Sender, Source: TObject; X, Y: Integer; 
     State: TDragState; var Accept: Boolean); 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    // Fill our image list with arbitrary images 
    if JvThumbView1.Directory = '' then 
    JvThumbView1.Directory := 'C:\Users\Public\Pictures\Sample Pictures'; 
    // Style all controls for showing the drag image if Delphi version is D7 or 
    // lower. See also comment in TMyDragObject.CreateFromThumbView 
    JvThumbView1.ControlStyle := JvThumbView1.ControlStyle + 
    [csDisplayDragImage]; 
    Image1.ControlStyle := Image1.ControlStyle + [csDisplayDragImage]; 
    ControlStyle := ControlStyle + [csDisplayDragImage]; 
end; 

procedure TForm1.Image1DragOver(Sender, Source: TObject; X, Y: Integer; 
    State: TDragState; var Accept: Boolean); 
begin 
    // The destination image component accepts all drag operations 
    Accept := True; 
end; 

procedure TForm1.JvThumbView1MouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    // If mouse down on a thumb... 
    if JvThumbView1.SelectedFile <> '' then 
    // then let's start dragging 
    JvThumbView1.BeginDrag(False, Mouse.DragThreshold); 
end; 

procedure TForm1.JvThumbView1StartDrag(Sender: TObject; 
    var DragObject: TDragObject); 
begin 
    // DragObject will automatically be destroyed when necessary when it's 
    // derived from TDragControlObjectEx 
    DragObject := TMyDragObject.CreateFromThumbView(JvThumbView1); 
end; 

{ TMyDragObject } 

const 
    DragImageSize = 100; 

constructor TMyDragObject.CreateFromThumbView(ThumbView: TJvThumbView); 
begin 
    inherited Create(ThumbView); 
    // This is the picture the user will drag around 
    FPictureToDrag := TPicture.Create; 
    FPictureToDrag.LoadFromFile(ThumbView.SelectedFile); 
    // We want a nice drag image, but this property is only available in >D7 
    { AlwaysShowDragImages := True; } 
end; 

destructor TMyDragObject.Destroy; 
begin 
    FDragImages.Free; 
    FPictureToDrag.Free; 
    inherited Destroy; 
end; 

procedure TMyDragObject.Finished(Target: TObject; X, Y: Integer; 
    Accepted: Boolean); 
begin 
    // Finished dragging 
    inherited Finished(Target, X, Y, Accepted); 
    // If we are over an Image component, then draw the picture 
    if Accepted and (Target is TImage) then 
    TImage(Target).Canvas.StretchDraw(Bounds(X, Y, DragImageSize, 
     DragImageSize), FPictureToDrag.Graphic); 
end; 

function TMyDragObject.GetDragImages: TDragImageList; 
var 
    DragImage: TBitmap; 
begin 
    if FDragImages = nil then 
    begin 
    FDragImages := TDragImageList.Create(nil); 
    // Set dimensions of drag image list 
    FDragImages.Width := DragImageSize; 
    FDragImages.Height := DragImageSize; 
    // Prepare drag image 
    DragImage:= TBitmap.Create; 
    try 
     DragImage.Width := DragImageSize; 
     DragImage.Height := DragImageSize; 
     DragImage.Canvas.StretchDraw(Rect(0, 0, DragImage.Width, 
     DragImage.Height), FPictureToDrag.Graphic); 
     FDragImages.AddMasked(DragImage, clWhite); 
    finally 
     DragImage.Free; 
    end; 
    end; 
    Result := FDragImages; 
end; 

end. 
+0

謝謝,也可以鏈接到Brian Long! –