Solved

Icon on system tray

Posted on 1999-01-13
3
306 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

19 Experts available now in Live!

Get 1:1 Help Now