VBGood网站全文搜索 Google

首页 - 经验之谈 - 设置文件关联
发表评论(0)作者:Sam Huggill, 平台:VB6.0+Win98, 阅读:9195, 日期:2001-04-29
Make a file association

By Sam Huggill


Many programs these days come with their own file format, so why shouldn注释:t yours? By accessing the correct parts of the registry you can make a link to your own file type, and execute your program when the file is double clicked on. This topic is far from complete in that you would want to interpret the command line parameters sent to your program about the file. Check out http://vbaccelerator.com for an article on command line params. Anyway, back to the code. This sample code creates a link to .xxx files and calls them MyApp.Document. It then loads up c:\LongPathname\MyApp.Exe Change these values as appropiate.

Add a new form with a command button (cmdTest). Copy the following code into the form:

Option Explicit

注释:// Windows Registry Messages
Private Const REG_SZ As Long = 1
Private Const REG_DWORD As Long = 4
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

注释:// Windows Error Messages
Private Const ERROR_NONE = 0
Private Const ERROR_BADDB = 1
Private Const ERROR_BADKEY = 2
Private Const ERROR_CANTOPEN = 3
Private Const ERROR_CANTREAD = 4
Private Const ERROR_CANTWRITE = 5
Private Const ERROR_NO_MORE_ITEMS = 259

注释:// Windows Security Messages
Private Const KEY_ALL_ACCESS = &H3F

注释:// Windows Registry API calls
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Private Declare Function RegCreateKeyEx _
  Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, _
  ByVal lpSubKey As String, _
  ByVal Reserved As Long, _
  ByVal lpClass As String, _
  ByVal dwOptions As Long, _
  ByVal samDesired As Long, _
  ByVal lpSecurityAttributes As Long, _
  phkResult As Long, _
  lpdwDisposition As Long) As Long

Private Declare Function RegOpenKeyEx _
  Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, _
  ByVal lpSubKey As String, _
  ByVal ulOptions As Long, _
  ByVal samDesired As Long, _
  phkResult As Long) As Long

Private Declare Function RegSetValueExString _
  Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, _
  ByVal lpValueName As String, _
  ByVal Reserved As Long, _
  ByVal dwType As Long, _
  ByVal lpValue As String, _
  ByVal cbData As Long) As Long

Private Declare Function RegSetValueExLong _
  Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, _
  ByVal lpValueName As String, _
  ByVal Reserved As Long, _
  ByVal dwType As Long, _
  lpValue As Long, _
  ByVal cbData As Long) As Long

Private Sub CreateAssociation()

  Dim sPath As String

  注释:// File Associations begin with a listing
  注释:// of the default extension under HKEY_CLASSES_ROOT.
  注释:// So the first step is to create that
  注释:// root extension item
  CreateNewKey ".xxx", HKEY_CLASSES_ROOT

  注释:// To the extension just added, add a
  注释:// subitem where the registry will look for
  注释:// commands relating to the .xxx extension
  注释:// ("MyApp.Document"). Its type is String (REG_SZ)
  SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ

  注释:// Create the 注释:MyApp.Document注释: item under
  注释:// HKEY_CLASSES_ROOT. This is where you注释:ll put
  注释:// the command line to execute or other shell
  注释:// statements necessary.
  CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT

  注释:// Set its default item to "MyApp Document".
  注释:// This is what is displayed in Explorer against
  注释:// for files with a xxx extension. Its type is
  注释:// String (REG_SZ)
  SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ

  注释:// Finally, add the path to myapp.exe
  注释:// Remember to add %1 as the final command
  注释:// parameter to assure the app opens the passed
  注释:// command line item.
  注释:// (results in 注释:"c:\LongPathname\Myapp.exe %1")
  注释:// Again, its type is string.
  sPath = "c:\LongPathname\Myapp.exe %1"
  SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ

  注释:// All done
  MsgBox "The file association has been made!"

End Sub

Private Function SetValueEx(ByVal hKey As Long, _
sValueName As String, lType As Long, _
vValue As Variant) As Long

  Dim nValue As Long
  Dim sValue As String

  Select Case lType
    Case REG_SZ
      sValue = vValue & Chr$(0)
      SetValueEx = RegSetValueExString(hKey, _
        sValueName, 0&, lType, sValue, Len(sValue))

    Case REG_DWORD
      nValue = vValue
      SetValueEx = RegSetValueExLong(hKey, sValueName, _
        0&, lType, nValue, 4)

  End Select
End Function

Private Sub CreateNewKey(sNewKeyName As String, _
  lPredefinedKey As Long)

  注释:// handle to the new key
  Dim hKey As Long
  注释:// result of the RegCreateKeyEx function
  Dim r As Long
  r = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
    vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hKey, r)

  Call RegCloseKey(hKey)

End Sub

Private Sub SetKeyValue(sKeyName As String, sValueName As String, _
vValueSetting As Variant, lValueType As Long)

  注释:// result of the SetValueEx function
  Dim r As Long
  注释:// handle of opened key
  Dim hKey As Long
  注释:// open the specified key
  r = RegOpenKeyEx(HKEY_CLASSES_ROOT, sKeyName, 0, _

  r = SetValueEx(hKey, sValueName, lValueType, vValueSetting)

  Call RegCloseKey(hKey)

End Sub

Private Sub cmdTest_Click()
End Sub