<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=150496&amp;view=findpost&amp;p=1205666</guid>
        <pubDate>Sat, 05 Aug 2006 14:16:18 +0000</pubDate>
        <title>Работа с ShellExecute</title>
        <link>https://forum.sources.ru/index.php?showtopic=150496&amp;view=findpost&amp;p=1205666</link>
        <description><![CDATA[Djoser: 1.Как открыть зарегистрированный тип файла в системе в нужной программе?<br>
2.Как открыть адрес URL в браузере по умолчанию?<br>
3.Как открыть окно почтовой программы для создания письма на определенный адрес? (можно отнести ко второму)<br>
4.Как распечатать на принтере содержимое файла?<br>
5.Как отобразить стандартное окно поиска файлов Windows? <br>
<br>
Код:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&#39;Пример демонстрирует основные возможности применения функции ShellExecute</div><div class="code_line">&nbsp;</div><div class="code_line">&#39;Пример от Djoser</div><div class="code_line">&#39;Части кода и редакция: B.V.; Api-Guide</div><div class="code_line">Option Explicit</div><div class="code_line">&nbsp;</div><div class="code_line">Private Declare Function FormatMessage Lib &quot;kernel32.dll&quot; Alias &quot;FormatMessageA&quot; (ByVal dwFlags As Long, ByRef lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, ByRef Arguments As Long) As Long</div><div class="code_line">Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long</div><div class="code_line">&nbsp;</div><div class="code_line">Private Const SW_SHOWNORMAL As Long = 1</div><div class="code_line">Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &amp;H1000</div><div class="code_line">Private Const MAX_SIZE As Long = 255</div><div class="code_line">&nbsp;</div><div class="code_line">Public Enum ShellOperations</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_Mail = 1</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_Explore = 2</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_FindIn = 3</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_Edit = 4</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_URL = 5</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_Run = 6</div><div class="code_line">&nbsp;&nbsp; &nbsp;SO_Print = 7</div><div class="code_line">End Enum</div><div class="code_line">&nbsp;</div><div class="code_line">Public Function iShell(ByVal hWnd As Long, ByRef Action As ShellOperations, ByVal ActString As String, Optional ByVal strCmdLine As String = vbNullString) As String</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;функция возвращает пустую строку, если все нормально</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;функция возвращает описание ошибки, если произошла ошибка</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;В lRetval должен быть возвращен Instance запущенного приложения</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;Если значение меньше или равно 32 - произошла ошибка</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;Chr$(92) = &quot;\&quot;</div><div class="code_line">&nbsp;&nbsp; &nbsp;Dim lRetval As Long</div><div class="code_line">&nbsp;&nbsp; &nbsp;Dim strAct As String</div><div class="code_line">&nbsp;&nbsp; &nbsp;Select Case Action</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_Mail</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;отправка почты (написать письмо)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;open&quot;, &quot;mailto:&quot; &amp; ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_Explore</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;открытие папки в проводнике</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ActString = StrChek(ActString, &quot;\&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;explore&quot;, ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_FindIn</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;поиск файлов в указанной папке средствами Windows</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ActString = StrChek(ActString, &quot;\&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;find&quot;, ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_Edit</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;открывает файл в соответствующем редакторе, например: *bmp -&#62; Paint</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;edit&quot;, ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_URL</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;переход на ссылку URL</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ActString = StrChek(ActString, &quot;/&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;open&quot;, &quot;http://&quot; &amp; ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_Run</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;открывает документ или запускает программу</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;open&quot;, ActString, strCmdLine, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Case ShellOperations.SO_Print</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;печатает документ (НЕ ТЕСТИРОВАЛОСЬ)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lRetval = ShellExecute(hWnd, &quot;print&quot;, ActString, vbNullString, vbNullString, SW_SHOWNORMAL)</div><div class="code_line">&nbsp;&nbsp; &nbsp;End Select</div><div class="code_line">&nbsp;&nbsp; &nbsp;If lRetval &#60;= 32 Then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;iShell = GetErrorString(lRetval)</div><div class="code_line">&nbsp;&nbsp; &nbsp;End If</div><div class="code_line">End Function</div><div class="code_line">&nbsp;</div><div class="code_line">Private Function StrChek(ByVal strMath As String, ByVal strRepl As String) As String</div><div class="code_line">&nbsp;&nbsp; &nbsp;StrChek = IIf(Right$(strMath, 1) &#60;&#62; strRepl, strMath &amp; strRepl, strMath)</div><div class="code_line">End Function</div><div class="code_line">&nbsp;</div><div class="code_line">&#39;Текст ошибки</div><div class="code_line">Private Function GetErrorString(ByVal lErr As Long) As String</div><div class="code_line">&nbsp;&nbsp; &nbsp;Dim strBuffer As String, lRetval As Long</div><div class="code_line">&nbsp;&nbsp; &nbsp;strBuffer = Space$(MAX_SIZE)</div><div class="code_line">&nbsp;&nbsp; &nbsp;lRetval = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0, lErr, 0, strBuffer, MAX_SIZE, 0)</div><div class="code_line">&nbsp;&nbsp; &nbsp;strBuffer = Left$(strBuffer, lRetval)</div><div class="code_line">&nbsp;&nbsp; &nbsp;GetErrorString = strBuffer</div><div class="code_line">End Function</div><div class="code_line">&nbsp;</div><div class="code_line">Private Sub Form_Load()</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#39;Пример запуска Блокнота</div><div class="code_line">&nbsp;&nbsp; &nbsp;Call iShell(0, SO_Run, &quot;notepad.exe&quot;)</div><div class="code_line">End Sub</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>Djoser</author>
        <category>Работа с Windows</category>
      </item>
	
      </channel>
      </rss>
	