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

📄 trayicon.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "TrayIcon.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------

TTrayIcon::TTrayIcon(TPopupMenu* Menu, TIcon* TrayIcon)
:   Icon(NULL),
    PopupMenu(NULL),
    MessageWnd(NULL)
{
    Icon = new TIcon();
    if(TrayIcon)
        Icon->Assign(TrayIcon);
    else
        Icon->Handle = ::LoadImage(::GetModuleHandle(NULL),"MAINICON",IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
    PopupMenu = Menu;
    AppIsHidden = false;

    MessageWnd = AllocateHWnd(TrayMessageProc);
    RestoreIcon();
}

TTrayIcon::~TTrayIcon()
{
    TNotifyIconData Data;
    memset(&Data, 0, sizeof(TNotifyIconData));
    Data.cbSize = sizeof(TNotifyIconData);
    Data.hWnd = MessageWnd;
    Data.uID = (UINT)this;
    Shell_NotifyIcon(NIM_DELETE,&Data);

    if(MessageWnd)
        DeallocateHWnd(MessageWnd);
    delete Icon;
}

void TTrayIcon::RestoreIcon()
{
    if(!Icon || !MessageWnd)
        return;

    TNotifyIconData Data;
    memset(&Data, 0, sizeof(TNotifyIconData));
    Data.cbSize = sizeof(TNotifyIconData);
    Data.hWnd = MessageWnd;
    Data.uID = (UINT)this;
    Data.hIcon = Icon->Handle;
    Data.uFlags = NIF_ICON | NIF_MESSAGE;
    Data.uCallbackMessage = WM_SYSTEM_TRAY_NOTIFY;
    Shell_NotifyIcon(NIM_ADD,&Data);

    SetHint(HintStr.c_str());
}

void TTrayIcon::SetHint(const char* NewHint)
{
    TNotifyIconData Data;
    memset(&Data, 0, sizeof(TNotifyIconData));
    Data.cbSize = sizeof(TNotifyIconData);
    Data.hWnd = MessageWnd;
    Data.uID = (UINT)this;
    Data.uFlags = NIF_TIP;

    HintStr = NewHint;

    if(!NewHint || strlen(NewHint) == 0) {
        Data.szTip[0] = 0;
    } else {
        strncpy(Data.szTip,HintStr.c_str(),64);
        Data.szTip[63] = 0;
    }
    Shell_NotifyIcon(NIM_MODIFY,&Data);
}

void TTrayIcon::HideApp()
{
//    if(!AppIsHidden) {
        Application->Minimize();
        ShowWindow(Application->Handle,SW_HIDE);
        AppIsHidden = true;
//    }
}

void TTrayIcon::RestoreApp()
{
//    if(AppIsHidden) {
        ShowWindow(Application->Handle,SW_RESTORE);
        Application->MainForm->Visible = true; // important if we are hidden at startup
        Application->ShowMainForm = true; // important if we are hidden at startup
        Application->Restore();
//        SetForegroundWindow(Application->Handle);
        if (Screen->ActiveForm && Screen->ActiveForm->Handle)
            SetForegroundWindow(Screen->ActiveForm->Handle);
        AppIsHidden = false;
//    }
}

void __fastcall TTrayIcon::TrayMessageProc(TMessage &Message)
{
    switch(Message.Msg)
    {
    case WM_QUERYENDSESSION:
        Message.Result = 1;  // should we really process this here?
        break;
    case WM_ENDSESSION:
//        EndSession();
        break;
    case WM_SYSTEM_TRAY_NOTIFY:
        switch(Message.LParam)
        {
        case WM_LBUTTONUP:
            RestoreApp();
            break;
        case WM_RBUTTONUP:
            ShowMenu();
            break;
        case WM_MOUSEMOVE:
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDBLCLK:
        case WM_RBUTTONDOWN:
        case WM_RBUTTONDBLCLK:
        case WM_MBUTTONDOWN:
        case WM_MBUTTONUP:
        case WM_MBUTTONDBLCLK:
            break;
        }
        break;
    }
}

void TTrayIcon::ShowMenu()
{
    TPoint point;

    if(PopupMenu) {
        GetCursorPos(&point);
        if (Screen->ActiveForm && Screen->ActiveForm->Handle)
            SetForegroundWindow(Screen->ActiveForm->Handle);
        PopupMenu->Popup(point.x,point.y);
    }
}




⌨️ 快捷键说明

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