[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Icon on system tray

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
ahTri
Asked:
ahTri
  • 2
1 Solution
 
MCoroliCommented:
.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
 
ahTriAuthor Commented:
I'm using Visual C, is this going to work ?
0
 
MCoroliCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now