VB 对话框窗口的使用

前端之家收集整理的这篇文章主要介绍了VB 对话框窗口的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一、概述
在 VB 编程中有许多调用窗口和对话框的操作,但是 VB 自已内置的却非常的少,这里所说的"不常用"也就是说通过 API 函数来实现各类调用窗口的操作。
二、通过 API 函数调用窗口的实用技巧如下:
(1)打开文件属性窗口
图1,是一个文件属性窗口,但是在VB中没提供"打开文件属性窗口"的函数,不过可以通过调用 API 函数"ShellExecuteEX "来完成。
Module1.bas
注释:声明 API 函数
Type SHELLEXECUTEINFO 注释:这可以VB自代的API帮助中找到
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type

Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400

Declare Function ShellExecuteEX Lib "shell32.dll" Alias _
"ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long

注释:定义子程序
Public Sub ShowProps(FileName As String,OwnerhWnd As Long)
Dim SEI As SHELLEXECUTEINFO
Dim r As Long
With SEI
.cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS Or _
SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
.hwnd = OwnerhWnd
.lpVerb = "properties"
.lpFile = FileName
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = 0
.hInstApp = 0
.lpIDList = 0
End With
r = ShellExecuteEX(SEI)
End Sub
注释:调用窗口
ShowProps "c:command.com",Me.hwnd

(2)激活文件目录对话窗口
图2,是文件目录对话框,这个对话框在 VB 编中经常用到,可是 VB 却没有调用它的函数,只有通过下面 API 函数来完成相关操作。

声明 API 函数
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long 注释:ITE MIDLIST
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long,ByVal pszPath As String) As Long

Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
注释:定义子程序
Sub BrowDir()
Dim bi As BROWSEINFO
Dim pidl&,rtn&,path$,pos%
pidl& = SHBrowseForFolder(bi)
path$ = Space$(512)
rtn& = SHGetPathFromIDList(ByVal pidl&,ByVal path$)
If rtn& Then
pos% = InStr(path$,Chr$(0))
MyPath = Left(path$,pos - 1)
End If
End Sub
注释:调用该对话框
BrowDir注释:返回 MyPath 的值就是所选的目录
(3)如何调用系統的【查找】、【替换】的对话框?

如图3、图4,大家在编写记事本程序时,都会去编写这两个对话框的功能,其时 Microsoft 提供了一个控件文档,就是 comdlg32.dll,它可以方便的调用这两个对话框
注释:声明 Type FINDREPLACE
Private Type FINDREPLACE
lStructSize As Long
hwndOwner As Long
hInstance As Long
flags As Long
lpstrFindWhat As String
lpstrReplaceWith As String
wFindWhatLen As Integer
wReplaceWithLen As Integer
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

注释:调用 Common Dialog DLL
Private Declare Function FindText Lib "comdlg32.dll" Alias "FindTextA" _
(pFindreplace As FINDREPLACE) As Long
Private Declare Function ReplaceText Lib "comdlg32.dll" Alias "ReplaceTextA" _
(pFindreplace As FINDREPLACE) As Long
Dim frText As FINDREPLACE
在表单中加入二个 Command Button,并命名为 cmdFind,cmdReplace,加入以下程序代码
Private Sub cmdFind_Click()
FindText frText 注释:调用查找对话框
End Sub
Private Sub cmdReplace_Click()
ReplaceText frText 注释:调用替换对话框
End Sub
Private Sub Form_Load()
With frText
.lpstrReplaceWith = "Replace Text"
.lpstrFindWhat = "Find Text"
.wFindWhatLen = 9
.wReplaceWithLen = 12
.hInstance = App.hInstance
.hwndOwner = Me.hWnd
.lStructSize = LenB(frText)
End With
End Sub
(4)在程序中调用关闭 Windows对话框 这个对话框比较简单只用一句话就搞定。 Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long 注释:调用 SHShutDownDialog 0

猜你在找的VB相关文章