{"id":1098,"date":"2024-08-01T08:00:55","date_gmt":"2024-08-01T00:00:55","guid":{"rendered":"http:\/\/www.wyrcad.com\/?p=1098"},"modified":"2024-07-15T15:00:52","modified_gmt":"2024-07-15T07:00:52","slug":"%e5%88%a9%e7%94%a8%e6%b3%a8%e5%86%8c%e8%a1%a8registry%e6%b3%a8%e5%86%8c%e6%96%b0%e6%96%87%e4%bb%b6%e7%b1%bb%e5%9e%8b%e5%b9%b6%e6%8c%87%e5%ae%9a%e5%ae%bf%e4%b8%bb%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=1098","title":{"rendered":"\u5229\u7528\u6ce8\u518c\u8868(Registry)\u6ce8\u518c\u65b0\u6587\u4ef6\u7c7b\u578b\u5e76\u6307\u5b9a\u9ed8\u8ba4\u6253\u5f00\u7a0b\u5e8f"},"content":{"rendered":"\n<p>Private Declare Function FindExecutable Lib &#8220;shell32.dll&#8221; Alias &#8220;FindExecutableA&#8221; (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long<\/p>\n\n\n\n<p>Public Sub Associate(ByVal FileExtend As String, ByVal FileType As String, ByVal FileDescribe As String, ByVal FileIcon As String, ByVal ProgramName As String) &#8216;\u6ce8\u518c\u6587\u4ef6\u7c7b\u578b,\u5e76\u8bbe\u7f6e\u5728\u6d4f\u89c8\u5668\u53cc\u51fb\u6253\u5f00\u6587\u4ef6<br>&#8216;\u7b2c\u56db\u4f4d\u53c2\u6570\u8bf4\u660e<br>&#8216;&#8221;C:\\WINDOWS\\SYSTEM32\\shell32.dll,-151&#8243;\uff0c\u8f6f\u4ef6\u6307\u5b9a\u56fe\u6807<br>&#8216;ProgramName\uff0c\u5e94\u7528\u8f6f\u4ef6\u56fe\u6807<br>&#8216;&#8221;%1&#8243;\uff0c\u7cfb\u7edf\u6307\u5b9a\u56fe\u6807<br>On Error Resume Next<br>Dim hkey As Long, ReturnVal As Long<br>ReturnVal = <a href=\"https:\/\/www.wyrcad.com\/?p=1026\">RegCreateKeyEx<\/a>(HKEY_CLASSES_ROOT, FileExtend, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)<br>If ReturnVal = 0 Then<br><a href=\"https:\/\/www.wyrcad.com\/?p=1026\">RegSetValueEx<\/a> hkey, vbNullString, 0, 1, FileType, LenB(StrConv(FileType, vbFromUnicode)) &#8216;Len(FileType) \u6c49\u5b57\u65f6\u4e0d\u51c6<br><a href=\"https:\/\/www.wyrcad.com\/?p=1026\">RegCloseKey<\/a> hkey<br>End If<br>ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)<br>If ReturnVal = 0 Then<br>RegSetValueEx hkey, vbNullString, 0, 1, FileDescribe, LenB(StrConv(FileDescribe, vbFromUnicode)) &#8216;Len(FileDescribe) \u6c49\u5b57\u65f6\u4e0d\u51c6<br>RegCloseKey hkey<br>End If<br>ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType &amp; &#8220;\\DefaultIcon&#8221;, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)<br>If ReturnVal = 0 Then<br>RegSetValueEx hkey, vbNullString, 0, 1, FileIcon, LenB(StrConv(FileIcon, vbFromUnicode)) &#8216;Len(FileIcon) \u6c49\u5b57\u65f6\u4e0d\u51c6<br>RegCloseKey hkey<br>End If<br>ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType &amp; &#8220;\\Shell\\Open\\Command&#8221;, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)<br>If ReturnVal = 0 Then<br>ProgramName = ProgramName &amp; Space$(1) &amp; &#8220;\/open %1&#8221;<br>RegSetValueEx hkey, vbNullString, 0, 1, ProgramName, LenB(StrConv(ProgramName, vbFromUnicode)) &#8216;Len(ProgramName) \u6c49\u5b57\u65f6\u4e0d\u51c6<br>RegCloseKey hkey<br>End If<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>Public Sub DisAssociate(ByVal FileExtend As String, ByVal FileType As String) &#8216;\u89e3\u9664\u5728\u6d4f\u89c8\u5668\u53cc\u51fb\u6253\u5f00\u6587\u4ef6<br><a href=\"https:\/\/www.wyrcad.com\/?p=1026\">DelKey<\/a> HKEY_CLASSES_ROOT, FileExtend<br>DelKey HKEY_CLASSES_ROOT, FileType<br><a href=\"https:\/\/www.wyrcad.com\/?p=1026\">SHDeleteKey<\/a> HKEY_CLASSES_ROOT, FileType &#8216;\u5220\u9664\u542b\u6709\u5b50\u9879\u7684\u9879<br>End Sub<\/p>\n\n\n\n<p>Public Sub SetRightMenu(ByVal sMenuShow As String, ByVal sProgram As String) &#8216;\u8bbe\u7f6e\u5728\u7cfb\u7edf\u53f3\u952e\u83dc\u5355\u6253\u5f00\u6587\u4ef6<br>Call <a href=\"https:\/\/www.wyrcad.com\/?p=1026\">SaveString<\/a>(HKEY_CLASSES_ROOT, &#8220;*\\shell\\&#8221; &amp; sMenuShow &amp; &#8220;\\command&#8221;, &#8220;&#8221;, sProgram &amp; Space$(1) &amp; &#8220;%1&#8243;) &#8216;&#8221;&#8221; \u4ee3\u8868\u5b50\u9879 command \u7684\u5b57\u7b26\u4e32\u503c(\u9ed8\u8ba4)<br>End Sub<\/p>\n\n\n\n<p>Public Sub DisRightMenu(ByVal sMenuShow As String) &#8216;\u89e3\u9664\u5728\u7cfb\u7edf\u53f3\u952e\u83dc\u5355\u6253\u5f00\u6587\u4ef6<br>SHDeleteKey HKEY_CLASSES_ROOT, sMenuShow &#8216;\u5220\u9664\u542b\u6709\u5b50\u9879\u7684\u9879<br>End Sub<\/p>\n\n\n\n<p>Public Function GetAssociatedProgram(ByVal sExtension As String) As String &#8216;\u6839\u636e\u6269\u5c55\u540d\u67e5\u627e\u9ed8\u8ba4\u6253\u5f00\u7a0b\u5e8f<br>GetAssociatedProgram = &#8220;&#8221;<br>&#8216;If LCase(sExtension) = &#8220;dwg&#8221; Then<br>&#8216;GetAssociatedProgram = Trim(GetString(HKEY_CLASSES_ROOT, &#8220;AutoCAD\\Shell\\Open\\command&#8221;, vbNullString))<br>&#8216;If GetAssociatedProgram = &#8220;&#8221; Then GetAssociatedProgram = Trim(GetString(HKEY_LOCAL_MACHINE, &#8220;SOFTWARE\\Classes\\autocad\\shell\\open\\command&#8221;, vbNullString)) &#8216;Belt and braces<br>&#8216;Exit Function<br>&#8216;End If<br>&#8216; Reading the host program of DWG from the registry.<br>Dim temp_title As String<br>Dim temp_path As String<br>Dim fnum As Integer<br>Dim result As String<br>Dim pos As Integer<br>&#8216; Get a temporary file name with this extension.<br>GetTempFile IIf(LCase(sExtension) = &#8220;dwg&#8221;, &#8220;dxf&#8221;, sExtension), temp_path, temp_title<br>&#8216; Findexecutable cannot find the host program of DWG. The host programs of DXF and DWT can be found, and they are the same host program as DWG.<br>&#8216; Make the file.<br>fnum = FreeFile<br>Open temp_path &amp; temp_title For Output As fnum<br>Close fnum<br>&#8216; Get the associated executable.<br>result = Space$(1024)<br>FindExecutable temp_title, temp_path, result<br>pos = InStr(result, Chr$(0))<br>GetAssociatedProgram = Left$(result, pos &#8211; 1)<br>&#8216; Delete the temporary file.<br>Kill temp_path &amp; temp_title<br>End Function<\/p>\n\n\n\n<p>Private Sub GetTempFile(ByVal sExtension As String, ByRef temp_path As String, ByRef temp_title As String)&#8217; Return a temporary file name.<br>Dim I As Integer<br>If Left$(sExtension, 1) &lt;&gt; &#8220;.&#8221; Then sExtension = &#8220;.&#8221; &amp; sExtension<br>temp_path = Environ(&#8220;TEMP&#8221;)<br>If Right$(temp_path, 1) &lt;&gt; &#8220;\\&#8221; Then temp_path = temp_path &amp; &#8220;\\&#8221;<br>I = 0<br>Do<br>temp_title = &#8220;tmp&#8221; &amp; Format$(I) &amp; sExtension<br>If Len(Dir$(temp_path &amp; temp_title)) = 0 Then Exit Do<br>I = I + 1<br>Loop<br>End Sub<\/p>\n\n\n\n<p>&#8216;\u6548\u679c<br>Public Sub Main()<br>On Error Resume Next<br>Dim OptFile As String &#8216;\u6240\u9009\u6587\u4ef6\u7684\u540d\u79f0<br>If Len(Command) = 0 Then &#8216;\u70b9\u51fb\u8f6f\u4ef6\u672c\u8eab\u542f\u52a8 Command = &#8220;&#8221;<br>&#8216;GO ON<br>Else<br>If LCase(Left(Command, 6)) = &#8220;\/open&#8221; &amp; Space$(1) Then &#8216;\u6d4f\u89c8\u5668\u53cc\u51fb\u542f\u52a8 Command = &#8220;\/open D:\\Test.iso&#8221;<br>OptFile = Right(Command, Len(Command) &#8211; 6)<br>Else &#8216;\u7cfb\u7edf\u53f3\u952e\u83dc\u5355\u542f\u52a8 Command = &#8220;D:\\Test.iso&#8221;<br>OptFile = Command<br>End If<br>End If<br>&#8216;GO ON<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>&#8216;\u8c03\u7528<br>Sub SetFile()<br>Dim sProgram As String<br>sProgram = &#8220;C:\\Windows\\notepad.exe&#8221;<br>Dim sShow As String<br>sShow = &#8220;Open With notepad&#8221;<br>Call Associate(&#8220;.iso&#8221;, &#8220;ISO File&#8221;, &#8220;&#8221;, &#8220;%1&#8221;, sProgram) &#8216;\u6ce8\u518c\u540e\u7f00\u4e3a iso \u7684\u6587\u4ef6,\u5e76\u786e\u5b9a\u5176\u9ed8\u8ba4\u6253\u5f00\u7a0b\u5e8f\u4e3a sProgram<br>Call SetRightMenu(sShow, sProgram) &#8216;\u5728\u7cfb\u7edf\u53f3\u952e\u83dc\u5355\u589e\u52a0\u4e00\u9879\u663e\u793a\u5b57\u7b26\u4e3ashow,\u5e76\u6307\u5b9a\u7531sProgram\u6253\u5f00\u6240\u9009\u6587\u4ef6<br>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Private Declare Function FindExecutable Lib &#8220;shel &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=1098\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5229\u7528\u6ce8\u518c\u8868(Registry)\u6ce8\u518c\u65b0\u6587\u4ef6\u7c7b\u578b\u5e76\u6307\u5b9a\u9ed8\u8ba4\u6253\u5f00\u7a0b\u5e8f\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-1098","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1098"}],"version-history":[{"count":10,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1098\/revisions"}],"predecessor-version":[{"id":1178,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1098\/revisions\/1178"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}