热情软件屋

 

如何格式化磁盘


编号:QA000975
建立日期: 1999年5月13日 最后修改日期:2001年2月15日
所属类别:

lgtest:
    vc50
    pwin98
    本人原来的程序是定期删除某一硬盘数据,但这样会造成硬盘碎片,所以求一程序快格硬盘。要求:必须自动进行不须人工操作。

回答:

    我以前在VB中做过这样的工作,应该也可以适用于VC。关键是调用SHFormatDrive。Shell32.lib中包括这个函数,但SHFormatDrive没有定义在Shellapi.h中。你可以使用下面的定义:
     #if !defined(SHFMT_OPT_FULL)
    
     #if defined (__cplusplus)
     extern "C" {
     #endif
    
     /*****************************************************************
     The SHFormatDrive API provides access to the Shell's format
     dialog box. This allows applications that want to format disks to bring
     up the same dialog box that the Shell uses for disk formatting.
    
     PARAMETERS
     hwnd = The window handle of the window that will own the
     dialog. NOTE that hwnd == NULL does not cause this
     dialog to come up as a "top level application"
     window. This parameter should always be non-null,
     this dialog box is only designed to be the child of
     another window, not a stand-alone application.
    
     drive = The 0 based (A: == 0) drive number of the drive
     to format.
    
     fmtID = Currently must be set to SHFMT_ID_DEFAULT.
    
     options = There are currently only two option bits defined.
    
     SHFMT_OPT_FULL
     SHFMT_OPT_SYSONLY
    
     SHFMT_OPT_FULL specifies that the "Quick Format"
     setting should be cleared by default. If the user
     leaves the "Quick Format" setting cleared, then a
     full format will be applied (this is useful for
     users that detect "unformatted" disks and want
     to bring up the format dialog box).
    
     If options is set to zero (0), then the "Quick Format"
     setting is set by default. In addition, if the user leaves
     it set, a quick format is performed. Under Windows NT 4.0,
     this flag is ignored and the "Quick Format" box is always
     checked when the dialog box first appears. The user can
     still change it. This is by design.
    
     The SHFMT_OPT_SYSONLY initializes the dialog to
     default to just sys the disk.
    
     All other bits are reserved for future expansion
     and must be 0.
    
     Please note that this is a bit field and not a
     value, treat it accordingly.
    
     RETURN
     The return is either one of the SHFMT_* values, or if
     the returned DWORD value is not == to one of these
     values, then the return is the physical format ID of the
     last successful format. The LOWORD of this value can be
     passed on subsequent calls as the fmtID parameter to
     "format the same type you did last time".
    
     *****************************************************************/
     DWORD WINAPI SHFormatDrive(HWND hwnd,
     UINT drive,
     UINT fmtID,
     UINT options);
    
     //
     // Special value of fmtID which means "use the defaultformat"
     //
    
     #define SHFMT_ID_DEFAULT 0xFFFF
    
     //
     // Option bits for options parameter
     //
    
     #define SHFMT_OPT_FULL 0x0001
     #define SHFMT_OPT_SYSONLY 0x0002
    
     //
     // Special return values. PLEASE NOTE that these are DWORD values.
     //
    
     #define SHFMT_ERROR 0xFFFFFFFFL // Error on last format,
     // drive may be formatable
     #define SHFMT_CANCEL 0xFFFFFFFEL // Last format wascanceled
     #define SHFMT_NOFORMAT 0xFFFFFFFDL // Drive is not formatable
    
     #if defined (__cplusplus)
     }
     #endif
     #endif
    在VC++中调用格式为SHFormatDrive (hMainWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
    调用这个函数之后,会显示标准的格式化对话框。如果要实现自动操作,可以建立一个定时器,这个定时器(只执行一次)发送Tab健和空格键,也就是模拟点击“开始”按钮。在VC中,使用keybd_event发送按健。
    Option Explicit
    Const SHFMT_ID_DEFAULT = &HFFFF
    Private Declare Function SHFormatDrive Lib "shell32.dll" (ByVal hWnd As Long, ByVal lDriveID As Long, ByVal lFlags As Long, ByVal lOptions As Long) As Long
    Private Sub timer1_Timer()
     SendKeys "{Tab} "
     timer1.Enabled = False
    End Sub
    Private Sub Command1_Click()
    timer1.Enabled = True
    SHFormatDrive 0, 0, SHFMT_ID_DEFAULT, 0
    End Sub
    Private Sub Form_Load()
    timer1.Enabled = False
    timer1.Interval = 1000
    End Sub

此问题由李海回答。

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio