http://www.applevb.com/sourcecode/extview2.zip
ListView的使用范例(2)建立文件浏览窗口 下载(10.2K)

解决方案 »

  1.   

    谢谢,但是那个程序有个问题:
    比如.pdf文件
    现在有个路径名C:\1.pdf,但是实际不存在这个文件!这时候咋办?如何
    显示?总不能搜索所有硬盘直到找到.pdf为至呀。还有嘛?
      

  2.   

    介绍你一个相关的知识!--程序同文件扩展名建立关联,需要修改注册表还有一些操作!不算很麻烦!
    ====================================================================
    如何使你的程序同文件扩展名建立关联?  如果你的程序是同文件操作有关,比如说你自定义了一个文件类型,用你的程序才能打开它并查看其中的内容。你有没有想过在你的文件同你的程序之间建立关联,这样不必每次都要先启动程序,再打开文件了。用户可直接点选你的文件,而不必每次都要面对一个“打开方式”对话框。其实要做到这一点,只须调用API函数就行了。下面的代码向你演示如何实现这一功能。
    首先在窗体的通用声明段中加入下面的代码:
          Option Explicit      Private Declare Function RegCreateKey Lib "advapi32.dll" Alias  "RegCreateKeyA" (ByVal hKey As Long, _
                            ByVal lpSubKey As String,  phkResult As Long) As Long
          Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, _
                     ByVal lpSubKey As String,  ByVal dwType As Long,  ByVal lpData As String,  ByVal cbData As Long) As Long      ' Return codes from Registration functions.
          Const ERROR_SUCCESS = 0&
          Const ERROR_BADDB = 1&
          Const ERROR_BADKEY = 2&
          Const ERROR_CANTOPEN = 3&
          Const ERROR_CANTREAD = 4&
          Const ERROR_CANTWRITE = 5&
          Const ERROR_OUTOFMEMORY = 6&
          Const ERROR_INVALID_PARAMETER = 7&
          Const ERROR_ACCESS_DENIED = 8&      Private Const HKEY_CLASSES_ROOT = &H80000000
          Private Const MAX_PATH = 260&
          Private Const REG_SZ = 1在窗体的Click事件中加入下面的代码 
          Private Sub Form_Click()          Dim sKeyName As String   'Holds Key Name in registry.
              Dim sKeyValue As String  'Holds Key Value in registry.
              Dim ret&           'Holds error status if any from API calls.
              Dim lphKey&        'Holds created key handle from RegCreateKey.      'This creates a Root entry called "MyApp".
              sKeyName = "MyApp"
              sKeyValue = "My Application"
              ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
              ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)      'This creates a Root entry called .BAR associated with "MyApp".
              sKeyName = ".BAR"
              sKeyValue = "MyApp"
              ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
              ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)      'This sets the command line for "MyApp".
              sKeyName = "MyApp"
              sKeyValue = "c:\mydir\my.exe %1"
              ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
              ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, _
                                  sKeyValue, MAX_PATH)
          End Sub            
    按F5运行程序并在窗体上点击一下鼠标,然后退出程序. 
    从开始菜单中运行REGEDIT,你可在HKEY_CLASSES_ROOT下找到.bar和MyApp两个子项,结构如下所示:      .bar = MyApp
          MyApp = My Application
            |
             -- Shell
                  |
                   -- open
                        |
                         -- command = c:\mydir\my.exe %1
    你可以查看关于RegCreateKey和RegSetValue的用法说明。
    ====================================================================
    不同的是你只需要设定与你希望关联的文件进行关联即可!
      

  3.   

    我做过这样的程序
    解决方法就是建一个空文件
    比如你的"c:\1.pdf"不存在
    就在app.path+"\temp\"下用open
    建一个temp.pdf文件,然后获得其图标,在把它删掉就OK了。
    这样的过程一种后缀只需一次,所以也很快,基本没有问题。
      

  4.   

    '给你一段代码,希对你有所帮助
    '以下在VB6 SP5 WINDOWSME下调试通过
    Private Const SHGFI_DISPLAYNAME = &H200 'SHFILEINFO中的 szDisplayName 返回文件显示的名称
    Private Const SHGFI_TYPENAME = &H400 'SHFILEINFO中的 szTypeName 返回文件类型名
    Private Const SHGFI_ICON = &H100 ''SHFILEINFO中的 hIcon 返回文件相关联的图标名柄
    Private Const SHGFI_ICONLOCATION = &H1000
    Private Const SHGFI_SMALLICON = &H1
     
    Private Const MAX_PATH = 260Private Type SHFILEINFO
        hIcon As Long            '得到的与文件相关联的图标的句柄
        iIcon As Long          ' 得到的与文件相关联的图标的索引号
        dwAttributes As Long               '结构返回的内容标志
        szDisplayName As String * MAX_PATH '文件显示的名称
        szTypeName As String * 80         ' 文件类型
    End TypeDim ShF As SHFILEINFOPrivate Declare Function SHGetFileInfo Lib "Shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal X As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    Private Declare Function ShFileExists Lib "Shell32.dll" Alias "#45" (ByVal FileN$) As Long
    '在窗体添 加一LISTVIEW并设其VIEW=2—lvwList
    '在窗体中添加一图片框PICTURE1;其AUTODRAW=TRUE,VISIBLE=FALSE
    '在窗体中添加一IMAGELIST名为IMAGELIST1设置其图片大小
    '由于IMAGELIST在使用前一定要初始化,你可由代码实现初始化,本例是在设置中实现初始化的
    '就是设定好其图片大小并预加载一图片并与LISTVIEW1关联(LISTVIEW的图片一般均从IMAGELIST中加载)
     
    Private Sub Form_Load()
        Dim FileName$
        Dim FileHao&
        '此处只是演示,实际应用中请自行处理
        Picture1.Cls
        Picture1.Picture = LoadPicture()
        FileName$ = "c:\windows\desktop\8.ico"
        '判所给文件是否存在
        If ShFileExists(FileName$) = 0 Then
         FileHao = FreeFile
         Open FileName$ For Output As FileHao
         Close FileHao
          SHGetFileInfo FileName$, 0, ShF, Len(ShF), SHGFI_ICON Or SHGFI_DISPLAYNAME Or SHGFI_TYPENAME
          If ShFileExists(FileName$) <> 0 Then Kill FileName$
        Else
           SHGetFileInfo FileName$, 0, ShF, Len(ShF), SHGFI_ICON Or SHGFI_DISPLAYNAME Or SHGFI_TYPENAME
        End If
        '创建一图片至PICTURE1
        DrawIcon Picture1.hdc, 0, 0, ShF.hIcon
        Picture1.Refresh
        '释放占用的内存
        DestroyIcon ShF.hIcon
        '添加至IMAGELIST1
        ImageList1.ListImages.Add 2, "2i", Picture1.Image
        '添加LISTVIEW1(此处人为添加了两次,应用时请自行处理)
        ListView1.ListItems.Add 1, "", DelNull(ShF.szDisplayName) & "为:" & DelNull(ShF.szTypeName), 0, "2i"
        ListView1.ListItems.Add 2, "", DelNull(ShF.szDisplayName) & "为:" & DelNull(ShF.szTypeName), 0, "2i"
    End Sub'删除字符串尾的空字符
    Function DelNull(sIn As String) As String
        Dim Pos As Integer
        Pos = InStr(1, sIn, vbNullChar)
        If Pos > 0 Then
            DelNull = Left$(sIn, Pos - 1)
        Else
            DelNull = sIn
        End If
    End Function