|
之前已经收集了CAD各版本在注册表中的位置,见:http://hi.baidu.com/kakanimo/blog/item/36e9abee02e4e211fcfa3c53.html,那搜索出本机上安装的AutoCAD版本只需要查看注册表信息就可以了,方法如下: 大致思路是:1.搜索注册表各CAD版本对应的位置是否存在,2.如果存在就读取相应项下面的存放CAD运行程序位置的键值是否存在.(注:第2步一定要,因为有时候安装后卸载后,CAD版本对应的位置是存在的。所以要通过读取项里面的键值进一步判断)。
c#实现见http://hi.baidu.com/kakanimo/blog/item/1a80a80e4e2550e1aa64577e.html
那就需要用到两个API函数用来读取注册表,如下:一个读取项,一个读取键值。
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
'注意以下的函数声明须在一行内写完
''''//注册表基本键值列表
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_CURRENT_CONFIG = &H80000005
''''键值类型常用取值
Public Const REG_NONE = 0
Public Const REG_SZ = 1 ' -->字符串
Public Const REG_EXPAND_SZ = 2 ' -->可展开式字符串
Public Const REG_BINARY = 3 ' -->Binary数据
Public Const REG_DWORD = 4 ' -->长整数
Public Const REG_DWORD_BIG_ENDIAN = 5 ' -->BIG_ENDIAN长整数
Public Const REG_MULTI_SZ = 7 '-->多重字符串
Dim hKey As Long
声明之后就是用这两个API判断啦。如下。写了个函数,先是判断注册项是否存在
'判断注册项是否存在
Public Function IsSubKeyName(RootKey As Long, SubKeyName As String, Optional hKey As Long) As Boolean
If RegOpenKey(RootKey, SubKeyName, hKey) = 0& Then
IsSubKeyName = True
Else
IsSubKeyName = False
End If
End Function
接下来是判断注册项下是否有相对应的键值:
Public Function SearchCADversion(Location As String)
Dim Name As String * 255
Dim lngTypeData As Long '返回注册表值的数据类型
Dim intname As String '文件名所在的位置
Dim Being As Boolean
If IsSubKeyName(HKEY_LOCAL_MACHINE, Location, hKey) Then '判断是否存在项目
RegQueryValueEx hKey, "AcadLocation", 0&, lngTypeData, ByVal Name, Len(Name) '读到"AcadLocation"下数据
intname = left(Name, InStr(Name, Chr(0)) - 1)
If Len(intname) = 0 Then '如果存在这个大项目,但是里面AcadLocation没有,就表示可能卸载过,也没有安装这个版本的CAD
Being = False
Else '即存在大项,又有程序位置,说明存在这个版本的CAD
Being = True
End If
Else '注册项不存在,表示没有这个版本的CAD
Being = False
End If
SearchCADversion = Being
End Function
看上面,里面的注释已经很详细了,通过传入的注册表位置Location判断是否有指定的CAD版本。返回值为False、True来判断。
核心的就是上面的东西了,接下来,就是界面,如图,让窗体启动的时候就显示是否有某版本的CAD,在load中写如下:
Private Sub Form_Load()
Dim Location(13) As String, i As Integer
Info = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Location(0) = "SOFTWARE\Autodesk\AutoCAD\R15.0\ACAD-1:804" '2002中文版
Location(1) = "SOFTWARE\Autodesk\AutoCAD\R15.0\ACAD-1:409" '2002英文版
Location(2) = "SOFTWARE\Autodesk\AutoCAD\R16.0\ACAD-201:804" '2004中文版
Location(3) = "SOFTWARE\Autodesk\AutoCAD\R16.0\ACAD-201:409" '2004英文版
Location(4) = "SOFTWARE\Autodesk\AutoCAD\R16.1\ACAD-301:804" '2005中文版
Location(5) = "SOFTWARE\Autodesk\AutoCAD\R16.1\ACAD-301:409" '2005英文版
Location(6) = "SOFTWARE\Autodesk\AutoCAD\R16.2\ACAD-4001:804" '2006中文版
Location(7) = "SOFTWARE\Autodesk\AutoCAD\R16.2\ACAD-4001:409" '2006英文版
Location(8) = "SOFTWARE\Autodesk\AutoCAD\R17.0\ACAD-5001:804" '2007中文版
Location(9) = "SOFTWARE\Autodesk\AutoCAD\R17.0\ACAD-5001:409" '2007英文版
Location(10) = "SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:804" '2008中文版
Location(11) = "SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409" '2008英文版
Location(12) = "SOFTWARE\Autodesk\AutoCAD\R17.2\ACAD-7001:804" '2009中文版
Location(13) = "SOFTWARE\Autodesk\AutoCAD\R17.2\ACAD-7001:409" '2009英文版
For i = 0 To 13
Info(i) = SearchCADversion(Location(i))
Option1(i).Enabled = Info(i)
Next
End Sub
如果存在某版本,就和某版本对应的option.Enabled =True.OK啦,就这样了,上图。搜索出我电脑中的CAD版本:
搜索出我电脑上安装了2002中文版和2006中文版。哈哈,到次结束。那搜索出来了,怎么用指定的CAD版本来画图呢?见:http://hi.baidu.com/kakanimo/blog/item/9126bfdf53fc475fccbf1ad2.html
|
|