|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Now when the window loads, we can make the window topmost this way:
Private Sub Form_Load()
retVal = SetWindowPos(Me.hwnd, HWND_TOPMOST, 100, 100, _
100, 100, SWP_DRAWFRAME Or SWP_SHOWWINDOW)
End Sub
Thats all it takes. Now the window will stay on top of other windows, as you see in Figure 23.11. Our topmost program is a success.
Determining Free And Total Disk SpaceThe Testing Department is calling again. Your program, SuperDuperDataCrunch, is overflowing users disks. Cant you check how much disk space is available before writing out your 800MB database? Hmm, you think, how would that work? You can use the GetDiskFreeSpace function to check how much space there is on a disk:
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
GetDiskFreeSpaceA (ByVal lpRootPathName As String, _
lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) _
As Long
Heres what this functions arguments mean:
To find the total free space, you multiply SectorsPerCluster * BytesPerSector * NumberOfFreeClusters. To find the total space on the drive, you multiply SectorsPerCluster * BytesPerSector * TotalNumberOfClusters. Lets see an example. Here, well determine the free space on the c: drive. We start by declaring GetDiskFreeSpace:
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
"GetDiskFreeSpaceA" (ByVal lpRootPathName As String,_
lngSectorsPerCluster As Long, lngBytesPerSector As Long, _
lngNumberOfFreeClusters As Long, lngTotalNumberOfClusters As Long)_
As Long
Next, when the form loads, we fill the variables lngSectorsPerCluster, lngBytesPerSector, lngNumberOfFreeClusters, and lngTotalNumberOfClusters with GetDiskFeeSpace for drive C:
Private Sub Form_Load()
Dim lngSectorsPerCluster As Long
Dim lngBytesPerSector As Long
Dim lngNumberOfFreeClusters As Long
Dim lngTotalNumberOfClusters As Long
retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
lngBytesPerSector, lngNumberOfFreeClusters, lngTotalNumberOfClusters)
...
Finally, we can display the total number of free bytes in a label, Label1, this way:
Private Sub Form_Load()
Dim lngSectorsPerCluster As Long
Dim lngBytesPerSector As Long
Dim lngNumberOfFreeClusters As Long
Dim lngTotalNumberOfClusters As Long
retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
lngBytesPerSector, lngNumberOfFreeClusters, _
lngTotalNumberOfClusters)
Label1.Caption = "Free space on drive c: " & _
Str(lngSectorsPerCluster * lngBytesPerSector * _
lngNumberOfFreeClusters) & " bytes"
End Sub
When you run the program, youll see the number of free bytes on the computers C: drive, something like the display in Figure 23.12.
Determining The Windows DirectorySometimes you want to determine where Windows is installed on a system to be able to install files such as initialization files or DLLs. You can use the GetWindowsDirectory function to find the Windows directory:
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As _
Long) As Long
Here are the arguments for the GetWindowsDirectory function:
Lets see an example. Here, well add some code to the example we started in the previous topic that displays free disk space. We add the declaration well need for GetWindowsDirectory:
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
"GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _
lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long)
As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As _
Long) As Long
Next, we set up a text buffer, get the Windows directory with GetWindowsDirectory, and display the Windows directory in a new label, Label2:
Private Sub Form_Load()
Dim lngSectorsPerCluster As Long
Dim lngBytesPerSector As Long
Dim lngNumberOfFreeClusters As Long
Dim lngTotalNumberOfClusters As Long
Dim strBuffer As String * 1024
retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
lngBytesPerSector, lngNumberOfFreeClusters, lngTotalNumberOfClusters)
Label1.Caption = "Free space on drive c: " & Str(lngSectorsPerCluster_
* lngBytesPerSector * lngNumberOfFreeClusters) & "bytes"
retVal = GetWindowsDirectory(strBuffer, 1024)
Label2.Caption = "Windows directory: " & strBuffer
End Sub
Thats itthe result appears in Figure 23.13. As you can see, were now able to find the directory in which Windows itself is installed.
The code for this example is located in the windisk folder on this books accompanying CD-ROM.
Connecting To Visual C++The Testing Department is calling again. Another company programmer has written a great routine for alphabetizing spreadsheets that would be great in your program, SuperDuperDataCrunch. Terrific, you say. They say, the only problem is that those routines are written in Visual C++, and youll have to interface them to Visual Basic. Visual C++. You say, whats that?
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |