⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 在delphi中实现windows核心编程.原书光盘代码核心编程.原书光盘代码
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;
  
const
   FILE_DELETE=1;
   FILE_RENAME=2;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    RadioGroup1: TRadioGroup;
    Edit1: TEdit;
    Edit2: TEdit;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
var
  WindowsDirs: array [0..MAX_PATH + 1] of Char;
  lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
  VerPlatForm: TOSVersionInfoA;
  StrLstDelte: TStrings;
  filename,s  :String;
  i:integer;
begin
  Result := FALSE;
  ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
  VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
  GetVersionEx(VerPlatForm);
  if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
  begin
     SetLastError(ERROR_NOT_SUPPORTED);
     Exit;
  end
  else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
  begin
     if flag=FILE_DELETE then
        Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
          MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
     else if (flag=FILE_RENAME) then
        Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
          MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
  end
  else begin
     StrLstDelte := TStringList.Create;
     GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
     filename:=WindowsDirs;
     if filename[length(filename)]<>'\' then filename:=filename+'\';
     filename:=filename+'wininit.ini';
     if FileExists(filename) then
        StrLstDelte.LoadFromFile(filename);
     if StrLstDelte.IndexOf('[rename]') = -1 then
        StrLstDelte.Add('[rename]');
     GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
     if fileexists(lpFileNameToDes) then
        GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
     else begin
        s:=extractfilename(lpFileNameToDes);
        i:=pos('.',s);
        if (i=0) then
        begin
           if length(s)>8 then raise exception.create('不是有效的短文件名(8+3格式)!');
        end
        else begin
           if (i-1>8)or(length(s)-i>3) then raise exception.create('不是有效的短文件名(8+3格式)!');
        end;
        strcopy(lpDirDes,lpFileNameToDes);
     end;
     if (flag=FILE_DELETE) then {删除}
        StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL='+string(lpDirSrc))
     else if (flag=FILE_RENAME) then {改名}
        StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, string(lpDirDes)+'='+string(lpDirSrc));

     StrLstDelte.SaveToFile(filename);
     Result := TRUE;
     StrLstDelte.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   if OpenDialog1.Execute then
      edit1.text:=OpenDialog1.FileName;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   if OpenDialog1.Execute then
      edit2.text:=OpenDialog1.FileName;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   i:uint;
begin
   if RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
   else i:=FILE_RENAME;
   if edit1.text='' then raise exception.create('源文件为空!');
   if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
   if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
      showmessage('出错了')
   else showmessage('ok');
end;

procedure TForm1.Edit2Change(Sender: TObject);
var
  VerPlatForm: TOSVersionInfoA;
  buf: array [0..MAX_PATH + 1] of Char;
begin
  if not fileexists(edit2.text) then exit;
  ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
  VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
  GetVersionEx(VerPlatForm);
  if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
  begin
     GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
     edit2.text:=buf;
  end;
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
   edit2.Enabled:=RadioGroup1.ItemIndex=1;
   button2.Enabled:=RadioGroup1.ItemIndex=1;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -