永发信息网

VB打开exe文件除了shell 还有什么方法

答案:2  悬赏:0  手机版
解决时间 2021-04-05 07:07
  • 提问者网友:斑駁影
  • 2021-04-04 19:22
VB打开exe文件除了shell 还有什么方法
最佳答案
  • 五星知识达人网友:痴妹与他
  • 2021-04-04 19:49
1. 用下面的代码打开程序并获得程序的句柄

然后用api movewindow把窗口移动到任何你想要的位置.....
Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
' 通过进程ID获得该进程的窗口句柄
Private Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
InstanceToWnd = 0
'On Error Resume Next
' 获得首个handle.
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)

' 循环查找直到找到为给定进程ID的窗口句柄
Do While test_hwnd <> 0
'检查窗口句柄是否为顶级窗口
If GetParent(test_hwnd) = 0 Then
' 是顶级窗口
' 取该窗口所属的进程ID
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)

If test_pid = target_pid Then
' 是我们指定进程的窗口,则将该窗口的句柄返回到函数名,并退出
InstanceToWnd = test_hwnd
Exit Do
End If
End If

' 取下一个窗口的句柄
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
'获取句柄函数
Public Function fun_Shell(PathName As Variant, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Long
Dim Pid As Long
Pid = Shell(PathName, WindowStyle)
fun_Shell = InstanceToWnd(Pid)
End Function
2 .movewindow的VB声明
Declare Function MoveWindow Lib "user32" Alias
"MoveWindow" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth
As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
说明
改变指定窗口的位置和大小。顶级窗口可能受最大或最小尺寸的限制,那些尺寸优先于这里设置的参数
返回值
Long,非零表示成功,零表示失败
参数表
参数 类型及说明
hwnd Long,欲移动窗口的句柄
x Long,窗口新的左侧位置
y Long,窗口新的顶部位置
nWidth Long,窗口的新宽度
nHeight Long,窗口的高宽度
bRepaint Long,如窗口此时应重画,则设为TRUE(非零)。FALSE(零)则表明应用程序会自己决定是否重画窗口

Top
全部回答
  • 1楼网友:鱼忧
  • 2021-04-04 21:25
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 'API ShellExecute的声明
Private Sub Command1_Click()
ShellExecute 0&, "open", "D:\迅雷.exe", vbNullString, vbNullString, 1
End Sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯