Delphi编程技巧集锦.docx
《Delphi编程技巧集锦.docx》由会员分享,可在线阅读,更多相关《Delphi编程技巧集锦.docx(47页珍藏版)》请在冰豆网上搜索。
Delphi编程技巧集锦
Delphi编程技巧集锦
四川省攀枝花市第三高级中学廖裕编辑整理
二○○四年十一月二十五日
◇[DELPHI]网络邻居复制文件
usesshellapi;
copyfile(pchar('newfile.txt'),pchar('//computername/direction/targer.txt'),false);
◇[DELPHI]产生鼠标拖动效果
通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL:
varxpanel,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目录
usesshellapi;
varwindir:
array[0..255]ofchar;
getwindowsdirectory(windir,sizeof(windir));
或者从注册表中读取,位置:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
SystemRoot键,取得如:
C:
\WINDOWS
◇[DELPHI]在FORM或其他容器上画线
varx,y:
array[0..50]ofinteger;
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]字符串列表使用
vartips:
tstringlist;
tips:
=tstringlist.create;
tips.loadfromfile('filename.txt');
edit1.text:
=tips[0];
tips.add('lastlineadditionstring');
tips.insert(1,'insertstringatNO2line');
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(attrandfaReadonly)=faReadonlythen...//只读
if(attrandfaSysfile)=faSysfilethen...//系统
if(attrandfaArchive)=faArchivethen...//存档
if(attrandfaHidden)=faHiddenthen...//隐藏
◇[DELPHI]执行程序外文件
WINEXEC//调用可执行文件
winexec('/ccopy*.*c:
\',SW_Normal);
winexec('startabc.txt');
ShellExecute或ShellExecuteEx//启动文件关联程序
functionexecutefile(constfilename,params,defaultDir:
string;showCmd:
integer):
THandle;
ExecuteFile('C:
\abc\a.txt','x.abc','c:
\abc\',0);
ExecuteFile('','','',0);
ExecuteFile('mailto:
tingweb@','','',0);
◇[DELPHI]取得系统运行的进程名
varhCurrentWindow:
HWnd;szText:
array[0..254]ofchar;
begin
hCurrentWindow:
=Getwindow(handle,GW_HWndFrist);
whilehCurrentWindow<>0do
begin
ifGetwindowtext(hcurrnetwindow,@sztext,255)>0thenlistbox1.items.add(strpas(@sztext));
hCurrentWindow:
=Getwindow(hCurrentwindow,GW_HWndNext);
end;
end;
◇[DELPHI]关于汇编的嵌入
AsmEnd;
可以任意修改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)返回'BrianWilfred'。
Length(st);//返回字符串表达式st的长度。
例如,st:
='Brian',则Length(st)返回值为5。
Pos(obj,target);//返回字符串obj在目标字符串target的第一次出现的位置,如果target没有匹配的串,Pos函数的返回值为0。
例如,target:
='BrianWilfred',则Pos('Wil',target)的返回值是7,Pos('hurbet',target)的返回值是0。
◇[DELPHI]关于处理注册表
usesRegistry;
varreg:
Tregistry;
reg:
=Tregistry.create;
reg.rootkey:
='HKey_Current_User';
reg.openkey('ControlPanel\Desktop',false);
reg.WriteString('TitleWallpaper','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提示:
ThisProgramMustBeRunUnderWin32.
VC++软件的DOS提示:
ThisProgramCannotBeRunInDOSMode.
◇[DELPHI]操作Cookie
response.cookies("name").domain:
='';
withresponse.cookies.adddo
begin
name:
='username';
value:
='username';
end
◇[DELPHI]增加到文档菜单连接
usesshellapi,shlOBJ;
shAddToRecentDocs(shArd_path,pchar(filepath));//增加连接
shAddToRecentDocs(shArd_path,nil);//清空
◇[杂类]备份智能ABC输入法词库
windows\system\user.rem
windows\system\tmmr.rem
◇[DELPHI]判断鼠标按键
ifGetAsyncKeyState(VK_LButton)<>0then...//左键
ifGetAsyncKeyState(VK_MButton)<>0then...//中键
ifGetAsyncKeyState(VK_RButton)<>0then...//右键
◇[DELPHI]设置窗体的最大显示
onFormCreate事件
self.width:
=screen.width;
self.height:
=screen.height;
◇[DELPHI]按键接受消息
OnCreate事件中处理:
Application.OnMessage:
=MyOnMessage;
procedureTForm1.MyOnMessage(varMSG:
TMSG;varHandle:
Boolean);
begin
ifmsg.message=256then...//ANY键
ifmsg.message=112then...//F1
ifmsg.message=113then...//F2
end;
◇[杂类]隐藏共享文件夹
共享效果:
可访问,但不可见(在资源管理、网络邻居中)
取共享名为:
direction$
访问:
//computer/dirction/
◇[JavaScript]JavaScript网页常用效果
网页60秒定时关闭
--
settimeout('window.close();',60000)
-->
关闭窗口
window.close();returnfalse;">关闭
定时转URL
设为首页
设为首页
收藏本站
window.external.addfavorite('','[未名码头]')">收藏本站
加入频道
window.external.addchannel('')">加入频道
◇[DELPHI]文本编辑相关
checkbox1.checked:
=notcheckbox1.checked;
ifcheckbox1.checkedthenrichedit1.font.style:
=richedit1.font.style+[fsBold]elserichedit1.font.style:
=richedit1.font.style-[fsBold]//粗体
ifcheckbox1.checkedthenrichedit1.font.style:
=richedit1.font.style+[fsItalic]elserichedit1.font.style:
=richedit1.font.style-[fsItalic]//斜体
ifcheckbox1.checkedthenrichedit1.font.style:
=richedit1.font.style+[fsUnderline]elserichedit1.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]DELPHI5UPDATE升级补丁序列号
1000003185
90X25fx0
◇[DELPHI]文件名的非法字符过滤
fori:
=1tolength(s)do
ifs[i]in['\','/',':
','*','?
','<','>','|']then
◇[DELPHI]转换函数的定义及说明
datetimetofiledate(datetime:
Tdatetime):
longint;将Tdatetime格式的日期时间值转换成DOS格式的日期时间值
datetimetostr(datetime:
Tdatetime):
string;将Tdatatime格式变量转换成字符串,如果datetime参数不包含日期值,返回字符串日期显示成为00/00/00,如果datetime参数中没有时间值,返回字符串中的时间部分显示成为00:
00:
00AM
datetimetostring(varresultstring;
constformat:
string;
datetime:
Tdatetime);根据给定的格式字符串转换时间和日期值,result为结果字符串,format为转换格式字符串,datetime为日期时间值
datetostr(date:
Tdatetime)使用shortdateformat全局变量定义的格式字符串将date参数转换成对应的字符串
floattodecimal(varresult:
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(constS:
string):
Tdatetime将字符串转换成日期值,S必须包含一个合法的格式日期的字符串。
strtodatetime(constS:
string):
Tdatetime将字符串S转换成日期时间格式,S必须具有MM/DD/YYHH:
MM:
SS[AM|PM]格式,其中日期和时间分隔符与系统时期时间常量设置相关。
如果没有指定AM或PM信息,表示使用24小时制。
strtofloat(constS:
string):
extended;将给定的字符串转换成浮点数,字符串具有如下格式:
[+|-]nnn…[.]nnn…[<+|-><+|->nnnn]
strtoint(constS:
string):
longint将数字字符串转换成整数,字符串可以是十进制或十六进制格式,如果字符串不是一个合法的数字字符串,系统发生ECONVERTERROR异常
strtointdef(constS:
string;default:
longint):
longint;将字符串S转换成数字,如果不能将S转换成数字,strtointdef函数返回参数default的值。
strtotime(constS:
string):
Tdatetime将字符串S转换成TDATETIME值,S具有HH:
MM:
SS[AM|PM]格式,实际的格式与系统的时间相关的全局变量有关。
timetostr(time:
Tdatetime):
string;将参数TIME转换成字符串。
转换结果字符串的格式与系统的时间相关常量的设置有关。
◇[DELPHI]程序不出现在ALT+CTRL+DEL
在implementation后添加声明:
functionRegisterServiceProcess(dwProcessID,dwType:
Integer):
Integer;stdcall;external'KERNEL32.DLL';
RegisterServiceProcess(GetCurrentProcessID,1);//隐藏
RegisterServiceProcess(GetCurrentProcessID,0);//显示
用ALT+DEL+CTRL看不见
◇[DELPHI]程序不出现在任务栏
useswindows
var
ExtendedStyle:
Integer;
begin
Application.Initialize;
◇Delphi中延时的程序
procedureDelay(msecs:
integer);
var
FirstTickCount:
longint;
begin
FirstTickCount:
=GetTickCount;
repeat
Application.ProcessMessages;
until((GetTickCount-FirstTickCount)>=Longint(msecs));
end;
在DELPHI中使用ADO直接访问Excel数据文件
承蒙各位大鱼大虾的支持,终于顿悟。
现在将心经写下来,让后来者提高修成正果的效率,早日成为大鱼大虾。
要用DELPHI中的ADO系列控件访问Excel数据文件,首先应该建立一些基本概念。
你需要将一个Excel数据文件想象为一个关系数据库,Excel文件中的每个工作表就对应该数据库中的一个表,而工作表中的每一列当然就是表的列了。
然后,按照下列面的秘诀进行就可以。
1.设置ADOConnection的ConnectionString
构造ConnectionString时,OLEDB的提供者要选择MicrosoftJet4.0OLEDBProvider作为ADO的驱动程序。
这本来是用于连接Access数据库的驱动程序,但也可打开Excel文件。
连接的数据库名称当然就是你要打开的Excel文件,注意扩展名是*.xls,而不是*.mdb。
最关键的一点是,还要设置扩展属性ExtendedProperties为“Excel8.0”,否则,测试连接时会报告无