转:SHFormateDrive用法

来源:互联网 发布:矢量图绘制软件 编辑:程序博客网 时间:2024/06/14 04:47
SHFormatDrive(格式化硬盘api函数的用法)后附正确使用
2010-04-13 21:09

#include <windows.h>
//#include <shellapi.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{

SHFormatDrive (NULL, 0, SHFMT_ID_DEFAULT, 0);

return 0;
}

已上是我的代码, 可是编译不能能过,我已经包含了shell32.lib,
请教怎么才能调用成功shformatdrive函数,希望能贴出编译成功,能够格式化硬盘的代码

在头部加入以下代码
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#if !defined(SHFMT_OPT_FULL)

#if defined (__cplusplus)
extern "C" {
#endif

DWORD WINAPI SHFormatDrive(HWND hwnd,
UINT drive,
UINT fmtID,
UINT options);

#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    

#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

//
void CFormatDriveDlg::OnFormat()
{
UINT OldMode = SetErrorMode(0); // Get the current Error Mode settings.
SetErrorMode(OldMode & !SEM_FAILCRITICALERRORS); // Force O/S to handle
//critical errors.
// Call SHFormatDrive here.
SHFormatDrive (this->m_hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);

SetErrorMode(OldMode); // Put it back the way it was.
}

想编写一个磁盘格式化的程序,调用了SHFormatdrive,弹出了Windows磁盘格式化对话窗,但还要点击“开始”按钮才能格式化磁盘,请教大虾是否有不需要点击“开始”按钮就能直接实现格式化的方法?

程序为:
Option Explicit
     Private Declare Function SHFormatDrive Lib "shell32" (ByVal Hend As Long, ByVal Drive As Long, ByVal FormatID As Long, ByVal Options As Long) As Long

     Private Sub FormatDisk(intDrive As Integer, blnQuickFormat As Boolean)
       Dim lngReturn As Long
   
       If (blnQuickFormat) Then
          lngReturn = SHFormatDrive(0, intDrive, 0&, 1&)
       Else
          lngReturn = SHFormatDrive(0, intDrive, 0&, 0&)
       End If
     End Sub

     Private Sub Command1_Click()
     'Dim disknum As Long
       'Call select1
       'Call FormatDisk(0, True)
       Call FormatDisk(w2, True)
     End Sub

在VC++中调用格式为SHFormatDrive (hMainWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
     调用这个函数之后,会显示标准的格式化对话框。如果要实现自动操作,可以建立一个定时器,这个定时器(只执行一次)发送Tab健和空格键,也就是模拟点击“开始”按钮。在VC中,使用keybd_event发送按健。

模拟按键在VB里面可以用SendKeys代替。具体用法见MSDN。这个应该可以解决你的问题。


原创粉丝点击