您的位置:首页 > 编程语言 > Delphi

Delphi编程技巧集锦

2012-02-01 02:18 330 查看
======================================================

注:本文源代码点此下载

======================================================

◇[delphi]网络邻居复制文件
uses shellapi;
copyfile(pchar('newfile.txt'),pchar('//computername/direction/targer.txt'),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事件:label.left:=xpanel-xlabel;label.top:=ypanel-ylabel;
◇[delphi]取得windows目录
uses shellapi;
var windir:array[0..255] of char;
getwindowsdirectory(windir,sizeof(windir));
或者从注册表中读取,位置:
hkey_local_machine\software\microsoft\windows\currentversion
systemroot键,取得如:c:\windows
◇[delphi]在form或其他容器上画线
var x,y:array [0..50] of integer;
canvas.pen.color:=clred;
canvas.pen.style:=psdash;
form1.canvas.moveto(trunc(x[i]),trunc(y[i]));
form1.canvas.lineto(trunc(x[j]),trunc(y[j]));
◇[delphi]字符串列表使用
var tips:tstringlist;
tips:=tstringlist.create;
tips.loadfromfile('filename.txt');
edit1.text:=tips[0];
tips.add('last line addition string');
tips.insert(1,'insert string at no 2 line');
tips.savetofile('newfile.txt');
tips.free;
◇[delphi]简单的剪贴板操作
richedit1.selectall;
richedit1.copytoclipboard;
richedit1.cuttoclipboard;
edit1.pastefromclipboard;
◇[delphi]关于文件、目录操作
chdir('c:\abcdir');转到目录
mkdir('dirname');建立目录
rmdir('dirname');删除目录
getcurrentdir;//取当前目录名,无'\'
getdir(0,s);//取工作目录名s:='c:\abcdir';
deletfile('abc.txt');//删除文件
renamefile('old.txt','new.txt');//文件更名
extractfilename(filelistbox1.filename);//取文件名
extractfileext(filelistbox1.filename);//取文件后缀
◇[delphi]处理文件属性
attr:=filegetattr(filelistbox1.filename);
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('command.com /c copy *.* c:\',sw_normal);
winexec('start abc.txt');
shellexecute或shellexecuteex//启动文件关联程序
function executefile(const filename,params,defaultdir:string;showcmd:integer):thandle;
executefile('c:\abc\a.txt','x.abc','c:\abc\',0);
executefile('http://tingweb.yeah.net','','',0);
executefile('mailto:tingweb@wx88.net','','',0);
◇[delphi]取得系统运行的进程名
var hcurrentwindow:hwnd;sztext:array[0..254] of char;
begin
hcurrentwindow:=getwindow(handle,gw_hwndfrist);
while hcurrentwindow 0 then listbox1.items.add(strpas(@sztext));
hcurrentwindow:=getwindow(hcurrentwindow,gw_hwndnext);
end;
end;
◇[delphi]关于汇编的嵌入
asm end;
可以任意修改eax、ecx、edx;不能修改esi、edi、esp、ebp、ebx。
◇[delphi]关于类型转换函数
floattostr//浮点转字符串
floattostrf//带格式的浮点转字符串
inttohex//整数转16进制
timetostr
datetostr
datetimetostr
fmtstr//按指定格式输出字符串
formatdatetime('yyyy-mm-dd,hh-mm-ss',date);
◇[delphi]字符串的过程和函数
insert(obj,target,pos);//字符串target插入在pos的位置。如插入结果大于target最大长度,多出字符将被截掉。如pos在255以外,会产生运行错。例如,st:='brian',则insert('ok',st,2)会使st变为'brokian'。
delete(st,pos,num);//从st串中的pos(整型)位置开始删去个数为num(整型)个字符的子字串。例如,st:='brian',则delete(st,3,2)将变为brn。
str(value,st);//将数值value(整型或实型)转换成字符串放在st中。例如,a=2.5e4时,则str(a:10,st)将使st的值为' 25000'。
val(st,var,code);//把字符串表达式st转换为对应整型或实型数值,存放在var中。st必须是一个表示数值的字符串,并符合数值常数的规则。在转换过程中,如果没有检测出错误,变量code置为0,否则置为第一个出错字符的位置。例如,st:=25.4e3,x是一个实型变量,则val(st,x,code)将使x值为25400,code值为0。
copy(st.pos.num);//返回st串中一个位置pos(整型)处开始的,含有num(整型)个字符的子串。如果pos大于st字符串的长度,那就会返回一个空串,如果pos在255以外,会引起运行错误。例如,st:='brian',则copy(st,2,2)返回'ri'。
concat(st1,st2,st3……,stn);//把所有自变量表示出的字符串按所给出的顺序连接起来,并返回连接后的值。如果结果的长度255,将产生运行错误。例如,st1:='brian',st2:=' ',st3:='wilfred',则concat(st1,st2,st3)返回'brian wilfred'。
length(st);//返回字符串表达式st的长度。例如,st:='brian',则length(st)返回值为5。
pos(obj,target);//返回字符串obj在目标字符串target的第一次出现的位置,如果target没有匹配的串,pos函数的返回值为0。例如,target:='brian wilfred',则pos('wil',target)的返回值是7,pos('hurbet',target)的返回值是0。
◇[delphi]关于处理注册表
uses registry;
var reg:tregistry;
reg:=tregistry.create;
reg.rootkey:='hkey_current_user';
reg.openkey('control panel\desktop',false);
reg.writestring('title wallpaper','0');
reg.writestring('wallpaper',filelistbox1.filename);
reg.closereg;
reg.free;
◇[delphi]关于键盘常量名
vk_back/vk_tab/vk_return/vk_shift/vk_control/vk_menu/vk_pause/vk_escape
/vk_space/vk_left/vk_right/vk_up/vk_down
f1--f12:$70(112)--$7b(123)
a-z:$41(65)--$5a(90)
0-9:$30(48)--$39(57)
◇[delphi]初步判断程序母语
delphi软件的dos提示:this program must be run under win32.
vc++软件的dos提示:this program cannot be run in dos mode.
◇[delphi]操作cookie
response.cookies("name").domain:='http://www.086net.com';
with response.cookies.add do
begin
name:='username';
value:='username';
end
◇[delphi]增加到文档菜单连接
uses shellapi,shlobj;
shaddtorecentdocs(shard_path,pchar(filepath));//增加连接
shaddtorecentdocs(shard_path,nil);//清空
◇[杂类]备份智能abc输入法词库
windows\system\user.rem
windows\system\tmmr.rem
◇[delphi]判断鼠标按键
if getasynckeystate(vk_lbutton)0 then ... //中键
if getasynckeystate(vk_rbutton)关闭
定时转url
设为首页
设为首页
收藏本站
收藏本站
加入频道
加入频道
◇[delphi]文本编辑相关
checkbox1.checked:=not checkbox1.checked;
if checkbox1.checked then richedit1.font.style:=richedit1.font.style+[fsbold] else richedit1.font.style:=richedit1.font.style-[fsbold]//粗体
if checkbox1.checked then richedit1.font.style:=richedit1.font.style+[fsitalic] else richedit1.font.style:=richedit1.font.style-[fsitalic]//斜体
if checkbox1.checked then richedit1.font.style:=richedit1.font.style+[fsunderline] else richedit1.font.style:=richedit1.font.style-[fsunderline]//下划线
memo1.alignment:=taleftjustify;//居左
memo1.alignment:=tarightjustify;//居右
memo1.alignment:=tacenter;//居中
◇[delphi]随机产生文本色
randomize;//随机种子
memo1.font.color:=rgb(random(255),random(255),random(255));
◇[delphi]delphi5 update升级补丁序列号
1000003185
90x25fx0
◇[delphi]文件名的非法字符过滤
for i:=1 to length(s) do
if s[i] in ['\','/',':','*','?','','|'] then
◇[delphi]转换函数的定义及说明
datetimetofiledate (datetime:tdatetime):longint; 将tdatetime格式的日期时间值转换成dos格式的日期时间值
datetimetostr (datetime:tdatetime):string; 将tdatatime格式变量转换成字符串,如果datetime参数不包含日期值,返回字符串日期显示成为00/00/00,如果datetime参数中没有时间值,返回字符串中的时间部分显示成为00:00:00 am
datetimetostring (var result string;
const format:string;
datetime:tdatetime); 根据给定的格式字符串转换时间和日期值,result为结果字符串,format为转换格式字符串,datetime为日期时间值
datetostr (date:tdatetime) 使用shortdateformat全局变量定义的格式字符串将date参数转换成对应的字符串
floattodecimal (var result:tfloatrec;value:
extended;precision,decimals:
integer); 将浮点数转换成十进制表示
floattostr (value:extended):string 将浮点数value转换成字符串格式,该转换使用普通数字格式,转换的有效位数为15位。
floattotext (buffer:pchar;value:extended;
format:tfloatformat;precision,
digits:integer):integer; 用给定的格式、精度和小数将浮点值value转换成十进制表示形式,转换结果存放于buffer参数中,函数返回值为存储到buffer中的字符位数,buffer是非0结果的字符串缓冲区。
floattotextfmt (buffer:pchar;value:extended;
format:pchar):integer 用给定的格式将浮点值value转换成十进制表示形式,转换结果存放于buffer参数中,函数返回值为存储到buffer中的字符位数。
inttohex (value:longint;digits:integer):
string; 将给定的数值value转换成十六进制的字符串。参数digits给出转换结果字符串包含的数字位数。
inttostr (value:longint):string 将整数转换成十进制形式字符串
strtodate (const s:string):tdatetime 将字符串转换成日期值,s必须包含一个合法的格式日期的字符串。
strtodatetime (const s:string):tdatetime 将字符串s转换成日期时间格式,s必须具有mm/dd/yy hh:mm:ss[am|pm]格式,其中日期和时间分隔符与系统时期时间常量设置相关。如果没有指定am或pm信息,表示使用24小时制。
strtofloat (const s:string):extended; 将给定的字符串转换成浮点数,字符串具有如下格式:
[+|-]nnn…[.]nnn…[nnnn]
strtoint (const s:string):longint 将数字字符串转换成整数,字符串可以是十进制或十六进制格式,如果字符串不是一个合法的数字字符串,系统发生econverterror异常
strtointdef (const s:string;default:
longint):longint; 将字符串s转换成数字,如果不能将s转换成数字,strtointdef函数返回参数default的值。
strtotime (const s:string):tdatetime 将字符串s转换成tdatetime值,s具有hh:mm:ss[am|pm]格式,实际的格式与系统的时间相关的全局变量有关。
timetostr (time:tdatetime):string; 将参数time转换成字符串。转换结果字符串的格式与系统的时间相关常量的设置有关。
◇[delphi]程序不出现在alt+ctrl+del
在implementation后添加声明:
function registerserviceprocess(dwprocessid, dwtype: integer): integer; stdcall; external 'kernel32.dll';
registerserviceprocess(getcurrentprocessid, 1);//隐藏
registerserviceprocess(getcurrentprocessid, 0);//显示
用alt+del+ctrl看不见
◇[delphi]程序不出现在任务栏
uses windows
var
extendedstyle : integer;
begin
application.initialize;

======================================================

在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: