歡迎光臨 Code²
Code Square, CodeSqaure, Pascal, Javascript
在Delphi中接受拖放並不難,只要在FormCreate說明一下,然後處理WM_DROPFILES就可以了,以下是簡單的例子。
Type
tFiles = array of string;
TForm1 = class(TForm)
...
private
procedure OnAcceptfiles(Sender: tobject; var Files: tFiles);
procedure AcceptFiles(var msg : TMessage); message WM_DROPFILES;
...
end;
...
implementation
uses
ShellAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
end;
procedure TPoemForm.AcceptFiles(var msg: TMessage);
const
cnMaxFileNameLen = 255;
var
i, n: integer;
acFileName: array [0..cnMaxFileNameLen] of char;
Files: tFiles;
begin
n:=DragQueryFile(msg.WParam, $FFFFFFFF, acFileName, cnMaxFileNameLen);
Setlength(Files, n);
try
for i:=0 to n-1 do
begin
DragQueryFile(msg.WParam, i, acFileName, cnMaxFileNameLen);
Files[i]:=acFileName;
end;
finally
DragFinish(msg.WParam);
end;
if n>0 then
OnAcceptfiles(Self, Files);
end;
procedure TPoemForm.OnAcceptfiles(Sender: tobject; var Files: tFiles);
begin
end;
....
end.
上面例子中整個Form都可接受拖放文件,如果想只要Form的一部分接受文件,改變DragAcceptFiles所用的Handle即可,也可以多次使用DragAcceptFiles,使得多個控制元件接受拖放。
使用GetDeviceCaps可以求得各種設備的大小,以下把它做成了OBJECT,方便使用:
interface
uses
windows,Classes;
type
TDeviceCaps=class
private
fHandle: tHandle;
function GetCapabilities(i: integer): integer;
public
constructor Create(h: thandle);
property Handle: tHandle read fHandle write fHandle;
property Capabilities[i: integer]: integer read GetCapabilities;
property Driver_Version: integer index windows.DRIVERVERSION read GetCapabilities;
property Technology: integer index windows.TECHNOLOGY read GetCapabilities;
property X_SizeMM: integer index windows.HORZSIZE read GetCapabilities;
property Y_SizeMM: integer index windows.VERTSIZE read GetCapabilities;
property X_Size: integer index windows.HORZRES read GetCapabilities;
property Y_Size: integer index windows.VERTRES read GetCapabilities;
property X_LogPixel: integer index windows.LOGPIXELSX read GetCapabilities;
property Y_LogPixel: integer index windows.LOGPIXELSY read GetCapabilities;
property X_PhysicalSize: integer index windows.PHYSICALWIDTH read GetCapabilities;
property Y_PhysicalSize: integer index windows.PHYSICALHEIGHT read GetCapabilities;
property X_PhysicalOffset: integer index windows.PHYSICALOFFSETX read GetCapabilities;
property Y_PhysicalOffset: integer index windows.PHYSICALOFFSETY read GetCapabilities;
end;
implementation
constructor TDeviceCaps.Create(h: thandle);
begin
fHandle:=h;
end;
function TDeviceCaps.GetCapabilities(i: integer): integer;
begin
result:=GetDeviceCaps(fHandle,i);
end;
使用方法如下:
var
PrinterDev: tDeviceCaps;
....
PrinterDev:=tDeviceCaps.Create(printer.handle);
Xdpi :=PrinterDev.X_LogPixel;
....
想知道Taskbar是否可見,或者想隱藏它,可以用以下Windows API:
function IsTaskBarVisible: boolean;
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle := FindWindow(@wndClass[0], nil);
result := ShowWindow(wndHandle, SW_SHOWNA);
end;
procedure HideTaskBar;
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle := FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_HIDE);
end;
procedure ShowTaskBar;
var wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle := FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_RESTORE);
end;