如何在Delphi程序中实现格式化软盘?越详细越好。

heziqq 2003-04-21 11:14:32
在网上查了很久,也没找到,请哪位大虾赐教。
...全文
22 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
heziqq 2003-04-22
  • 打赏
  • 举报
回复
谢谢sysu大虾不厌其烦的解答,您的方案完全正确。
zhoutian618 2003-04-21
  • 打赏
  • 举报
回复
WinExec('Format A:',SW_NORMAL);
sysu 2003-04-21
  • 打赏
  • 举报
回复
{From 超级猛料}

格式化磁盘

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'

procedure TForm1.btnFormatDiskClick(Sender: TObject);

var

retCode: LongInt;

begin

retCode:=SHFormatDrive(Handle, 0, SHFMT_ID_DEFAULT,SHFMT_OPT_QUICKFORMAT);

if retCode < 0 then

ShowMessage('Could not format drive');

end;

end.

也可以用下面的方法调用:

procedure TForm1.Button1Click(Sender: TObject);

begin

ShellExecute(Application.Handle,

PChar('Open'),

PChar('C:.exe'),

PChar('Shell32.dll,SHFormatDrive'),

PChar('C:'),

SW_SHOWNORMAL);

end;
DJ_KK 2003-04-21
  • 打赏
  • 举报
回复
up
sysu 2003-04-21
  • 打赏
  • 举报
回复
下面程序在98下测试通过,会弹出格式化软盘的对话框。

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

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

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;

end.
heziqq 2003-04-21
  • 打赏
  • 举报
回复
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;

结果编译不通,sysu大虾能指点一下吗?

easyboot 2003-04-21
  • 打赏
  • 举报
回复
shellexecute(0,'open','format a:',nil,nil,SW_SHOWNORMAL)
sysu 2003-04-21
  • 打赏
  • 举报
回复
在ueses里加上 ShellApi;
heziqq 2003-04-21
  • 打赏
  • 举报
回复
我从没使用过ShellExecute函数,不知该如何引用,能说详细点吗

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧