各位大哥:本人有一个几何作图程序,把所作的图用自己定义的文件*.pgl(二进制)保存,可是别人安装后,不能直接打开*.pgl文件(比如我们双击test.doc文件,Word会自动打开,并显示此test.doc文件内容)。我模仿别人的软件,更改注册表里的东西,但还是不行的。

解决方案 »

  1.   

    flyahead0(苯鸟先飞),您好,您能不能详细谈谈呢
      

  2.   

    '打开文件关联的例子Const ERROR_SUCCESS = 0&
    Const REG_SZ = 1
    Const HKEY_CLASSES_ROOT = &H80000000
    Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, lpdwType As Long, lpbData As Any, cbData As Long) As Long
    Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, phkResult As Long) As Long
    Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As LongPublic Function RegSetStringValue(ByVal hKey As Long, ByVal strValueName As String, _
        ByVal strData As String, Optional ByVal fLog) As Boolean
         Dim lResult As Long
         On Error GoTo 0
         lResult = RegSetValueEx(hKey, strValueName, 0&, REG_SZ, ByVal strData, _
            LenB(StrConv(strData, vbFromUnicode)) + 1)
         If lResult = 0 Then
            RegSetStringValue = True
         Else
            RegSetStringValue = False
         End If
    End FunctionPublic Function StripTerminator(ByVal strString As String) As String
         Dim intZeroPos As Integer
         intZeroPos = InStr(strString, Chr$(0))
         If intZeroPos > 0 Then
            StripTerminator = Left$(strString, intZeroPos - 1)
         Else
            StripTerminator = strString
         End If
    End FunctionPublic Sub SetFileType(ByVal strFileType As String, ExePathFile As String)
        Dim Length As Integer
        
        Dim KeyId As Long
        Call RegCreateKey(HKEY_CLASSES_ROOT, "." & strFileType, KeyId)
        Call RegSetValueEx(KeyId, "", 0&, REG_SZ, ByVal strFileType & "file", Len(strFileType & "file") + 1)
        Dim KeyId1 As Long
        Call RegCreateKey(HKEY_CLASSES_ROOT, strFileType & "file", KeyId1)
        Call RegSetValueEx(KeyId1, "", 0&, REG_SZ, ByVal strFileType & "类型", LenB(strFileType & "类型") + 1)
        Dim KeyId2 As Long
        Call RegCreateKey(KeyId1, "DefaultIcon", KeyId2)
        Call RegSetValueEx(KeyId2, "", 0&, REG_SZ, ByVal ExePathFile & ",0", Len(ExePathFile & ",0") + 1)
        Dim KeyId3 As Long
        Call RegCreateKey(KeyId1, "Shell", KeyId3)
        Dim KeyId4 As Long
        Call RegCreateKey(KeyId3, "Open", KeyId4)
        Dim KeyId5 As Long
        Call RegCreateKey(KeyId4, "command", KeyId5)
        Call RegSetValueEx(KeyId5, "", 0&, REG_SZ, ByVal ExePathFile & " %1", Len(ExePathFile & " %1") + 1)
        'MsgBox "创建自定义类型后缀名成功。", vbInformation, "系统提示"
    End Sub用法:
    Call SetFileType("csp", strEXEPathFile)
    “csp”是文件后缀名。
    strEXEPathFile 是打开这个后缀名的 Exe 文件完整路径。
      

  3.   

    在注册表中文件关系是要,还有一点很多地方都没有讲.
    你的程序要知道打开的是哪个文件,就要在Form_Load 中分析 Command 对象的值.
      

  4.   

    Public Sub SetFileType(ByVal strFileType As String, ExePathFile As String)
        Dim Length As Integer
        
        Dim KeyId As Long
        Call RegCreateKey(HKEY_CLASSES_ROOT, "." & strFileType, KeyId)
        Call RegSetValueEx(KeyId, "", 0&, REG_SZ, ByVal strFileType & "file", Len(strFileType & "file") + 1)
        Dim KeyId1 As Long
        Call RegCreateKey(HKEY_CLASSES_ROOT, strFileType & "file", KeyId1)
        Call RegSetValueEx(KeyId1, "", 0&, REG_SZ, ByVal strFileType & "类型", LenB(strFileType & "类型") + 1)
        Dim KeyId2 As Long
        Call RegCreateKey(KeyId1, "DefaultIcon", KeyId2)
        Call RegSetValueEx(KeyId2, "", 0&, REG_SZ, ByVal ExePathFile & ",0", Len(ExePathFile & ",0") + 1)
        Dim KeyId3 As Long
        Call RegCreateKey(KeyId1, "Shell", KeyId3)
        Dim KeyId4 As Long
        Call RegCreateKey(KeyId3, "Open", KeyId4)
        Dim KeyId5 As Long
        Call RegCreateKey(KeyId4, "command", KeyId5)
        Call RegSetValueEx(KeyId5, "", 0&, REG_SZ, ByVal ExePathFile & " %1", Len(ExePathFile & " %1") + 1)
        'MsgBox "创建自定义类型后缀名成功。", vbInformation, "系统提示"
    End Sub
    good!!!
      

  5.   


    文件扩展名建立关联如何使你的程序同文件扩展名建立关联? 
    ------------------------------------------------------------------------ 
    -------- 
    如果你的程序是同文件操作有关,比如说你自定义了一个文件类型,用你的程序才 
    能打开它并查看其中的内容。你有没有想过在你的文件同你的程序之间建立关联, 
    这样不必每次都要先启动程序,再打开文件了。用户可直接点选你的文件,而不必 
    每次都要面对一个“打开方式”对话框。其实要做到这一点,只须调用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