歡迎光臨 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;
要知道一個檔案文件是否曾被修改,一個方便的方法是看看它的最後修改日期,Delphi提供了兩個和文件日期有關的函數:FileAge和FileGetDate,但是它們返回的都是文件建立的時間而不是最後修改的時間。
Windows中每一個文件有三個時間屬性,建立時間、最後打開時間和最後修改時間,可以用下列函數取得:
function TFileTime2DateTime(Time: TFileTime): tDatetime;
var
LocalTime: TFileTime;
DosTime: Integer;
begin
FileTimeToLocalFileTime(Time, LocalTime);
FileTimeToDosDateTime(LocalTime, LongRec(DosTime).Hi, LongRec(DosTime).Lo);
Result:=FileDateToDateTime(DosTime);
end;
function DateTime2TFileTime(Time: tDatetime): TFileTime;
var
LocalTime: TFileTime;
DosTime: Integer;
begin
DosTime:=DateTimeToFileDate(Time);
DosDateTimeToFileTime(LongRec(DosTime).Hi, LongRec(DosTime).Lo, LocalTime);
LocalFileTimeToFileTime(LocalTime, Result);
end;
function GetFileCreationTime(const filename:string): tdatetime;
var
hFile: THandle;
FileTime: TFileTime;
begin
hFile:=FileOpen(filename, fmOpenRead);
try
GetFileTime(hFile, @FileTime, nil, nil);
Result:=TFileTime2DateTime(FileTime);
finally
FileClose(hFile);
end;
end;
function GetFileLastAccessTime(const filename:string): tdatetime;
var
hFile: THandle;
FileTime: TFileTime;
begin
hFile:=FileOpen(filename, fmOpenRead);
try
GetFileTime(hFile, nil, @FileTime, nil);
Result:=TFileTime2DateTime(FileTime);
finally
FileClose(hFile);
end;
end;
function GetFileLastWriteTime(const filename:string): tdatetime;
var
hFile: THandle;
FileTime: TFileTime;
begin
hFile:=FileOpen(filename, fmOpenRead);
try
GetFileTime(hFile, nil, nil, @FileTime);
Result:=TFileTime2DateTime(FileTime);
finally
FileClose(hFile);
end;
end;
在Delphi中把文字复制到剪貼板是很容易的,只要Uses clipbrd;後設定Clipboard.AsText即可,但是如果文字中使用了非ASCII字元,在不同語言的系統中將出現亂碼,解決的方法是复制時說明文字的編碼:
procedure CopyToClipboard(const s: string; LocaleID: LCID = $0404);
var
MemHandle: HGLOBAL;
P: ^LCID;
begin
if s='' then exit;
with Clipboard do
begin
MemHandle:=GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, sizeof(LCID));
if MemHandle <> 0 then
begin
p:=GlobalLock(MemHandle);
p^:=LocaleID;
GlobalUnlock(MemHandle);
Open;
try
AsText := s;
SetAsHandle(CF_Locale, MemHandle);
finally
Close;
end;
end else astext:=s;
end;
end;
當然,也可以使用Unicode:
procedure CopyToClipBoardw(const ws: widestring);
var
MemHandle: HGLOBAL;
ptr: ^LCID;
begin
MemHandle := GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, 2*length(ws)+2);
if MemHandle <> 0 then
begin
ptr:=GlobalLock(MemHandle);
Move(PWideChar(ws)^, ptr^, 2*length(ws)+2);
GlobalUnlock(MemHandle);
Clipboard.SetAsHandle(CF_UNICODETEXT, MemHandle);
end;
end;