System Tray Icons | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||
Download: |
|
|||||||||||
This is a suite of classes to make it easy to create Applications that minimize to the System Tray. In CTrayIcon clicking the icon immediately restores the Application's Main Window. CMenuTrayIcon handles a Context Menu for the icon, so Right-Clicking the Icon makes one of your Icon Resources pop up and Double-Clicking the icon performs that menu's Default Action. CAniTrayIcon extends CTrayIcon to animate the Icon (you give it a list of Icon Resources). CAniMenuTrayIcon has Animated Icons and a Context Menu.
Since this class cannot maintain the Title-Bar movement animation on Minimize and Restore that the operating system normally implements you may wish to use my CWndAnimator class with this class.
CTrayIcon helps you make a basic TrayIcon application. Create a single instance of the class after your Application's m_pMainWnd variable is set. In CMyApp::InitInstance() (this example is for a Dialog Application):
CMyDlg dlg; m_pMainWnd=&dlg; CTrayIcon TI("My ToolTip Text", AfxGetApp()->LoadIcon(IDR_MAINFRAME)); dlg.DoModal();
If you want your Main Window's Task Bar Button to be hidden when the window is minimized to the System Tray you will need to add the following function:
Use ClassWizard to add an OnSysCommand handler.
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam) { switch(nID & 0xFFF0) { case SC_MINIMIZE: if(lParam) ShowWindow(SW_HIDE); else SetForegroundWindow(); return; case IDM_ABOUTBOX: CAboutDlg().DoModal(); return; // This line is only for a Dialog Application with an About Box. default: CDialog::OnSysCommand(nID, lParam); return; } }CMenuTrayIcon is a CTrayIcon with a Context Menu.
Your CMyApp::InitInstance() would now have a couple of extra parameters to construct the CMenuTrayIcon. You need to create a Menu Resource, in this case called IDR_PopUps. The first SubMenu will be used for the System Tray Icon (You can keep all your other PopUp Menus in the same Menu Resource). The last parameter is the ID of the Default Menu Item. The Default Menu Item will be shown in bold text in the menu and will be what happens when the user double-clicks the icon.
CMyDlg dlg; m_pMainWnd=&dlg; CMenuTrayIcon MTI("My ToolTip Text", AfxGetApp()->LoadIcon(IDR_MAINFRAME), IDR_PopUps, ID_ShowMe); dlg.DoModal();
You will also have to use ClassWizard to create handlers for each of these menu items (COMMAND and, optionally, UPDATE_COMMAND_UI (be aware that Dialog Applications don't support UPDATE_COMMAND_UI: you need CFrameWnd derived windows for that)). To show your application, use ShowWindow(SW_RESTORE); To close a dialog application, use CDialog::OnCancel();
CAniTrayIcon is a CTrayIcon that handles Animated Icons.
Your Application can now set a NULL Terminated Array of Icon Resource IDs and turn Animation on and off:
CMyDlg dlg; m_pMainWnd=&dlg; CAniTrayIcon ATI("My ToolTip Text", AfxGetApp()->LoadIcon(IDR_MAINFRAME)); static const UINT Icons[]={IDI_ICON1,IDI_ICON2,IDI_ICON3, ... ,0}; AMTI.SetIcons(Icons); ATI.Animate(100); // Show one frame every 100 milliseconds. dlg.DoModal();Well, that'll show you how it works, but to allow your Application to access the CAniTrayIcon::Animate and CAniTrayIcon::StopAnimating functions, you'll need to create an instance of the class you want to use in your Application Header file, then use the Create function when you've got the pointer to the Main Window. You can then access the class by using AfxGetApp(). For example: ((CMyApp*)AfxGetApp())->ATI.StopAnimating();
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.