Delphi基礎開發技巧

來源:互聯網
上載者:User

[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 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);//清空

[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;

[DELPHI]得到映像上某一點的RGB值

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

var

red,green,blue:byte ;

i:integer;

begin

i:= image1.Canvas.Pixels[x,y];

Blue:= GetBValue(i);

Green:= GetGValue(i):

Red:= GetRValue(i);

Label1.Caption:=inttostr(Red);

Label2.Caption:=inttostr(Green);

Label3.Caption:=inttostr(Blue);

end;

[DELPHI]關於日期格式分解轉換

var year,month,day:word;now2:Tdatatime;

now2:=date();

decodedate(now2,year,month,day);

lable1.Text :=inttostr(year)+'年'+inttostr(month)+'月'+inttostr(day)+'日';

[DELPHI]如何判斷當前網路連接方式

判斷結果是MODEM、區域網路或是Proxy 伺服器方式。

uses wininet;

Function ConnectionKind :boolean;

var flags: dword;

begin

Result := InternetGetConnectedState(@flags, 0);

if Result then

begin

if (flags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then

begin

showmessage('Modem');

end;

if (flags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then

begin

showmessage('LAN');

end;

if (flags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then

begin

showmessage('Proxy');

end;

if (flags and INTERNET_CONNECTION_MODEM_BUSY)=INTERNET_CONNECTION_MODEM_BUSY then

begin

showmessage('Modem Busy');

end;

end;

end;

◇[DELPHI]如何判斷字串是否是有效EMAIL地址

function IsEMail(EMail: String): Boolean;

var s: String;ETpos: Integer;

begin

ETpos:= pos('@', EMail);

if ETpos > 1 then

begin

s:= copy(EMail,ETpos+1,Length(EMail));

if (pos('.', s) > 1) and (pos('.', s) < length(s)) then

Result:= true else Result:= false;

end

else

Result:= false;

end;

[DELPHI]判斷系統是否串連INTERNET

需要引入URL.DLL中的InetIsOffline函數。

函數申明為:

function InetIsOffline(Flag: Integer): Boolean; stdcall; external 'URL.DLL';

然後就可以調用函數判斷系統是否串連到INTERNET

if InetIsOffline(0) then ShowMessage('not connected!')

else ShowMessage('connected!');

該函數返回TRUE如果本地系統沒有串連到INTERNET。

附:

大多數裝有IE或OFFICE97的系統都有此DLL可供調用。

InetIsOffline

BOOL InetIsOffline(

DWORD dwFlags,

);

[DELPHI]簡單地播放和暫停WAV檔案

uses mmsystem;

function PlayWav(const FileName: string): Boolean;

begin

Result := PlaySound(PChar(FileName), 0, SND_ASYNC);

end;

procedure StopWav;

var

buffer: array[0..2] of char;

begin

buffer[0] := #0;

PlaySound(Buffer, 0, SND_PURGE);

end;

[DELPHI]取機器BIOS資訊

with Memo1.Lines do

begin

Add('MainBoardBiosName:'+^I+string(Pchar(Ptr($FE061))));

Add('MainBoardBiosCopyRight:'+^I+string(Pchar(Ptr($FE091))));

Add('MainBoardBiosDate:'+^I+string(Pchar(Ptr($FFFF5))));

Add('MainBoardBiosSerialNo:'+^I+string(Pchar(Ptr($FEC71))));

end;

[DELPHI]網路下載檔案

uses UrlMon;

function DownloadFile(Source, Dest: string): Boolean;

begin

try

Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;

except

Result := False;

end;

end;

if DownloadFile('http://www.borland.com/delphi6.zip, 'c:/kylix.zip') then

ShowMessage('Download succesful')

else ShowMessage('Download unsuccesful')

[DELPHI]解析伺服器IP地址

uses winsock

function IPAddrToName(IPAddr : String): String;

var

SockAddrIn: TSockAddrIn;

HostEnt: PHostEnt;

WSAData: TWSAData;

begin

WSAStartup($101, WSAData);

SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));

HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);

if HostEnt<>nil then result:=StrPas(Hostent^.h_name) else result:='';

end;

[DELPHI]取得捷徑中的串連

function ExeFromLink(const linkname: string): string;

var

FDir,

FName,

ExeName: PChar;

z: integer;

begin

ExeName:= StrAlloc(MAX_PATH);

FName:= StrAlloc(MAX_PATH);

FDir:= StrAlloc(MAX_PATH);

StrPCopy(FName, ExtractFileName(linkname));

StrPCopy(FDir, ExtractFilePath(linkname));

z:= FindExecutable(FName, FDir, ExeName);

if z > 32 then

Result:= StrPas(ExeName)

else

Result:= '';

StrDispose(FDir);

StrDispose(FName);

StrDispose(ExeName);

end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.