Solved

Icon on system tray

Posted on 1999-01-13
3
310 Views
Last Modified: 2010-04-16
How to make the icon of the program appear in system tray in stead of the tast bar ?
how to make the right mouse button context menu on the start menu popup a custom menu instead of the system menu (restor,
minimise, maximise, ...)
0
Comment
Question by:ahTri
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
MCoroli earned 50 total points
ID: 1182063
.Find below source code from C++Builder example. (If use Builder, you can find this example - TrayIcon). I solved the problem you are saying about using those source texts. Hope it will help you


1. h-file; i marked interesting strings with '***'
traymain.h
#ifndef traymainH
#define traymainH
//---------------------------------------------------------------
#include <Forms.hpp>
#include <StdCtrls.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
***#define MYWM_NOTIFY         (WM_APP+100)
***#define IDC_MYICON                     1006
***extern HINSTANCE g_hinst;
***LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi);
//---------------------------------------------------------------
class TForm1 : public TForm
{
__published:
    TButton *Button1;
    TCheckBox *CheckBox1;
    TRadioButton *RadioButton1;
    TRadioButton *RadioButton2;
    TEdit *Edit1;
    TEdit *Edit2;
    TImage *Image2;
    TImage *Image1;
    TLabel *Label1;
    TLabel *Label2;
    TPopupMenu *PopupMenu1;
    TMenuItem *Properties1;
    TMenuItem *ToggleState1;
    TMenuItem *Shutdown1;
    void __fastcall FormDestroy(TObject *Sender);
    void __fastcall CheckBox1Click(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall RadioButtonClick(TObject *Sender);
    void __fastcall EditKeyUp(TObject *Sender, WORD &Key, TShiftState Shift);
    void __fastcall Properties1Click(TObject *Sender);
***    void __fastcall ToggleState1Click(TObject *Sender);
    void __fastcall Shutdown1Click(TObject *Sender);
   
   
private:        // private user declarations
***    void __fastcall DrawItem(TMessage& Msg);
***    void __fastcall MyNotify(TMessage& Msg);
***    bool __fastcall TrayMessage(DWORD dwMessage);
***    HANDLE __fastcall IconHandle(void);
***    void __fastcall ToggleState(void);
***    PSTR __fastcall TipText(void);
public:         // public user declarations
    virtual __fastcall TForm1(TComponent* Owner);
***BEGIN_MESSAGE_MAP
***MESSAGE_HANDLER(WM_DRAWITEM,TMessage,DrawItem)
***MESSAGE_HANDLER(MYWM_NOTIFY,TMessage,MyNotify)
***END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------
extern TForm1 *Form1;
//---------------------------------------------------------------
#endif


2. The main application file
trayicon.cpp
#include <vcl.h>
#pragma hdrstop
HINSTANCE g_hinst;
//---------------------------------------------------------------
USEFILE("readme.txt");
USEFORM("traymain.cpp", Form1);
USERES("trayicon.res");
//---------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE hInstance, LPSTR, int)
{
    Application->Initialize();
    g_hinst = hInstance;
    Application->CreateForm(__classid(TForm1), &Form1);
        Application->Run();

    return 0;
}
//---------------------------------------------------------------

3. Source of the form; here are all function descripted
traymain.cpp

#include <vcl.h>
#include <shellapi.h>
#pragma hdrstop

#include "traymain.h"
//---------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------
void __fastcall TForm1::DrawItem(TMessage& Msg)
{
     IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
     TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------
void __fastcall TForm1::MyNotify(TMessage& Msg)
{
    POINT MousePos;

    switch(Msg.LParam)
    {
        case WM_RBUTTONUP:
            if (GetCursorPos(&MousePos))
            {
                PopupMenu1->PopupComponent = Form1;
                SetForegroundWindow(Handle);
                PopupMenu1->Popup(MousePos.x, MousePos.y);
            }
            else
                Show();
            break;
        case WM_LBUTTONUP:
            ToggleState();
            break;
        default:
            break;
    }
    TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
      TrayMessage(NIM_DELETE);
}
//---------------------------------------------------------------
bool __fastcall TForm1::TrayMessage(DWORD dwMessage)
{
   NOTIFYICONDATA tnd;
   PSTR pszTip;

   pszTip = TipText();

   tnd.cbSize          = sizeof(NOTIFYICONDATA);
   tnd.hWnd            = Handle;
   tnd.uID             = IDC_MYICON;
   tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage      = MYWM_NOTIFY;

   if (dwMessage == NIM_MODIFY)
    {
        tnd.hIcon            = IconHandle();
        if (pszTip)
           lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
          else
        tnd.szTip[0] = '\0';
    }
   else
    {
        tnd.hIcon = NULL;
        tnd.szTip[0] = '\0';
    }

   return (Shell_NotifyIcon(dwMessage, &tnd));
}
//---------------------------------------------------------------
HANDLE __fastcall TForm1::IconHandle(void)
{
    if (RadioButton1->Checked)
        return (Image1->Picture->Icon->Handle);
    else
        return (Image2->Picture->Icon->Handle);
}
//---------------------------------------------------------------
void __fastcall TForm1::ToggleState(void)
{
    if (RadioButton1->Checked)
    {
        RadioButton1->Checked = false;
        RadioButton2->Checked = true;
    }
    else
    {
        RadioButton2->Checked = false;
        RadioButton1->Checked = true;
    }
    TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------
PSTR __fastcall TForm1::TipText(void)
{
    if (RadioButton1->Checked)
        return (Edit1->Text.c_str());
    else
        return (Edit2->Text.c_str());
}
//---------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
      if (CheckBox1->Checked)
    {
          TrayMessage(NIM_ADD);
        TrayMessage(NIM_MODIFY);
    }
      else
          TrayMessage(NIM_DELETE);

    Button1->Enabled = CheckBox1->Checked;
}
//---------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Hide();
}
//---------------------------------------------------------------
void __fastcall TForm1::RadioButtonClick(TObject *Sender)
{
    if (!CheckBox1->Checked)
        return;

    TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------
void __fastcall TForm1::EditKeyUp(TObject *Sender, WORD &Key,
    TShiftState Shift)
{
    if (!CheckBox1->Checked)
        return;

    TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------
LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
{
      HICON hIcon;

      hIcon=(HICON)LoadImage(g_hinst,MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
            16, 16, 0);
      if (!hIcon)
            return(FALSE);

DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
            16, 16, 0, NULL, DI_NORMAL);

      return(TRUE);
}
//---------------------------------------------------------------
void __fastcall TForm1::Properties1Click(TObject *Sender)
{
    Show();    
}
//---------------------------------------------------------------
void __fastcall TForm1::ToggleState1Click(TObject *Sender)
{
    ToggleState();
}
//---------------------------------------------------------------
void __fastcall TForm1::Shutdown1Click(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------

0
 

Author Comment

by:ahTri
ID: 1182064
I'm using Visual C, is this going to work ?
0
 
LVL 1

Expert Comment

by:MCoroli
ID: 1182065
I'm sure it will not work if you compile it iv VC.
It just can help you to undestand how icon can be placed to system tray. You can find some API functions and they are the same for Builder and VC.
By the way, i heard VC has an exellent help. Can't you find an example there?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now