DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Closing an application in the system tray

  1. #1
    Nehdia Sameen Guest

    Closing an application in the system tray


    Hello!

    I'm writing a small utility app that detects when I am connected to the Internet
    and launches certain applications I need (such as GetRight, my firewall,
    etc.). This part of the code I have already written. What I also want to
    do is to close the applications when my connection is closed. I know that
    to close an app you have to post a WM_CLOSE message to the main window. This
    does not, however, work with apps running in the system tray. How, then,
    can I close an application running in the system tray?

    Thanks very much!

    Regards,
    Nehdia Sameen

  2. #2
    Craig Clearman Guest

    Re: Closing an application in the system tray

    Nehdia,

    >I know that
    >to close an app you have to post a WM_CLOSE message to the main window. This
    >does not, however, work with apps running in the system tray.


    Sure it does. The problem is that the main window is usually hidden.
    You should use a program like Spy++ to find the window that
    corresponds to your program, and find its class name. If that class
    name is unique, then you can search all open windows for that class.
    Otherwise, you need to find some information about the window that
    makes it unique. Then you send your WM_CLOSE message to that hidden
    window.

    Ciao, Craig


  3. #3
    Craig Clearman Guest

    Re: Closing an application in the system tray

    Nehdia,

    >I know that
    >to close an app you have to post a WM_CLOSE message to the main window. This
    >does not, however, work with apps running in the system tray.


    Sure it does. The problem is that the main window is usually hidden.
    You should use a program like Spy++ to find the window that
    corresponds to your program, and find its class name. If that class
    name is unique, then you can search all open windows for that class.
    Otherwise, you need to find some information about the window that
    makes it unique. Then you send your WM_CLOSE message to that hidden
    window.

    Ciao, Craig


  4. #4
    Alex Nitulescu Guest

    Re: Closing an application in the system tray


    "Nehdia Sameen" <nehdia_s@yahoo.com> wrote:
    >
    >Hello!
    >
    >I'm writing a small utility app that detects when I am connected to the

    Internet
    >and launches certain applications I need (such as GetRight, my firewall,
    >etc.). This part of the code I have already written. What I also want to
    >do is to close the applications when my connection is closed. I know that
    >to close an app you have to post a WM_CLOSE message to the main window.

    This
    >does not, however, work with apps running in the system tray. How, then,
    >can I close an application running in the system tray?
    >
    >Thanks very much!
    >
    >Regards,
    >Nehdia Sameen


    ShellTrayRemove
    Call PostMessage(frmYourForm.hwnd, WM_CLOSE, 0&, ByVal 0&)

    where
    ShellTrayRemove is

    Private Sub ShellTrayRemove()

    'Remove the icon from the taskbar
    Call Shell_NotifyIcon(NIM_DELETE, NID)
    End Sub

    and
    Public Const NIM_DELETE As Long = &H2
    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
    Public NID As NOTIFYICONDATA
    Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA"
    (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

    It works fine in my app. But now you don't know the hwnd of the windows in
    other apps - you might have to find it out. And I still wonder why doesn't
    WM_CLOSE work in your case...

    Alex.

  5. #5
    Alex Nitulescu Guest

    Re: Closing an application in the system tray


    "Nehdia Sameen" <nehdia_s@yahoo.com> wrote:
    >
    >Hello!
    >
    >I'm writing a small utility app that detects when I am connected to the

    Internet
    >and launches certain applications I need (such as GetRight, my firewall,
    >etc.). This part of the code I have already written. What I also want to
    >do is to close the applications when my connection is closed. I know that
    >to close an app you have to post a WM_CLOSE message to the main window.

    This
    >does not, however, work with apps running in the system tray. How, then,
    >can I close an application running in the system tray?
    >
    >Thanks very much!
    >
    >Regards,
    >Nehdia Sameen


    ShellTrayRemove
    Call PostMessage(frmYourForm.hwnd, WM_CLOSE, 0&, ByVal 0&)

    where
    ShellTrayRemove is

    Private Sub ShellTrayRemove()

    'Remove the icon from the taskbar
    Call Shell_NotifyIcon(NIM_DELETE, NID)
    End Sub

    and
    Public Const NIM_DELETE As Long = &H2
    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
    Public NID As NOTIFYICONDATA
    Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA"
    (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

    It works fine in my app. But now you don't know the hwnd of the windows in
    other apps - you might have to find it out. And I still wonder why doesn't
    WM_CLOSE work in your case...

    Alex.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center