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

我的學習生涯(Delphi篇) - 01

編輯:Delphi

    Delphi中包裝了幾乎所有的WIN32 API。

    這些API至今還活躍在Windows平台!


    比如下文出現的 BlockRead/ BlockWrite就是調用IOPro函數。

 
 

 

    效果如下圖:

 

 

 

 Unit1.pas

 

[delphi]
unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ShellApi, StdCtrls; 
 
type 
  TForm1 = class(TForm) 
    Button1: TButton; 
    Button2: TButton; 
    Button3: TButton; 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Label1: TLabel; 
    Label2: TLabel; 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure Button3Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
  procedure mycopyfile(Sfname,Dfname:string); 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  lphKey: HKEY; 
  sKeyName: string; 
  sKeyValue: string; 
begin 
    sKeyName := 'myfile'; 
    sKeyValue :='我的文檔'; 
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey); 
    RegSetValue(lphKey,'',REG_SZ,pchar(sKeyValue),0); 
    sKeyName := '.xbf'; 
    sKeyValue := 'myfile'; 
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey); 
    RegSetValue(lphKey,'',REG_SZ,pchar(sKeyValue),0); 
    sKeyName := 'myfile'; 
    sKeyValue := 'e:\WINNT\SYSTEM32\NotePad.exe "%1"'; 
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey); 
    RegSetValue(lphKey,'shell\open\command',REG_SZ,pchar(sKeyValue),MAX_PATH); 
 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  //ShellExecute(Application.Handle,'open','NotePad.exe','',nil,SW_SHOWNORMAL);  
 
  //WinExec('command.com',SW_SHOWNORMAL);  
end; 
 
 
 
procedure mycopyfile(Sfname,Dfname:string); 
Var 
  Sourcef,Destinef:file; 
  NumRead,NumWritten:Integer; 
  Buf:array[1..4096] of char;//定義緩沖區;  
Begin 
  //Dfname:='c:\xiaobin.xbf';  
  AssignFile(Sourcef,Sfname); 
  Reset(Sourcef,1); 
  AssignFile(Destinef,Dfname); 
  Rewrite(Destinef,1); 
  Repeat 
    BlockRead(Sourcef,Buf,SizeOf(Buf),Numread);//讀源文件  
    BlockWrite(destinef,buf,NumRead,NumWritten);//寫目標文件;  
  Until (Numread=0) or (Numwritten<>numread); 
 
    //BlockRead(Sourcef,buf,SIZEOF(buf),NumRead);  
    //BlockWrite(Destinef,buf,NumRead,NumWritten);  
    closeFile(Sourcef); 
    Closefile(destinef); 
 
end; 
 
 
procedure TForm1.Button3Click(Sender: TObject); 
begin 
  if  FileExists(Edit2.Text) then 
  begin 
    if Application.MessageBox('文件已經存在!是否覆蓋?','提示',MB_YESNO)=IDYES then 
    begin 
      mycopyfile(Edit1.Text,Edit2.Text); 
      //CopyFile('c:\xiaobin.xbf','d:\xiaobin.xbf',True);  
      Application.MessageBox('文件復制完成!','消息',32); 
    end; 
  end 
  else 
  begin 
    mycopyfile(Edit1.Text,Edit2.Text); 
    //CopyFile('c:\xiaobin.xbf','d:\xiaobin.xbf',True);  
    Application.MessageBox('文件復制完成!','消息',32); 
  end; 
   
end; 
 
end. 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellApi, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  procedure mycopyfile(Sfname,Dfname:string);

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  lphKey: HKEY;
  sKeyName: string;
  sKeyValue: string;
begin
    sKeyName := 'myfile';
    sKeyValue :='我的文檔';
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey);
    RegSetValue(lphKey,'',REG_SZ,pchar(sKeyValue),0);
    sKeyName := '.xbf';
    sKeyValue := 'myfile';
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey);
    RegSetValue(lphKey,'',REG_SZ,pchar(sKeyValue),0);
    sKeyName := 'myfile';
    sKeyValue := 'e:\WINNT\SYSTEM32\NotePad.exe "%1"';
    RegCreateKey(HKEY_CLASSES_ROOT,pchar(sKeyName),lphKey);
    RegSetValue(lphKey,'shell\open\command',REG_SZ,pchar(sKeyValue),MAX_PATH);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //ShellExecute(Application.Handle,'open','NotePad.exe','',nil,SW_SHOWNORMAL);

  //WinExec('command.com',SW_SHOWNORMAL);
end;

 

procedure mycopyfile(Sfname,Dfname:string);
Var
  Sourcef,Destinef:file;
  NumRead,NumWritten:Integer;
  Buf:array[1..4096] of char;//定義緩沖區;
Begin
  //Dfname:='c:\xiaobin.xbf';
  AssignFile(Sourcef,Sfname);
  Reset(Sourcef,1);
  AssignFile(Destinef,Dfname);
  Rewrite(Destinef,1);
  Repeat
    BlockRead(Sourcef,Buf,SizeOf(Buf),Numread);//讀源文件
    BlockWrite(destinef,buf,NumRead,NumWritten);//寫目標文件;
  Until (Numread=0) or (Numwritten<>numread);

    //BlockRead(Sourcef,buf,SIZEOF(buf),NumRead);
    //BlockWrite(Destinef,buf,NumRead,NumWritten);
    closeFile(Sourcef);
    Closefile(destinef);

end;


procedure TForm1.Button3Click(Sender: TObject);
begin
  if  FileExists(Edit2.Text) then
  begin
    if Application.MessageBox('文件已經存在!是否覆蓋?','提示',MB_YESNO)=IDYES then
    begin
      mycopyfile(Edit1.Text,Edit2.Text);
      //CopyFile('c:\xiaobin.xbf','d:\xiaobin.xbf',True);
      Application.MessageBox('文件復制完成!','消息',32);
    end;
  end
  else
  begin
    mycopyfile(Edit1.Text,Edit2.Text);
    //CopyFile('c:\xiaobin.xbf','d:\xiaobin.xbf',True);
    Application.MessageBox('文件復制完成!','消息',32);
  end;
 
end;

end.

 

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