2024年3月21日发(作者:)

Delphi常用代码汇总

◇[DELPHI]网络邻居复制文件

uses shellapi;

copyfile(pchar(''),pchar('//computername/direction/'),false);

◇[DELPHI]产生鼠标拖动效果

通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL:

var xpanel,ypanel,xlabel,ylabel:integer;

PANEL的MouseMove事件:xpanel:=x;ypanel:=y;

PANEL的DragOver事件:xpanel:=x;ypanel:=y;

LABEL的MouseMove事件:xlabel:=x;ylabel:=y;

LABEL的EndDrag事件::=xpanel-xlabel;:=ypanel-ylabel;

◇[DELPHI]取得WINDOWS目录

uses shellapi;

var windir:array[0..255] of char;

getwindowsdirectory(windir,sizeof(windir));

或者从注册表中读取,位置:

HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersion

SystemRoot键,取得如:C:WINDOWS

◇[DELPHI]在form或其他容器上画线

var x,y:array [0..50] of integer;

:=clred;

:=psDash;

(trunc(x[i]),trunc(y[i]));

(trunc(x[j]),trunc(y[j]));

◇[DELPHI]字符串列表使用

var tips:tstringlist;

tips:=;

omfile('');

:=tips[0];

('last line addition string');

(1,'insert string at NO 2 line');

file('');

;

◇[DELPHI]简单的剪贴板操作

all;

clipboard;

lipboard;

romclipboard;

◇[DELPHI]关于文件、目录操作

Chdir('c:abcdir');转到目录

Mkdir('dirname');建立目录

Rmdir('dirname');删除目录

GetCurrentDir;//取当前目录名,无''

Getdir(0,s);//取工作目录名s:='c:abcdir';

Deletfile('');//删除文件

Renamefile('','');//文件更名

ExtractFilename(me);//取文件名

ExtractFileExt(me);//取文件后缀

◇[DELPHI]处理文件属性

attr:=filegetattr(me);

if (attr and faReadonly)=faReadonly then ... //只读

if (attr and faSysfile)=faSysfile then ... //系统

if (attr and faArchive)=faArchive then ... //存档

if (attr and faHidden)=faHidden then ... //隐藏

◇[DELPHI]执行程序外文件

WINEXEC//调用可执行文件

winexec(' /c copy *.* c:',SW_Normal);

winexec('start ');

ShellExecute或ShellExecuteEx//启动文件关联程序

function executefile(const filename,params,defaultDir:string;showCmd:integer):THandle;

ExecuteFile('C:','','c:abc',0);

ExecuteFile('','','',0);

ExecuteFile('mailto:tingweb@','','',0);

◇[DELPHI]取得系统运行的进程名

var hCurrentWindow:HWnd;szText:array[0..254] of char;

begin

hCurrentWindow:=Getwindow(handle,GW_HWndFrist);

while hCurrentWindow <> 0 do

begin

if Getwindowtext(hcurrnetwindow,@sztext,255)>0 then

(strpas(@sztext));

hCurrentWindow:=Getwindow(hCurrentwindow,GW_HWndNext);

end;

end;

◇[DELPHI]关于汇编的嵌入

Asm End;

可以任意修改EAX、ECX、EDX;不能修改ESI、EDI、ESP、EBP、EBX。