Visual Basic Tip: 10/30/95

How to format a floppy disk using VB and Windows 3.x File Manager.
'Subroutine to call Format code in FORM1.FRM
'  The subroutine FMFormat assumes that it is being called from the main form
'  of your project.  It ensures the program ends if Form1 is unloaded!

Sub Command1_Click ()
 FMFormat form1
End Sub

'Declarations in MODULE.BAS

Type Rect
  Left As Integer
  Top As Integer
  Right As Integer
  Bottom As Integer
End Type
Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
Declare Sub GetWindowRect Lib "User" (ByVal hWnd As Integer, lpRect As Rect)
Declare Function IsWindow Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function WinExec Lib "Kernel" (ByVal lpCmdLine As String, ByVal nCmdShow As Integer) As Integer
Declare Function SetActiveWindow Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function LockWindowUpdate Lib "User" (ByVal hwndLock As Integer) As Integer
Declare Function GetDesktopWindow Lib "User" () As Integer
Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Declare Function PostMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As Integer

Const WM_COMMAND = & H111
Const WM_CLOSE = & H10
Const SWP_NOSIZE = & H1

'Subroutine CenterDialog in MODULE.BAS

Sub CenterDialog (WinText As String, FMThwnd As Integer)
 Dim lpDlgRect As Rect
 Dim lpDskRect As Rect

   FMThwnd = FindWindow(0&, WinText)
   If FMThwnd Then Exit Do
   x% = DoEvents()
 Call GetWindowRect(FMThwnd, lpDlgRect)
 wdth% = lpDlgRect.Right - lpDlgRect.Left
 hght% = lpDlgRect.Bottom - lpDlgRect.Top
 Call GetWindowRect(GetDesktopWindow(), lpDskRect)
 Scrwdth% = lpDskRect.Right - lpDskRect.Left
 Scrhght% = lpDskRect.Bottom - lpDskRect.Top
 x% = (Scrwdth% - wdth%) \ 2
 y% = (Scrhght% - hght%) \ 2
 Call SetWindowPos(FMThwnd, 0, x%, y%, 0, 0, SWP_NOZORDER Or SWP_NOSIZE)
End Sub

'Subroutine FMFormat in MODULE.BAS

Sub FMFormat (f As Form)
 Dim WFflag As Integer, FMThwnd As Integer
 formhwnd = f.hWnd

 FMThwnd = FindWindow(0&, "Format Disk")
 If FMThwnd > 0 Then  'format dialog already open
  x% = SetActiveWindow(FMThwnd)
  Exit Sub
 End If

 WFflag = False
 FMhWnd = FindWindow("WFS_Frame", 0&)
 If FMhWnd = 0 Then
  i% = WinExec("Winfile", 0)
  FMhWnd = FindWindow("WFS_Frame", 0&)
  If FMhWnd = 0 Then
    MsgBox "Can't find the File Manager.", 48, "Warning"
    Exit Sub
  End If
  WFflag = True
 End If
 x% = LockWindowUpdate(GetDesktopWindow())
 x% = PostMessage(FMhWnd, WM_COMMAND, &HCB, 0)
 Call CenterDialog("Format Disk", FMThwnd)
 x% = LockWindowUpdate(0)
 While IsWindow(FMThwnd)
  x% = DoEvents()
  If IsWindow(formhwnd) = 0 Then x% = PostMessage(FMThwnd, WM_CLOSE, 0, 0)
 If WFflag Then x% = PostMessage(FMhWnd, WM_CLOSE, 0, 0)

 If IsWindow(formhwnd) = 0 Then
  x% = SetActiveWindow(formhwnd)
 End If
End Sub
Return to Phil's Web Page

Created by: Philip