首页->技巧->系统->详细内容
Creating Nested Directories

Abstract
You can use the Visual Basic MkDir statement to create a new
directory on any floppy or hard disk. This article explains how you
can use this statement to create nested subdirectories, even if part
of the path already exists.

The MkDir Statement
When developing an application in Visual Basic原 you may need to allow
the user to store a data file in a specific directory on disk. The
dirctory can be created with Visual Basic's MkDir statement. The
syntax for this statement is:

MkDir "C:\<JUNK>"
where JUNK is the name of the directory you want to create and C:\ is
the drive you want to create it on. If you want to create a nested
subdirectory, for example C:\JUNK\TEST, you must first create the
JUNK directory and then create the TEST directory. However, if the
JUNK directory already exists, the MkDir statement will interrupt your
application with a "Path/File Access Error" displayed in a message
box.
To prevent this error message from appearing when your application is
creating directories, you can use the On Error and Resume Next
statements. These two statements allow you to trap this access error
and force your Visual Basic program to continue executing as if the
error had not actually occurred.

The example program below creates a directory that is three levels
deep, called "\JUNK\TEST\TEST". The first time the MkDir statement is
executed, it creates the JUNK directory. The second and third times
MkDir is executed, however, it produces Error 76. The Resume Next
statement tells our program to ignore this error each time it is
encountered until we have successfully created the entire directory
structure.

Example Program
This program shows how to create nested directories even if one of
the directories in the path already exists.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Command Button control to Form1. Command1 is created by
default. Set its Caption property to "Create Directory".
3. Add the following code to the Click event for Command1:

Sub Command1_Click()
CreateNewDirectory "C:\junk3\test\test"
MsgBox "Directory was created", 16, "Dir Demo"
End Sub

4. Create a new function called CreateNewDirectory. Add the following
code to this function:

Sub CreateNewDirectory(DirName As String)
Dim NewLen As Integer
Dim DirLen As Integer
Dim MaxLen As Integer

NewLen = 4
MaxLen = Len(DirName)

If Right$(DirName, 1) <> "\" Then
DirName = DirName + "\"
MaxLen = MaxLen + 1
End If
On Error GoTo DirError

MakeNext:
DirLen = InStr(NewLen, DirName, "\")
MkDir Left$(DirName, DirLen - 1)
NewLen = DirLen + 1
If NewLen >= MaxLen Then
Exit Sub
End If
GoTo MakeNext
DirError:
Resume Next
End Sub

Run the program by pressing the F5 function key. When you click the
"Create Directory" command button, the program creates a directory
called "JUNK\TEST\TEST" on drive C. Change the path in the Click event
for Command1 to "C:\JUNK\TEST\TEST2" and run the program a second
time. It will create a second subdirectory called TEST2 under
C:\JUNK\TEST. Notice that Error 76 is ignored by the program. This
error is generated by MkDir when it attempts to create a directory
(in this case, C:\JUNK\TEST) that already exists.
返回

Invoking Menu Items in Other Applications with SendMessage

Abstract
Within a Visual Basic application, you can execute a menu item in
another Windows厝based program. This article explains how to use
several Windows application programming interface (API) functions to
execute menu commands.

Executing Menu Commands
In some situations, you may need to actuate an application, such as
Notepad, and execute one or more of that application's menu commands.
The Windows application programming interface (API) provides several
functions that enable you to perform this type of operation in Visual
Basic叟

- The Windows API FindWindow function can be used to determine the
handle of the application that contains the menu item you want to
execute. FindWindow returns an integer value containing the
application's handle.
- You also need to retrieve the handle associated with the target
window's menu. The GetMenu function will return the handle as an
integer value.
- Once you have the target window's menu handle, you need to determine
the entry's position in the menu and retrieve the handle of the
pop-up menu. In the example program below, we want to retrieve the
handle of the File menu selection. Therefore, we would call the
GetSubMenu function with zero as the entry's position. The first
entry in every pop-up menu always begins with entry number zero.
- Next, we want to retrieve the ID number of the specific menu entry
we want to execute. We retrieve this ID number by calling the
GetMenuItemID function with the entry's position specified as one
(the position of the Open menu selection in the pop-up menu).
- The final step is to make the target application the active
application and to issue the SendMessage function, which in turn
sends a WM_COMMAND message to the target window. The WM_COMMAND
message is set to the target application's window to execute the
File/Open command.

Example Program
The following example program executes another application's menu
commands. This program assumes that the Windows Notepad application
program is already running in memory. This program uses SendMessage to
execute the File/Open menu selection in Notepad.
When you execute the program, click the Command Button. After a second
or two, you will see that Notepad has been activated and that its Open
File dialog box is displayed on the screen.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following Constant and Declare statements to the General
Declarations section of Form1 (note that each Declare statement
should be typed as a single line of code):

Private Declare Function FindWindow Lib "User" (ByVal lpClassName
As Any, ByVal lpWindowName As Any) As Integer
Private Declare Function GetMenu Lib "User" (ByVal hWnd
As Integer) As Integer
Private Declare Function GetMenuItemID Lib "User" (ByVal hMenu
As Integer, ByVal nPos As Integer) As Integer
Private Declare Function GetSubMenu Lib "User" (ByVal hMenu
As Integer, ByVal nPos As Integer) As Integer
Private Declare Function SendMessage Lib "User" (ByVal hWnd
As Integer, ByVal wMsg As Integer, ByVal wParam
As Integer, lParam As Any) As Long
Const WM_COMMAND = &H111

3. Add a Command Button control to Form1. Command1 is created by
default.
4. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
Dim hWnd As Integer
Dim hMainMenu As Integer
Dim hMenu As Integer
Dim MenuID As Integer

hWnd = FindWindow("NotePad", "Untitled - NotePad")
If hWnd = 0 Then Exit Sub

hMainMenu = GetMenu(hWnd)
hMenu = GetSubMenu(hMainMenu, 0)
MenuID = GetMenuItemID(hMenu, 1)
AppActivate "Untitled - NotePad"
X& = SendMessage(hWnd, WM_COMMAND, MenuID, 0&)
End Sub
返回

Implementing a "wait" function in VB

Here's a simple way of implementing an accurate "wait" function in VB.

1) Add a timer called timer1 to a form. Set its Interval property to 0
and it's enabled property to FALSE.

2) Add two labels (label1 and label2) and a command button (command1) to
the form.

2) Add the following subroutine and Timer1 event code:


Public Sub Wait(seconds)

'-- Turn timer on
Timer1.Enabled = True

'-- Set Timer Interval
Me.Timer1.Interval = 1000 * seconds
While Me.Timer1.Interval > 0
DoEvents
Wend

'-- Turn timer off
Timer1.Enabled = False
End Sub


Private Sub Timer1_Timer()
Timer1.Interval = 0
End Sub


That's it! Use the Wait function anywhere a delay is required, for
example:

Private Sub Command1_Click()
Label1.Caption = Now
Wait (5)
Label2.Caption = Now
End Sub
返回

获得驱动器的卷标

在Form中添加一个CommandButton控件,再加入一下一段代码:
Private Declare Function GetVolumeInformation Lib "kernel32" _
Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long

Const FILE_VOLUME_IS_COMPRESSED = &H8000

Public Sub GetVolInfo(ByVal path As String)
Dim aa As Long
Dim VolName As String
Dim fsysName As String
Dim VolSeri As Long, compress As Long
Dim Sysflag As Long, Maxlen As Long

'初试化字符串的长度
VolName = String(255, 0)
fsysName = String(255, 0)
aa = GetVolumeInformation(path, VolName, 256, VolSeri, Maxlen, _
Sysflag, fsysName, 256)
VolName = Left(VolName, InStr(1, VolName, Chr(0)) - 1)
fsysName = Left(fsysName, InStr(1, fsysName, Chr(0)) - 1)
compress = Sysflag And FILE_VOLUME_IS_COMPRESSED
If compress = 0 Then
Me.Print "未压缩驱动器"
Else
Me.Print "压缩驱动器"
End If

Me.Print "驱动器卷标 :", VolName
Me.Print "驱动器标号 : ", Hex(VolSeri)
Me.Print "驱动器文件系统 (FAT, HPFS, or NTFS)", fsysName
Me.Print "支持的文件名长度", Maxlen
End Sub

Private Sub Command1_Click()
Form1.Caption = "c:驱动器信息"
Call GetVolInfo("c:\")
End Sub
返回

启动控制面板命令大全

控制面板
模块: control.exe
命令: rundll32.exe shell32.dll,Control_RunDLL
结果: 显示控制面板窗口。
例子:
Dim x
x = Shell("rundll32.exe shell32.dll,Control_RunDLL")

辅助选项
模块: access.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL access.cpl,,5
结果: 显示辅助选项/常规。

命令: rundll32.exe shell32.dll,Control_RunDLL access.cpl,,1
结果: 显示辅助选项/键盘。

命令: rundll32.exe shell32.dll,Control_RunDLL access.cpl,,2
结果: 显示辅助选项/声音。

命令: rundll32.exe shell32.dll,Control_RunDLL access.cpl,,3
结果: 显示辅助选项/显示。

命令: rundll32.exe shell32.dll,Control_RunDLL access.cpl,,4
结果: 显示辅助选项/鼠标。

添加新硬件
模块: sysdm.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl @1

增加新的打印机
模块:shell32.dll
命令: rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter

添加/删除程序
模块:appwiz.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1
结果:显示安装/卸载。

命令: rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1
结果:显示安装/卸载。

命令: rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,2
结果:显示Windows 安装。

命令: rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,3
结果:显示启动盘。

复制磁盘
模块:diskcopy.dll
命令:rundll32.exe diskcopy.dll,DiskCopyRunDll

时间/日期
模块: timedate.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,0
结果: 显示设置日期/时间。

命令: rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,1
结果: 显示设置时间区域。

拨号连接(DUN)
模块: rnaui.dll
命令: rundll32.exe rnaui.dll,RnaDial 连接_名称
结果: 打开指定的拨号连接。
例子:
x= Shell("rundll32.exe rnaui.dll,RnaDial "  &  "连接_名称", 1)

显示器
模块: desk.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
结果: 背景设置。

命令: rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1
结果: 屏幕保护设置。

命令: rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2
结果: 外观设置。

命令: rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3
结果: 设置窗口。

操纵杆
模块: joy.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL joy.cpl

邮件/传真
模块: mlcfg32.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL mlcfg32.cpl
结果: 出现 MS Exchange 属性设置。

邮局设置
模块: wgpocpl.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL wgpocpl.cpl
结果: 显示 MS Postoffice Workgroup Admin 设置。

主设置
模块: main.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @0
结果: 显示鼠标属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @1
结果: 显示键盘/速度属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @1,,1
结果: 显示键盘/语言属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @1,,2
结果: 显示键盘/常规属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @2
结果: 显示打印机属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @3
结果: 显示字体属性。

命令: rundll32.exe shell32.dll,Control_RunDLL main.cpl @4
结果: 显示电源管理属性。

增加 Modem
模块:modem.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL modem.cpl,,add

多媒体
模块: mmsys.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,0
结果:声音。

命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,1
结果:视频。

命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2
结果:声音 MIDI。

命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,3
结果:CD/音乐。

命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,4
结果:高级。

命令: rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl @1
结果:声音。

网络
模块:netcpl.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL netcpl.cpl

打开方式窗口(Open With)
模块: shell32.dll
命令:rundll32.exe shell32.dll,OpenAs_RunDLL path\filename

口令
模块: password.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL password.cpl

区域设置
模块: intl.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,0
结果: 区域设置。

命令: rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,1
结果: 数字格式设置。

命令: rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,2
结果: 金额格式设置。

命令: rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,3
结果:时间格式设置。

命令: rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,4
结果: 日期格式设置。

屏幕保护
模块: appwiz.cpl
命令: rundll32.exe desk.cpl,InstallScreenSaver c:\win\system\Flying Windows.scr
结果: 安装屏幕保护并显示预览属性页。

系统设置
模块: sysdm.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,0
结果: 显示常规设置。

命令: rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,1
结果: 显示设备管理设置。

命令: rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,2
结果: 显示硬件设置。

命令: rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3
结果: 显示性能设置。

IE4 设置
模块: inetcpl.cpl
命令: rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl
返回

Back to top