100分悬赏解决一个问题。请进吧:)

wgku 2002-02-07 03:35:52
问题是这样的,我做了个自定义格式的文件,后缀名为*.dzd。而我想双击它时用自已做的程序打开它。这个程序是dszd.exe,我想把*.dzd文件的图标也改得和它一样。我要怎么做??还有,如果我的程序第一次在别的机子上运行,是不是要在注册表里添加一些什么才能实现这种关系,而下次启动时要判断一下是否已建立关联??。关键是我不知道要怎么做,和在注册表里要添加什么,望大家来帮帮忙:)
...全文
42 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyl910 2002-02-07
  • 打赏
  • 举报
回复
VB操作注册表:
http://www.sqreg.com/file/vb/reg_01.htm
http://www.sqreg.com/file/vb/reg_02.htm
http://www.sqreg.com/file/vb/reg_03.htm
http://www.sqreg.com/file/vb/reg_04.htm
http://www.sqreg.com/file/vb/reg_05.htm
http://www.sqreg.com/file/vb/reg_06.htm
http://www.sqreg.com/file/vb/reg_07.htm
zyl910 2002-02-07
  • 打赏
  • 举报
回复
VB操作注册表:
http://www.sqreg.com/file/vb/reg_01.htm
http://www.sqreg.com/file/vb/reg_02.htm
http://www.sqreg.com/file/vb/reg_03.htm
http://www.sqreg.com/file/vb/reg_04.htm
http://www.sqreg.com/file/vb/reg_05.htm
http://www.sqreg.com/file/vb/reg_06.htm
http://www.sqreg.com/file/vb/reg_07.htm
wgku 2002-02-07
  • 打赏
  • 举报
回复
大家刚才所说的都可以用API函数中的有关注册表的来实现吧。。。。。
可上面的程序。。。。我都看花眼了。。。@_*
给个解释、注释先?
RegCreateKey '这是创建一个新的键吧
RegSetValue '那这个呢??
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&
dengwei007 2002-02-07
  • 打赏
  • 举报
回复
研究一下注册表中的HKEY_CLASSES_ROOT吧

举个例子:后缀名为bas的文件,应该有:
HKEY_CLASSES_ROOT\.bas
HKEY_CLASSES_ROOT\.bas\shell
HKEY_CLASSES_ROOT\.bas\shell \open
HKEY_CLASSES_ROOT\.bas\shell \open \command

  此层次结构说明了bas子键与command子键所指定的应用程序建立了关联。在command子键中还有如下设置:#@=E:\VB\vb.exe %1 也就是说,bas文件后缀与E:\VB\vb.exe关联。

再加一招:
在ShellNew子键下面加一个设置:

#NullFile=:将NullFile设置为空串,以便能将.bas文件扩展名添加到系统的新建菜单中。

zyl910 2002-02-07
  • 打赏
  • 举报
回复
在HKEY_CLASSES_ROOT\dzd_File\DefaultIcon下“默认”值键的值设为“你的程序的位置,0”

0表示 使用程序的资源文件的第0个图标!
*注:一般情况下,程序的资源文件的第0个图标 为程序图标!
wgku 2002-02-07
  • 打赏
  • 举报
回复
问:zyl910(910:分儿,我来了!) ,可以用程序建立与图标的关联吧??但是图标包括在程序中了,怎么做呢??
zyl910 2002-02-07
  • 打赏
  • 举报
回复
图标:
在HKEY_CLASSES_ROOT\dzd_File下建立“DefaultIcon”主键
在HKEY_CLASSES_ROOT\dzd_File\DefaultIcon下“默认”值键的值设为“你的程序的位置,0”
ferrytang 2002-02-07
  • 打赏
  • 举报
回复
如何使你的程序同文件扩展名建立关联?

如果你的程序是同文件操作有关,比如说你自定义了一个文件类型,用你的程序才
能打开它并查看其中的内容。你有没有想过在你的文件同你的程序之间建立关联,
这样不必每次都要先启动程序,再打开文件了。用户可直接点选你的文件,而不必
每次都要面对一个“打开方式”对话框。其实要做到这一点,只须调用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:mydirmy.exe %1"
ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
ret& = RegSetValue&(lphKey&, "shellopencommand", REG_SZ, _
sKeyValue, MAX_PATH)
End Sub
按F5运行程序并在窗体上点击一下鼠标,然后退出程序.


从开始菜单中运行REGEDIT,你可在HKEY_CLASSES_ROOT下找到.bar和MyApp两个子
项,结构如下所示:

.bar = MyApp
MyApp = My Application
|
-- Shell
|
-- open
|
-- command = c:mydirmy.exe %1

40Star 2002-02-07
  • 打赏
  • 举报
回复
HKEY_CLASSES_ROOT
下的都是注册的扩展名的执行方式
ferrytang 2002-02-07
  • 打赏
  • 举报
回复
如何使你的程序同文件扩展名建立关联?

如果你的程序是同文件操作有关,比如说你自定义了一个文件类型,用你的程序才
能打开它并查看其中的内容。你有没有想过在你的文件同你的程序之间建立关联,
这样不必每次都要先启动程序,再打开文件了。用户可直接点选你的文件,而不必
每次都要面对一个“打开方式”对话框。其实要做到这一点,只须调用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:mydirmy.exe %1"
ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
ret& = RegSetValue&(lphKey&, "shellopencommand", REG_SZ, _
sKeyValue, MAX_PATH)
End Sub
按F5运行程序并在窗体上点击一下鼠标,然后退出程序.


从开始菜单中运行REGEDIT,你可在HKEY_CLASSES_ROOT下找到.bar和MyApp两个子
项,结构如下所示:

.bar = MyApp
MyApp = My Application
|
-- Shell
|
-- open
|
-- command = c:mydirmy.exe %1

lihanbing 2002-02-07
  • 打赏
  • 举报
回复
REGEDIT
; This .REG file may be used by your SETUP program.
; If a SETUP program is not available, the entries below will be
; registered in your InitInstance automatically with a call to
; CWinApp::RegisterShellFileTypes and COleObjectFactory::UpdateRegistryAll.

HKEY_CLASSES_ROOT\.dzd = DSZD.Document
HKEY_CLASSES_ROOT\DSZD.Document\shell\open\command = DSZD.EXE %1
HKEY_CLASSES_ROOT\DSZD.Document\shell\open\ddeexec = [open("%1")]
HKEY_CLASSES_ROOT\DSZD.Document\shell\open\ddeexec\application = DSZD
; note: the application is optional
; (it defaults to the app name in "command")

HKEY_CLASSES_ROOT\DSZD.Document = DSZD Document
zyl910 2002-02-07
  • 打赏
  • 举报
回复
在HKEY_CLASSES_ROOT下建立“.dzd”主键
将其中的“默认”值键的值设为“dzd_File”

在HKEY_CLASSES_ROOT下建立“dzd_File”主键
在HKEY_CLASSES_ROOT\dzd_File下建立“Shell”主键
在HKEY_CLASSES_ROOT\dzd_File\Shell下建立“open”主键
在HKEY_CLASSES_ROOT\dzd_File\Shell\open下“默认”值键的值设为“打开”
在HKEY_CLASSES_ROOT\dzd_File\Shell\open下建立“Command”主键
在HKEY_CLASSES_ROOT\dzd_File\Shell\open\Command下“默认”值键的值设为“你的程序名 "%1"”
ferrytang 2002-02-07
  • 打赏
  • 举报
回复
如何在Windows操作系统中改变文件打开方式

(合肥 张建军 滕明贵 韩莹)

  在Windows 95/NT/98操作系统中改变文件打开方式的问题,又可称为改变文件类型关联的问题,即把某类型(扩展名)的文件与某应用程序关联,例如通常当双击*.txt文件时系统自动调用Notepad.exe。本文介绍利用Windows注册表编辑器Regedit.exe手工或编程改变文件打开方式的方法,并提供程序实例。
  一、基本思路:
  1、注册表编辑器Regedit.exe是用于更改系统注册表设置的高级工具,包含了关于系统配置及运行的重要信息,默认访问路径为C:WindowsRegedit.exe。双击Regedit.exe图标,运行注册表编辑器。在左侧显示栏内看到HKEY_CLASSES_ROOT、KEY_CURRENT_USER、HKEY_LOCAL_MACHINE等主键。与文件类型有关的所有主键、键名、键值都存放在HKEY_CLASSES_ROOT下。
  ◆双击HKEY_CLASSES_ROOT,向下拖动滚动条,找到.txt主键,右侧显示栏内“txtfile”说明:在HKEY_CLASSES_ROOT下有一txtfile主键,其下存放了打开*.txt文件应用程序的有关信息。
  ◆向下拖动滚动条,找到txtfile主键,右侧显示栏内“文本文档”为文件类型描述。双击txtfile,DefaultIcon右侧显示栏内“shell32.dll,-152”为*.txt文件的图标;shellopencommand,右侧显示栏内“C:WINDOWSNOTEPAD.EXE %1”为打开*.txt文件的应用程序名称及参数。  改变打开文件方式的方法(例如用VISIO打开*.exc文件):
  ◆手工:打开系统注册表,在HKEY_CLASSES_ROOT下找到.exc及另一主键名,找到此主键,将shellopencommand右侧显示栏内“C:WINDOWSNOTEPAD.EXE %1”改为“C:VISIO.EXE %1”(假设VISIO.EXE的访问路径是C:,具体视情况而定),按F5刷新系统注册表。
  ◆编程:利用VB、Delphi、C++Builder等读写系统注册表,可自动改变文件打开方式。本文提供VB、Delphi编程实例。
  二、编程实例:
  ㈠利用VB编程
  1、在VB5.0 IDE中,新建工程Project1,在Form1上添加命令按钮Command1。
  2、选择菜单“工程”—“添加模块”—“模块”—“打开”,在Project1中添加模块Moudle1。
  3、在Moudle1“通用—声明”部分声明API函数和常量。
  Const REG_SZ = 1
  Global Const HKEY_CLASSES_ROOT = &H80000000
Declare Function OSRegQueryValueEx 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 OSRegOpenKey Lib “advapi32”Alias
“RegOpenKeyA”(ByVal hKey As Long, ByVal lpszSubKey As String,
phkResult As Long) As Long

  Declare Function OSRegSetValueEx Lib“advapi32”Alias
“RegSetValueExA”(ByVal hKey As Long, ByVal lpszValueName As String,
ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As Any,
ByVal cbData As Long) As Long

  Declare Function OSRegCloseKey Lib“advapi32”Alias
“RegCloseKey”(ByVal hKey As Long) As Long

  4、在Moudle 1中编写函数。
  Function RegOpenKey(ByVal hKey As Long, ByVal lpszSubKey As String,
phkResult As Long) As Boolean
   Dim lResult As Long
   On Error GoTo 0 ` 关闭错误陷阱
   lResult = OSRegOpenKey(hKey, lpszSubKey, phkResult)
   If lResult = 0 Then
   RegOpenKey = True
   Else
   RegOpenKey = False
   End If
  End Function
  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 = OSRegSetValueEx(hKey, strValueName, 0&, REG_SZ, ByVal strData,
LenB(StrConv(strData, vbFromUnicode)) + 1)
   If lResult = 0 Then
   RegSetStringValue = True
   Else
   RegSetStringValue = False
   End If
  End Function
  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 Function
  Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String,
strData As String) As Boolean
   Dim lResult As Long
   Dim lValueType As Long
   Dim strBuf As String
   Dim lDataBufSize As Long
   RegQueryStringValue = False
   On Error GoTo 0
   lResult = OSRegQueryValueEx(hKey, strValueName, 0&, lValueType, ByVal 0&,
lDataBufSize)
   If lResult = ERROR_SUCCESS Then
   If lValueType = REG_SZ Then
   strBuf = String(lDataBufSize, “”)
   lResult = OSRegQueryValueEx(hKey, strValueName, 0&, 0&, ByVal strBuf,
lDataBufSize)
   If lResult = ERROR_SUCCESS Then
   RegQueryStringValue = True
   strData = StripTerminator(strBuf)
   End If
   End If
   End If
  End Function
  5、双击Command1,编写Click事件代码。
  Private Sub Command1_Click()
   Dim hKey As Long
   Dim MyReturn As Long
   Dim MyData As String
   MyReturn = OSRegOpenKey(HKEY_CLASSES_ROOT, “.exc”, hKey)
  MyReturn=RegQueryStringValue(hKey,“”,MyData)
  MyReturn=OSRegOpenKey(HKEY_CLASSES_ROOT, MyData+“shellopencommand”,hKey)
   MyReturn = RegSetStringValue(hKey,“”,“c:visio.exe 1%”, False)
   If MyReturn Then
   MsgBox “改变文件打开方式成功!”,vbInformation,“请注意”
   Else
   MsgBox “改变文件打开方式失败!”,vbExclamation,“请注意”
   End If
   OSRegCloseKey (hKey)
  End Sub
  6、按F5运行程序,在简体中文Windows95/NT/98、VB5.0/6.0环境中调试通过。
  ㈡利用Delphi编程
  1、在Delphi3.0 IDE中,新建工程Project1,在Form1上添加按钮Button1。
  2、在uses子句中添加Registry。
  3、双击Button1,编写Click事件代码。
  procedure TForm1.Button1Click(Sender: TObject);
  var
   MyRegistry : TRegINIFile;
   Return:string;
  begin
   try
   MyRegistry := TRegINIFile.Create(``);
  MyRegistry.RootKey := HKEY_CLASSES_ROOT;
   Return:=MyRegistry.ReadString (`.gid`,``,`No! Not Found the Key!`);
   MyRegistry.WriteString(Return,``,`这只是一个演示!`);
   MyRegistry.WriteString(Return+`DefaultIcon`,``,`c:visio.exe,1`);
   MyRegistry.WriteString(Return+`shellopencommand`,``,`c:visio.exe %1`);
   finally
   MyRegistry.Free;
   end;
   ShowMessage(`改变文件打开方式成功!`);
  end;
  4、按F9运行程序,在简体中文Windows95/NT/98、VB5.0环境中调试通过。
ColderRain 2002-02-07
  • 打赏
  • 举报
回复
好象是改"CLASS_ROOT"键值,具体我忘了,在VC里好象实现这个很容易.
关注!
wgku 2002-02-07
  • 打赏
  • 举报
回复
?????为什么没人理呢???

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧