⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 C++Builder高级应用开发指南随书源码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::AddTrayIcon()
{
   NOTIFYICONDATA tnd;
   PSTR pszTip="托盘图标范例";
   lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
   tnd.cbSize          = sizeof(NOTIFYICONDATA);
   tnd.hWnd            = Handle;
   tnd.uID             = ID_MYICON;
   tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage	= WM_MYNOTIFY;
   tnd.hIcon           = Application->Icon->Handle;
   Shell_NotifyIcon(NIM_ADD,&tnd);
}
//---------------------------------------------------------------------------
void TForm1::DeleteTrayIcon()
{
   NOTIFYICONDATA tnd;
   tnd.cbSize          = sizeof(NOTIFYICONDATA);
   tnd.hWnd            = Handle;
   tnd.uID             = ID_MYICON;
   tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage	= WM_MYNOTIFY;
   tnd.hIcon           = Application->Icon->Handle;

   Shell_NotifyIcon(NIM_DELETE,&tnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MyNotify(TMessage& Msg)
{
    POINT MousePos;

    switch(Msg.LParam)
    {
        case WM_RBUTTONUP:
            GetCursorPos(&MousePos);
            PopupMenu1->PopupComponent = Form1;
            SetForegroundWindow(Handle);
            PopupMenu1->Popup(MousePos.x, MousePos.y);
            break;
        case WM_LBUTTONUP:
            Show();
        default:
            break;
    }
    TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ShowItemClick(TObject *Sender)
{
    Show();    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ExitItemClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   Hide();     
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
    if (CheckBox1->Checked)
    {
    	AddTrayIcon();
    }
    else
    {
    	DeleteTrayIcon();
    }
    Button1->Enabled = CheckBox1->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   DeleteTrayIcon();     
}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -