使用VB6打开WhatsApp桌面应用程序。

5

您好,我有一个疑问。问题是如何从VB6中的按钮打开WhatsApp桌面版,并在特定号码上发送信息。例如,在C#中可以使用Process属性来实现:

var process = $"whatsapp://send?phone=54123456789&text=hello!!";
Process.Start(process);

但是在vb6中我不知道如何完成这个方法,因为它可能是:

Shell "C://path_to_whatsapp_installed/whatsapp.exe" 

但我无法在特定的聊天中打开它


5
使用ShellExecute API,就像打开HTTP URL(例如https://dev59.com/Tm855IYBdhLWcg3wGQbY)一样使用。 - Alex K.
2
在VB.NET中的做法与C#相同:使用System.Diagnostics.Process.Start方法。 - Hel O'Ween
1
但是,我说的是VB6而不是VB.NET @HelO'Ween - user14351562
2
确实。我 <- 眼睛上的番茄。抱歉。 - Hel O'Ween
2个回答

2
您可以使用ShellExecute函数:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (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 

Const SW_SHOWNORMAL = 1
Const SW_SHOWMAXIMIZED = 3

在您的表单中这样调用它:

ShellExecute Me.hWnd, "Open", "whatsapp://send?phone=54123456789&text=hello!", "", "", SW_SHOWMAXIMIZED

1
哇,太棒了!感谢您的帮助。我有一个疑问,关于 ByVal lpParameters As String, ByVal lpDirectory As String,我可以放什么进去? - user14351562
2
@FrancoJoelBalsamo 这是一个链接,链接到文档 - Brian M Stafford
我有一个疑问,是否可以添加像PDF / XLS / JPEG这样的文件进行发送?还是不行? - user14351562

1

Win32 ShellExecute 包装器。我在一个公共辅助类中有这个。

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWNOACTIVATE As Long = 4
Private Const SW_SHOW As Long = 5
Private Const SW_MINIMIZE As Long = 6
Private Const SW_SHOWMINNOACTIVE As Long = 7
Private Const SW_SHOWNA As Long = 8
Private Const SW_RESTORE As Long = 9
Private Const SW_SHOWDEFAULT As Long = 10
Private Const SW_FORCEMINIMIZE As Long = 11

' ShellOpenDocument verbs

Public Enum ShellExecuteVerbs
   sevNULL
   sevEdit
   sevExplore
   sevFind
   sevOpen
   sevPrint
   sevRunAs
End Enum

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
   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

'------------------------------------------------------------------------------
'Purpose  : Opens a document with the registered application for this file type
'
'Prereq.  : -
'Parameter: sFileName      - Fully qualified filename
'           eShellVerb     - The action the associated application should do with documentName
'           lWindowState   - Window state and/or focus of the associated application
'           hWndParent     - Parent window handle
'           sWorkingDirectory - Working directory
'Returns  : > 32 = Success
'Note     : See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
'           for possible error codes <= 32
'------------------------------------------------------------------------------
Public Function ShellOpenDocument( _
   ByVal sFileName As String, _
   Optional ByVal eShellVerb As ShellExecuteVerbs = sevOpen, _
   Optional ByVal lWindowState As Long = SW_SHOWNORMAL, _
   Optional ByVal hWndParent As Long = 0, _
   Optional ByVal sWorkingDirectory As String = vbNullString _
   ) As Long
   
   Dim sVerb As String
   
   Select Case eShellVerb

      Case ShellExecuteVerbs.sevNULL
         sVerb = vbNull
      Case ShellExecuteVerbs.sevEdit
         sVerb = "edit"
      Case ShellExecuteVerbs.sevExplore
         sVerb = "explore"
      Case ShellExecuteVerbs.sevFind
         sVerb = "find"
      Case ShellExecuteVerbs.sevOpen
         sVerb = "open"
      Case ShellExecuteVerbs.sevPrint
         sVerb = "print"
      Case ShellExecuteVerbs.sevRunAs
         sVerb = "runas"
      Case Else
         sVerb = vbNull
   End Select
   
   If Len(sWorkingDirectory) < 1 Then
      sWorkingDirectory = App.Path
   End If
   
   ShellOpenDocument = ShellExecute(hWndParent, _
                           sVerb, _
                           sFileName, _
                           vbNullString, _
                           sWorkingDirectory, _
                           lWindowState)

End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接