程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi編程技巧集

Delphi編程技巧集

編輯:Delphi
◇[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:[email protected]'','''','''',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 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_MButton)<>0 then ... //中鍵
if GetAsyncKeyState(VK_RButton)<>0 then ... //右鍵

◇[Delphi]設置窗體的最大顯示
onFormCreate事件
self.width:=screen.width;
self.height:=screen.height;

◇[Delphi]按鍵接受消息
OnCreate事件中處理:Application.OnMessage:=MyOnMessage;
procedure TForm1.MyOnMessage(var MSG:TMSG;var Handle:Boolean);
begin
if msg.message=256 then ... //ANY鍵
if msg.message=112 then ... //F1
if msg.message=113 then ... //F2
end;

◇[雜類]隱藏共享文件夾
共享效果:可訪問,但不可見(在資源管理、網絡鄰居中)
取共享名為:direction$
訪問://computer/dirction/

◇[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…[<+|-><E|e><+|->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;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved