VB如何让一个程序后台运行

VB如何让一个程序后台运行,第1张

1添加一个Timer时间控铅颤掘件

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

'申明api

Private Function MyHotKey(vKeyCode) As Boolean

MyHotKey = ((GetAsyncKeyState(vKeyCode) <0))

End Function

Private Sub Form_Load()

Timer1.Interval = 100 '定时检测

Me.Visible = False

End Sub

Private Sub Timer1_Timer()

If MyHotKey(vbKeyO) Then Me.Visible = True

'还可以换槐核洞仿成调用其他函数

End Sub

我帮你改了一段代码,可以获取进程游握早辩的PID和路径: 在窗体设计器里放一个listbox,命名为process; 再放2个txtbox,一个名为ProcessID,用来显示PID; 另一个名为Path,用来显示进程路径; 最后再放一个按钮,命名为cmdRefresh,用于刷新列表; 完成后即可运行代码。 Option Explicit Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetForegroundWindow Lib "user32" () As Long Const GW_CHILD = 5 Const GW_HWNDNEXT = 2 Dim lngHand As Long Dim strName As String * 255 Dim lngProcID As Long Dim lngDeskTopHandle As Long Dim lngWindowCount As Long Public Function GetOpenWindowNames() As Long lngDeskTopHandle = GetDesktopWindow() lngHand = GetWindow(lngDeskTopHandle, GW_CHILD) Do While lngHand <>神睁庆 0 GetWindowText lngHand, strName, Len(strName) lngHand = GetWindow(lngHand, GW_HWNDNEXT) If Left$(strName, 1) <>vbNullChar Then form1.process.AddItem Left$(strName, InStr(1, strName, vbNullChar)) End If Loop End Function Public Function GetProcessPathByProcessID(PID As Long) As String On Error GoTo Z Dim cbNeeded As Long Dim szBuf(1 To 250) As Long Dim Ret As Long Dim szPathName As String Dim nSize As Long Dim hProcess As Long hProcess = OpenProcess(&H400 Or &H10, 0, PID) If hProcess <>0 Then Ret = EnumProcessModules(hProcess, szBuf(1), 250, cbNeeded) If Ret <>0 Then szPathName = Space(260) nSize = 500 Ret = GetModuleFileNameExA(hProcess, szBuf(1), szPathName, nSize) GetProcessPathByProcessID = Left(szPathName, Ret) End If End If Ret = CloseHandle(hProcess) If GetProcessPathByProcessID = "" Then GetProcessPathByProcessID = "SYSTEM" End If Exit Function Z: End Function Private Sub cmdRefresh_Click() GetOpenWindowNames lngHand = FindWindow(vbNullString, process.Text) GetWindowThreadProcessId lngHand, lngProcID ProcessID = lngProcID Path = GetProcessPathByProcessID(lngProcID) End Sub Private Sub Form_Load() cmdRefresh.Caption = "刷新" GetOpenWindowNames lngHand = FindWindow(vbNullString, process.Text) GetWindowThreadProcessId lngHand, lngProcID ProcessID = lngProcID Path = GetProcessPathByProcessID(lngProcID) End Sub Private Sub process_Click() lngHand = FindWindow(vbNullString, process.Text) GetWindowThreadProcessId lngHand, lngProcID ProcessID = lngProcID Path = GetProcessPathByProcessID(lngProcID) End Sub

新建个EXE工程,把Form1的Visible属性设为False.给工具箱加入Microsoft

Internet

Control,并在窗体猜咐上嫌兆春加入一个这芹耐个控件.

代码:

Private

Sub

Form_Load()

Me.Timer1.Interval

=

10

*

60

*

1000

Me.Timer1.Enabled

=

True

End

Sub

Private

Sub

Timer1_Timer()

Me.WebBrowser1.Navigate

"http://网址"

End

Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12559825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存