不同程序之间拖拽的问题(100分)

  • 主题发起人 主题发起人 foye
  • 开始时间 开始时间
F

foye

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个程序甲(没有源码不能修改),打开子窗口A和B,能从A窗口的listview里拖拽任一行数据到B窗口的listview里。
我想自己做另一个程序,实现从我的程序向程序甲的B窗口里拖放listview里的内容,请问应该怎么做?分不够可以再加,谢谢!
 
我也不懂,帮顶,顺便学习了
 
学习ing。。。。
 
unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, ImgList;

type
TScrollDirection = (sdUp, sdDown, sdNone);

TForm1 = class(TForm)
TreeView1: TTreeView;
ImageList1: TImageList;
ListView1: TListView;
ListView2: TListView;
Timer1: TTimer;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
TargetWin: TWinControl;
ScrollDirection: TScrollDirection;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
//the maximum distance from the top or bottom of
//the WinControls in which a drag scroll will occur...
SCROLLMARGIN: integer = 18;

//---------------------------------------------------------------------

procedure TForm1.FormCreate(Sender: TObject);
var
rect: TRect;
begin
Treeview1.fullexpand;
//Get the height of a listview item for
//a better value for SCROLLMARGIN...
rect := Listview1.items[0].DisplayRect(drBounds);
SCROLLMARGIN := rect.bottom-rect.Top;
end;
//---------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;

//---------------------------------------------------------------------
// This demonstrates dragging of nodes within a treeview...
//---------------------------------------------------------------------

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source = Sender;
if not Accept then exit;

//See if scrolling is needed...
with TTreeView(Sender) do
begin
if (Y < SCROLLMARGIN) then
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;

if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
with TTreeView(Sender) do
begin
if (Sender <> Source) or (DropTarget = nil) or
(DropTarget = Selected) then exit;
Selected.MoveTo(DropTarget,naAddChildFirst);
end;
end;
//---------------------------------------------------------------------

procedure TForm1.TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;

//---------------------------------------------------------------------
// This demonstrates dragging of nodes between listviews...
//---------------------------------------------------------------------

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if (Source is TListView) {and (Source <> Sender)} then
Accept := true else
Accept := false;
if not Accept then exit;

//OK, now see if scrolling is needed...
with TListView(Sender) do
begin
if (Y < SCROLLMARGIN*2) then //nb: allow for the listview header
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;

if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------

procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
ListItem: TListItem;
begin
Timer1.enabled := false;
if not (Source is TListView) or (Source = Sender) then exit;
with TListView(Source).Selected do
begin
ListItem := TListView(Sender).items.add;
ListItem.Caption := Caption;
ListItem.ImageIndex := ImageIndex;
Delete; //deletes source item after adding target item
end;
end;
//---------------------------------------------------------------------

procedure TForm1.ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;

//---------------------------------------------------------------------
// Timer used with all WinControls for scrolling...
//---------------------------------------------------------------------

procedure TForm1.Timer1Timer(Sender: TObject);
begin
//Note: Timer1.interval = 100 but this can changed
//to speed up or slow down the scroll rate.
ImageList1.HideDragImage;
with TargetWin do
if ScrollDirection = sdUp then
sendmessage(handle,WM_VSCROLL,SB_LINEUP,0) else
sendmessage(handle,WM_VSCROLL,SB_LINEDOWN,0);
ImageList1.ShowDragImage;
end;

end.


unit draglistview;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, ImgList;

type
TScrollDirection = (sdUp, sdDown, sdNone);

TFrm_drag_listview = class(TForm)
TreeView1: TTreeView;
ImageList1: TImageList;
ListView1: TListView;
ListView2: TListView;
Timer1: TTimer;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
TargetWin: TWinControl;
ScrollDirection: TScrollDirection;
public
{ Public declarations }
end;

var
Frm_drag_listview: TFrm_drag_listview;

implementation

{$R *.DFM}

var
//the maximum distance from the top or bottom of
//the WinControls in which a drag scroll will occur...
SCROLLMARGIN: integer = 18;

//---------------------------------------------------------------------

procedure TFrm_drag_listview.FormCreate(Sender: TObject);
var
rect: TRect;
begin
Treeview1.fullexpand;
//Get the height of a listview item for
//a better value for SCROLLMARGIN...
rect := Listview1.items[0].DisplayRect(drBounds);
SCROLLMARGIN := rect.bottom-rect.Top;
end;
//---------------------------------------------------------------------

procedure TFrm_drag_listview.Button1Click(Sender: TObject);
begin
close;
end;

//---------------------------------------------------------------------
// This demonstrates dragging of nodes within a treeview...
//---------------------------------------------------------------------

procedure TFrm_drag_listview.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source = Sender;
if not Accept then exit;

//See if scrolling is needed...
with TTreeView(Sender) do
begin
if (Y < SCROLLMARGIN) then
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;

if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------

procedure TFrm_drag_listview.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
with TTreeView(Sender) do
begin
if (Sender <> Source) or (DropTarget = nil) or
(DropTarget = Selected) then exit;
Selected.MoveTo(DropTarget,naAddChildFirst);
end;
end;
//---------------------------------------------------------------------

procedure TFrm_drag_listview.TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;

//---------------------------------------------------------------------
// This demonstrates dragging of nodes between listviews...
//---------------------------------------------------------------------

procedure TFrm_drag_listview.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if (Source is TListView) {and (Source <> Sender)} then
Accept := true else
Accept := false;
if not Accept then exit;

//OK, now see if scrolling is needed...
with TListView(Sender) do
begin
if (Y < SCROLLMARGIN*2) then //nb: allow for the listview header
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;

if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------

procedure TFrm_drag_listview.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
ListItem: TListItem;
begin
Timer1.enabled := false;
if not (Source is TListView) or (Source = Sender) then exit;
with TListView(Source).Selected do
begin
ListItem := TListView(Sender).items.add;
ListItem.Caption := Caption;
ListItem.ImageIndex := ImageIndex;
Delete; //deletes source item after adding target item
end;
end;
//---------------------------------------------------------------------

procedure TFrm_drag_listview.ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;

//---------------------------------------------------------------------
// Timer used with all WinControls for scrolling...
//---------------------------------------------------------------------

procedure TFrm_drag_listview.Timer1Timer(Sender: TObject);
begin
//Note: Timer1.interval = 100 but this can changed
//to speed up or slow down the scroll rate.
ImageList1.HideDragImage;
with TargetWin do
if ScrollDirection = sdUp then
sendmessage(handle,WM_VSCROLL,SB_LINEUP,0) else
sendmessage(handle,WM_VSCROLL,SB_LINEDOWN,0);
ImageList1.ShowDragImage;
end;

end.
 
to tang_xue_feng :
这个真能实现跨程序之间的拖拽?能在QQ里交流吗? 123601069
 
高手们,快来呀
 
copy from 独帅
//SetLVItemText: 修改其他进程中ListView的节点的Text
//有几个函数没有检查返回值,在实际应用时最好加上
//hListView: ListView的Handle
//iIndex : 要修改的ListView的节点索引(0为第一个...)
//sNewText : 要修改成的文本
procedure SetLVItemText(const hListView: HWND; const iIndex: Integer; const sNewText: String);
var
hli: TLVItem;
Buff: PChar;
PID: THandle;
PRC: THandle;
R: Cardinal;
begin
GetWindowThreadProcessId(hListView, @PID);
PRC := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, PID);
Buff := VirtualAllocEx(PRC, nil, 4096, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);

FillChar(hli, sizeof(hli), 0);
hli.mask := LVIF_TEXT;
hli.pszText := PChar(integer(Buff) + SizeOf(hli) * 4);
hli.cchTextMax := Length(sNewText)+1;

WriteProcessMemory(PRC, Buff, @hli, Sizeof(hli), R);

WriteProcessMemory(PRC, PChar(integer(Buff) + SizeOf(hli) * 4), PChar(sNewText), Length(sNewText)+1, R);

SendMessage(hListView, LVM_SETITEMTEXT, iIndex, LongInt(Buff));

VirtualFreeEx(PRC, Buff, 0, MEM_RELEASE);
CloseHandle(PRC);
end;
 
还有更好的方法没有?
 
多人接受答案了。
 
后退
顶部