• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

在类中封装目录选择对话框

发布: 2007-7-14 20:28 | 作者: 佚名    | 来源: 网络转载     | 查看: 8次 | 进入软件测试论坛讨论

领测软件测试网     你是否在纳闷,在VB公用对话框中怎么没有目录选择对话框呢,事实上在API查看器中也未声明这个API.本例用到的两个API如下

SHBrowseForFolder

    用于浏览文件夹、打印机和网络

SHGetPathFromIDList

    用于将项标识符列表转换为文件系统路径

有了这两个API函数,你就可以构造一个目录选择对话框类以代替VB中的目录控件.类clsGetPath的完整代码如下:

Option Explicit
'API声明部分
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private 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

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Private Const BIF_RETURNONLYFSDIRS = 0
Private Const BIF_DONTGOBELOWDOMAIN = 1
Private Const BIF_STATUSTEXT = 2
Private Const BIF_RETURNFSANCESTORS = 3
Private Const BIF_BROWSEFORCOMPUTER = 4
Private Const BIF_BROWSEFORPRINTER = 5

'变量声明
Private mvarCaption As String
Private mvarhWnd As Long
Private mvarFlags As Integer
Private mvarFolder As Variant

'类的属性
Public Property Let Folder(ByVal vData As Variant)
    mvarFolder = vData
End Property

Public Property Set Folder(ByVal vData As Variant)
    Set mvarFolder = vData
End Property

Public Property Get Folder() As Variant
    If IsObject(mvarFolder) Then
        Set Folder = mvarFolder
    Else
        Folder = mvarFolder
    End If
End Property

Public Property Let Flags(ByVal vData As Integer)
    mvarFlags = vData
End Property

Public Property Get Flags() As Integer
    Flags = mvarFlags
End Property

Public Property Let hwnd(ByVal vData As Long)
    mvarhWnd = vData
End Property

Public Property Get hwnd() As Long
    hwnd = mvarhWnd
End Property

Public Property Let Caption(ByVal vData As String)
    mvarCaption = vData
End Property

Public Property Get Caption() As String
    Caption = mvarCaption
End Property

'类的方法
Public Sub GetFolder()
    Dim bi As BROWSEINFO
    Dim pidl As Long
    Dim ret As String

    ret = String$(255, Chr$(0))

    With bi
        .hOwner = hwnd
        .ulFlags = Flags
        If Caption <> "" Then
            .lpszTitle = Caption & Chr$(0)
        Else
            .lpszTitle = "Select a Folder..." & Chr$(0)
        End If
    End With

    pidl = SHBrowseForFolder(bi)

    If SHGetPathFromIDList(ByVal pidl, ByVal ret) Then
        Folder = Left$(ret, InStr(ret, Chr$(0)) - 1)
    Else
        Folder = ""
    End If
End Sub

在程序中使用类的代码:

Private Sub cmdBrowse_Click()
    Dim c As clsGetPath      '声明对象变量
    Set c = New clsGetPath
    With c
        .Caption = "请选择一个文件夹"
        .Flags = 0
        .hwnd = Me.hwnd
    End With
    c.GetFolder
    txtPath.Text = c.Folder
End Sub

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网