There is an API hidden away in Shell32.dll called SHFormatDrive, this brings up the standard format removable drive dialog. I stumbled across this in the borland.public.delphi.winapi newsgroup.
{implementation section}
..
..
const
SHFMT_ID_DEFAULT = $FFFF;
// Formating options
SHFMT_OPT_QUICKFORMAT = $0000;
SHFMT_OPT_FULL = $0001;
SHFMT_OPT_SYSONLY = $0002;
// Error codes
SHFMT_ERROR = $FFFFFFFF;
SHFMT_CANCEL = $FFFFFFFE;
SHFMT_NOFORMAT = $FFFFFFFD;
function SHFormatDrive(Handle: HWND; Drive, ID, Options: Word): LongInt;
stdcall; external 'shell32.dll' name 'SHFormatDrive'
function SHFormatDrive(Handle: HWND; Drive, ID, Options: Word): LongInt; stdcall; external 'shell32.dll' name 'SHFormatDrive';
implementation
{$R *.dfm}
uses ShellApi;
procedure TForm1.Button1Click(Sender: TObject);
begin
if SHFormatDrive(Handle, 0, SHFMT_ID_DEFAULT,SHFMT_OPT_QUICKFORMAT) < 0 then
ShowMessage('格式化不成功!');
end;
easyboot大虾,你的方法我用了,打开一个DOS窗口格式化软盘。这不是我想要的,我想看到如同Explore中图形化格式软盘的界面。所以又试了以下方法:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
.......
private
function DiskInDrive(const Drive: char): Boolean;
function SHFormatDrive(hWnd : HWnd;
Drive, fmtID, Options : LongInt):longint;
stdcall; external 'shell32.dll' name 'SHFormatDrive'
implementation
uses Unit2,shellAPI;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DiskInDrive('a') then
begin
//格式化软盘
ShellExecute(Form1.Handle, PChar('Open'),PChar('C:.exe'),
PChar('Shell32.dll,SHFormatDrive'),PChar('C:'),SW_SHOWNORMAL);
~~~~~~~~~~~????
是否要换为A:我还真担心会将硬盘格式化了
//转换到A盘
chdir('a:');
end
else
begin
showMessage('Soft Disk doesn''t exist.');
abort;
end;
assignfile(f,'2.txt');
showMessage('ddd ' + Edit1.text);
rewrite(f);
writeln(f,'Hello' );
CloseFile(f);
end;