Results 1 to 7 of 7

Thread: Manipulating a program in the system tray

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    i'm pretty sure that you can get the handle of an application as soon as you shell it.

    Do you have to use the ShellExecute method, or can you use shell?

  2. #2
    New Member
    Join Date
    Nov 2001
    Location
    Australia
    Posts
    2
    To Open it I'm currently using-

    UCalcID = ShellExecute(frmMain.hwnd, "open", Application.Path & "UCalc.exe", "-fast", Application.Path, SW_SHOWNORMAL)

    To Close it I attempted-

    PostMessage UCalcID, WM_CLOSE, CLng(0), CLng(0)

    but it didn't work...

    I also attempted SendMessage to close and FindWindow to find whether it was running but both didn't work...

  3. #3

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    That's because it ShellExecute returns a ErrorSuccess value, not a handle.

    try this:

    VB Code:
    1. Option Explicit
    2.  
    3.       Private Type PROCESS_INFORMATION
    4.          hProcess As Long
    5.          hThread As Long
    6.          dwProcessId As Long
    7.          dwThreadId As Long
    8.       End Type
    9.  
    10.       Private Type STARTUPINFO
    11.          cb As Long
    12.          lpReserved As String
    13.          lpDesktop As String
    14.          lpTitle As String
    15.          dwX As Long
    16.          dwY As Long
    17.          dwXSize As Long
    18.          dwYSize As Long
    19.          dwXCountChars As Long
    20.          dwYCountChars As Long
    21.          dwFillAttribute As Long
    22.          dwFlags As Long
    23.          wShowWindow As Integer
    24.          cbReserved2 As Integer
    25.          lpReserved2 As Long
    26.          hStdInput As Long
    27.          hStdOutput As Long
    28.          hStdError As Long
    29.       End Type
    30.  
    31.       Private Declare Function CreateProcess Lib "kernel32" _
    32.          Alias "CreateProcessA" _
    33.          (ByVal lpApplicationName As String, _
    34.          ByVal lpCommandLine As String, _
    35.          lpProcessAttributes As Any, _
    36.          lpThreadAttributes As Any, _
    37.          ByVal bInheritHandles As Long, _
    38.          ByVal dwCreationFlags As Long, _
    39.          lpEnvironment As Any, _
    40.          ByVal lpCurrentDriectory As String, _
    41.          lpStartupInfo As STARTUPINFO, _
    42.          lpProcessInformation As PROCESS_INFORMATION) As Long
    43.  
    44.       Private Declare Function OpenProcess Lib "kernel32.dll" _
    45.          (ByVal dwAccess As Long, _
    46.          ByVal fInherit As Integer, _
    47.          ByVal hObject As Long) As Long
    48.  
    49.       Private Declare Function TerminateProcess Lib "kernel32" _
    50.          (ByVal hProcess As Long, _
    51.          ByVal uExitCode As Long) As Long
    52.  
    53.       Private Declare Function CloseHandle Lib "kernel32" _
    54.          (ByVal hObject As Long) As Long
    55.  
    56.       Const SYNCHRONIZE = 1048576
    57.       Const NORMAL_PRIORITY_CLASS = &H20&
    58.  
    59.       Private Sub Form_Click()
    60.          Dim pInfo As PROCESS_INFORMATION
    61.          Dim sInfo As STARTUPINFO
    62.          Dim sNull As String
    63.          Dim lSuccess As Long
    64.          Dim lRetValue As Long
    65.  
    66.          sInfo.cb = Len(sInfo)
    67.          lSuccess = CreateProcess(sNull, _
    68.                                  "Calc.exe", _
    69.                                  ByVal 0&, _
    70.                                  ByVal 0&, _
    71.                                  1&, _
    72.                                  NORMAL_PRIORITY_CLASS, _
    73.                                  ByVal 0&, _
    74.                                  sNull, _
    75.                                  sInfo, _
    76.                                  pInfo)
    77.  
    78.          MsgBox "Calculator has been launched!"
    79.  
    80.          lRetValue = TerminateProcess(pInfo.hProcess, 0&)
    81.          lRetValue = CloseHandle(pInfo.hThread)
    82.          lRetValue = CloseHandle(pInfo.hProcess)
    83.  
    84.          MsgBox "Calculator has terminated!"
    85.       End Sub

    Try that ?

  4. #4
    New Member
    Join Date
    Nov 2001
    Location
    Australia
    Posts
    2
    This is what I was working from on ShellExecute. (From www.vbapi.com) Is this incorrect then?
    --------------------
    If successful, the function returns a handle to the instance of the application which the function started. If an error occured, the function returns either 0 (indicating a lack of memory) or one of the following flags:

    etc. etc.
    --------------------

    Also, how would I be able to tell if the program is running ?

    I don't have that spy.exe program that is mentioned on these forums a few times and couldn't find it with a search... Anywhere to get it?

    Or is there some other way to determine if the sys tray program is running?


    EDIT: Oh yeah, the code you listed above works perfectly... Thanks alot! You made my day...
    Last edited by Master Victor; Nov 20th, 2001 at 09:38 AM.

  5. #5

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    No probs man

    I thought you still needed help until i read that edit

  6. #6
    Member
    Join Date
    Oct 2001
    Posts
    54
    can u figure out this?

    Private Sub Timer1_Timer()
    Static isFirstFrame As Boolean
    If isFristFrame Then
    TrayIcon.Icon = LoadPicture("pic1.ico")
    Else
    TrayIcon.Icon = LoadPicture("pic2.ico")
    End If
    modifyIcon 'method that call API to change icon in the tray
    isFirstFrame = Not isFirstFrame
    End Sub

    Private Sub Form_Load()
    Set trayIcon = New TrayIcon
    Set trayIcon.OwnerForm = Me
    trayIcon.Visible = True
    Timer1.Enabled = true
    Timer1.Interval = 250
    End Sub

    Private Sub Command1_Click()
    MsgBox "test....."

    End Sub

  7. #7
    Member
    Join Date
    Oct 2001
    Posts
    54
    sorry, err click....

    My question: I use timer to get animated icon in my app(suppose 2 frames). It works well. But when I click button Command1 to do some operation, say a msgbox pop up, then the animation stop until that operation is finished. I hope the icon is always animated no matter what happen.

    do u have any suggstion? thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width