XXXXXXXXX--------------又是“剪切板”的问题(关于Copy/Cut文件)!--------------XXXXXXXXX ( 积分: 10

  • 主题发起人 主题发起人 starsite
  • 开始时间 开始时间
S

starsite

Unregistered / Unconfirmed
GUEST, unregistred user!
XXXXXXXXX--------------又是“剪切板”的问题(关于Copy/Cut文件)!--------------XXXXXXXXX ( 积分: 100 )<br />用 TShellListView 的右键菜单“剪切”文件。然后读取剪切板数据,你会发现,只能取到文件路径,但无法判断是Copy还是Cut! (注:用外部程序判断是正常的,只有在进程内判断又问题!)
期待望高手指点!

读取剪切板的函数如下:
const
DROPEFFECT_COPY = 1; // 复制
DROPEFFECT_MOVE = 2; // 剪切

function GetClipbrdFiles(const fileDirs: TStrings; var dwEffect: Cardinal) : Boolean;
var
f: THandle;
buffer: array [0..MAX_PATH] of Char;
i, numFiles : Integer;
uDropEffect : Cardinal;
begin
Result := false;
if not Clipboard.HasFormat(CF_HDROP) then
begin
Exit;
end;

try
Clipboard.Open();
except
end;

try
f := Clipboard.GetAsHandle(CF_HDROP);
if f &lt;&gt; 0 then
begin
uDropEffect := RegisterClipboardFormat('Preferred DropEffect');
dwEffect := PCardinal(Clipboard.GetAsHandle(uDropEffect))^;
if fileDirs &lt;&gt; nil then
begin
fileDirs.Clear();
numFiles := DragQueryFile(f, $FFFFFFFF, nil, 0);
for i := 0 to numfiles - 1 do
begin
buffer[0] := #0;
DragQueryFile(f, i, buffer, SizeOf(buffer));
fileDirs.Add(buffer);
end;
end;
end;
finally
Clipboard.Close();
end;

Result := (fileDirs.Count &gt; 0);
end;
 
function GetDropEffectString(DropEffect :Cardinal):string;
begin
Result := '';
if DropEffect = DROPEFFECT_NONE then
begin
Result := 'DROPEFFECT_NONE =0';
Exit;
end;
if DropEffect and DROPEFFECT_COPY &gt;0 then
Result := 'DROPEFFECT_COPY='+IntToStr(DROPEFFECT_COPY) +',';
if DropEffect and DROPEFFECT_MOVE &gt;0 then
Result := Result + 'DROPEFFECT_MOVE='+IntToStr(DROPEFFECT_MOVE) +',';
if DropEffect and DROPEFFECT_LINK &gt;0 then
Result := Result + 'DROPEFFECT_LINK='+IntToStr(DROPEFFECT_LINK) +',';
if DropEffect and DROPEFFECT_SCROLL &gt;0 then
Result := Result + 'DROPEFFECT_SCROLL='+IntToStr(DROPEFFECT_SCROLL) +',';
if Result &lt;&gt; '' then
Result := IntToStr(DropEffect) + ':'+ Copy(Result,1,Length(Result)-1)
else
Result := IntToStr(DropEffect);
end;

function GetClipbrdFiles(hData :THandle):string;
var
i,iFilesCount : integer;
FileName :array [0..MAX_PATH] of char;
dataObject :IDataObject;
formatEtc : TFormatEtc;
stgMedium : TStgMedium;
h ,dwEffect :THandle;
uDropEffect :UINT;
begin
Result := '';
if hData&gt;0 then
begin
// hData 必须是 GetClipboardData(CF_HDROP) 返回值或其它有效的DragFile句柄值
iFilesCount := DragQueryFile(hData, $FFFFFFFF, nil, 0);
for i:=0 to iFilesCount -1 do
begin
DragQueryFile(hData, i, FileName, SizeOf(FileName));
Result := Result + sLineBreak + FileName;
end;
end
else
begin
if not OpenClipboard(0) then Exit;
try
uDropEffect := RegisterClipboardFormat('Preferred DropEffect'); // 一般 CF_HDROP 带
if IsClipboardFormatAvailable(uDropEffect) then
begin
h := GetClipboardData(uDropEffect); // Clipboard.GetAsHandle
try
dwEffect := PCardinal(GlobalLock(h))^;
finally
GlobalUnlock(h);
end;
Result := 'Preferred DropEffect: ' + GetDropEffectString(dwEffect);
end;
finally
CloseClipboard;
end;

if not IsClipboardFormatAvailable(CF_HDROP) then Exit;
case OleGetClipboard(dataObject) of
S_OK:
begin
FillChar(formatEtc,SizeOf(formatEtc),0);
formatEtc.cfFormat := CF_HDROP;
formatEtc.dwAspect := DVASPECT_CONTENT;
formatEtc.tymed := TYMED_HGLOBAL;
dataObject.GetData(formatEtc,stgMedium); //

Result := Result + sLineBreak +
'CF_HDROP: stgMedium.hGlobal=' + IntToStr(stgMedium.hGlobal)+
' tymed =' + IntToStr(stgMedium.tymed) + sLineBreak ;
// 下面几句可递归调用
iFilesCount := DragQueryFile(stgMedium.hGlobal, $FFFFFFFF, nil, 0);
for i:=0 to iFilesCount -1 do
begin
DragQueryFile(stgMedium.hGlobal, i, FileName, SizeOf(FileName));
Result := Result + FileName + sLineBreak;
end;
ReleaseStgMedium(stgMedium);
end;
E_OUTOFMEMORY: Result := Result + sLineBreak +' E_OUTOFMEMORY';
CLIPBRD_E_CANT_CLOSE: Result := Result + sLineBreak +' CLIPBRD_E_CANT_CLOSE';
CLIPBRD_E_CANT_OPEN: Result := Result + sLineBreak +' CLIPBRD_E_CANT_OPEN';
end; // case end.
end;
end;
 
后退
顶部