Solved

Icon on system tray

Posted on 1999-01-13
3
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

628 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