Is there a way to refresh the taskbar's tray icon? I had to taskkill a third party program and need that icon to go away.
Printable View
Is there a way to refresh the taskbar's tray icon? I had to taskkill a third party program and need that icon to go away.
Try this
Form code
vb Code:
Private Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadID As Long End Type Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _ hHandle As Long, ByVal dwMilliseconds As Long) As Long 'API Constants Const SMTO_BLOCK = &H1 Const SMTO_ABORTIFHUNG = &H2 Const WM_NULL = &H0 Const WM_CLOSE = &H10 Const PROCESS_ALL_ACCESS = &H1F0FFF 'API functions Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _ lpdwProcessId As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _ (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As _ Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ lpApplicationName As String, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const INFINITE = -1& Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Public Function ExecCmd(cmdline$) ' Initialize the STARTUPINFO structure: start.cb = Len(start) ' Start the shelled application: ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc) ' Wait for the shelled application to finish: ret& = WaitForSingleObject(proc.hProcess, INFINITE) Call GetExitCodeProcess(proc.hProcess, ret&) Call CloseHandle(proc.hThread) Call CloseHandle(proc.hProcess) ExecCmd = ret& End Function Sub Form_Click() Dim retval As Long Dim lngResult As Long Dim lngReturnValue As Long 'Replace the path and app.name.type with that of the application you want to use retval = ExecCmd("C:\Users\User\Desktop\Project.exe") lngReturnValue = SendMessageTimeout(retval, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult) DoEvents 'u can give a tool tip text here nid.szTip = "" & vbNullChar nid.cbSize = Len(nid) nid.hwnd = retval nid.uId = vbNull nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE nid.uCallBackMessage = WM_MOUSEMOVE nid.hIcon = Me.Icon If lngReturnValue Then MsgBox ("Responding") Else Shell_NotifyIcon NIM_DELETE, nid 'Close the host and the client if the client does not respond Call GetExitCodeProcess(proc.hProcess, ret&) Call CloseHandle(proc.hThread) Call CloseHandle(proc.hProcess) Unload Me End If End Sub
module code
vb Code:
Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean 'declaration for putting the application to system tray Public Type NOTIFYICONDATA cbSize As Long hwnd As Long uId As Long uFlags As Long uCallBackMessage As Long hIcon As Long szTip As String * 64 End Type 'declare constants Public Const NIM_ADD = &H0 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2 Public Const WM_MOUSEMOVE = &H200 Public Const NIF_MESSAGE = &H1 Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 'Dimension a variable as the user-defined data type. Public nid As NOTIFYICONDATA
Change
"C:\Users\User\Desktop\Project.exe"
to match that of your system and the external program.
Edit:
I posted the original code in the Detect if program is not responding! thread.
I replaced the program but each time I launch the code, the window freezes.