一个拖拽内容到Word的例子

这是一个拖拽内容到Word中的例子。如视频所示:

从程序中的Tree这拖内容到Word中。然后 在拖拽完成后事件中,记录日志。

拖拽

代码如下:

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    TreeView1: TTreeView;
    GroupBox1: TGroupBox;
    Memo1: TMemo;
    DropTextSource1: TDropTextSource;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure DropTextSource1AfterDrop(Sender: TObject; DragResult: TDragResult;
      Optimized: Boolean);
  private
    DNode: TTreeNode;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DropTextSource1AfterDrop(Sender: TObject;
  DragResult: TDragResult; Optimized: Boolean);
begin
if DragResult=drDropCopy then
  Memo1.Lines.Add('AfterDrop: ' + trim(DropTextSource1.Text));

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  n: TTreeNode;
begin
  TreeView1.Items.BeginUpdate();
  n := TreeView1.Items.AddChild(nil, '月巴月巴');
  for i := 1 to 100 do
    TreeView1.Items.AddChild(n, '月巴月巴' + inttostr(i));
  TreeView1.Items.EndUpdate();
  n.Expanded := true;
  top := 0;
  left := screen.PrimaryMonitor.Width - Width;
end;

procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  n: TTreeNode;
  s: UnicodeString;
begin
  n := TreeView1.GetNodeAt(X, Y);
  if n <> nil then
  begin
    DNode := n;
    TreeView1.Select(DNode);
    s := DNode.Text + '说: 啥' ;
    DropTextSource1.UnicodeText :=s;
    DropTextSource1.Text :=s;
    DropTextSource1.Execute();
  end;

end;

end.

你可能感兴趣的:(c#,开发语言)