原创 Labwindows/cvi8.5学习日志(47)----系统托盘trayicon

2010-8-12 10:41 3545 7 7 分类: 软件与OS

fbc8d37a-ce90-4a3c-a757-64e5c8a4cf41.jpg


//***************************************************************************
// Labwindows/cvi8.5  系统托盘
// 改写自NI官方的例程
// szlihongtao
// 2010-08-12
//***************************************************************************
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* FILE:    trayicon.c                                                       */
/*                                                                           */
/* PURPOSE: This example illustrates how to use the WinTools instrument      */
/*          driver to install icons in the status area of the Windows shell  */
/*          taskbar, called the System Tray.  Your app can respond to events */
/*          on the icon via a simple callback mechanism.                     */
/*                                                                           */
/*---------------------------------------------------------------------------*/
#include <userint.h>
#include <ansi_c.h>
#include <cvirte.h>    
#include "trayicon.h"
#include "toolbox.h"


/*---------------------------------------------------------------------------*/
/* Module-globals                                                            */
/*---------------------------------------------------------------------------*/
static int g_panelHandle;


/*---------------------------------------------------------------------------*/
/* Internal function prototypes                                              */
/*---------------------------------------------------------------------------*/
int CVICALLBACK TaskbarIconCB (int iconHandle, int event, int eventData);


/*---------------------------------------------------------------------------*/
/* This is the application's entry-point.                                    */
/*---------------------------------------------------------------------------*/
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpszCmdLine, int nCmdShow)
{
    int trayIconHandle;
    int menuItemIndex;
       
    if (InitCVIRTE (hInstance, 0, 0) == 0) 
        return -1;
    if ((g_panelHandle = LoadPanel (0, "trayicon.uir", PANEL)) < 0)
   {
        CloseCVIRTE ();
        return -1;
  }
    SetPanelAttribute (g_panelHandle, ATTR_HAS_TASKBAR_BUTTON, 0);// 没有任务栏按钮,防止同时有任务栏按钮/系统托盘,这2项是一致的
    SetSystemAttribute (ATTR_TASKBAR_BUTTON_VISIBLE, 0);   // 不显示任务栏


    /* Add an icon to the taskbar System Tray */
    /* 增加图标,得到句柄 */ 
    InstallSysTrayIcon ("cvi.ico", "CVI Tray Icon: Right-click for popup",
                        TaskbarIconCB, &trayIconHandle);
   
    /* Create a right-click menu for the icon and add some items to it */
    AttachTrayIconMenu (trayIconHandle);    // 增加右击弹出式菜单
    InsertTrayIconMenuItem (trayIconHandle, "Default Item", &menuItemIndex);// 最下面一行的菜单项
    InsertTrayIconMenuItem (trayIconHandle, "Dimmed Item", &menuItemIndex);
    InsertTrayIconMenuItem (trayIconHandle, 0, &menuItemIndex);      // 0表示分隔符
    InsertTrayIconMenuItem (trayIconHandle, "Checked Item", &menuItemIndex);// 最上面一行的菜单项
   
    /* Set some attributes of the menu */
 /*  Specifies the 1朾ased index of the default item in the popup
  menu as returned by InsertTrayIconMenuItem, or 0 if you do not
  want a default menu item.
  制定弹出式菜单项的基准索引值,如果不制定的话,则默认值为0
  在本软件中,指定为1,则   "Default Item" 的索引值为1
  "Dimmed Item"的索引值为  2
  分隔符的索引值为         3
  "Checked Item"的索引值为 4 
 */
    SetTrayIconMenuAttr (trayIconHandle, ATTR_POPUP_DEFAULT_ITEM, 1); // 基准索引值为1
    SetTrayIconMenuItemAttr (trayIconHandle, 2, ATTR_DIMMED, 1);   // 灰色显示
    SetTrayIconMenuItemAttr (trayIconHandle, 4, ATTR_CHECKED, 1);    // 打勾


    /* Display the panel and run the GUI -- the app must process events in   */
    /* order to receive messages from the tray icon.                         */
    DisplayPanel (g_panelHandle);
    RunUserInterface ();


    /* Discard the tray icon's menu and then the icon itself */
    DetachTrayIconMenu (trayIconHandle);   // 删除托盘的弹出式菜单
    RemoveSysTrayIcon (trayIconHandle);    // 删除系统托盘


    /* Clean up and return */
    DiscardPanel (g_panelHandle);
    CloseCVIRTE ();
    return 0;
}  
/*---------------------------------------------------------------------------*/
/* This function responds to events from the system tray icon.  We will      */
/* simply look at the incoming event and display it in the TextBox on our    */
/* panel.                                                                    */
/*---------------------------------------------------------------------------*/
int CVICALLBACK TaskbarIconCB (int iconHandle, int event, int eventData)
{
    char eventName [70];
   
    switch (event)
   {
        case EVENT_LEFT_CLICK:
            strcpy (eventName, "Left button down\n");   
            break;
        case EVENT_LEFT_MOUSE_UP:
            strcpy (eventName, "Left button up\n");
            break;
        case EVENT_RIGHT_CLICK:
            strcpy (eventName, "Right button down\n");
            break;
        case EVENT_RIGHT_MOUSE_UP:
            strcpy (eventName, "Right button up\n");
            break;
        case EVENT_LEFT_DOUBLE_CLICK:
            strcpy (eventName, "Left double-click\n");
            break;
        case EVENT_RIGHT_DOUBLE_CLICK:       
            strcpy (eventName, "Right double-click\n");
            break;
        case EVENT_MENU_ITEM: // 选中了菜单项
  {
            /* With menu events, eventData contains the selected item's ID */
            sprintf (eventName, "Menu item %d selected\n", eventData);
            break;
  } 
        default:
            eventName[0] = 0;
  }
    SetCtrlVal (g_panelHandle, PANEL_TEXTBOX, eventName);
   
    /* Honor our popup menu -- return non-zero to prevent it from appearing */
    return 0;
}
//***************************************************************************
int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
                         int eventData1, int eventData2)
{
    if (event == EVENT_CLOSE)
        QuitUserInterface (0);
    return 0;
}
//***************************************************************************
int CVICALLBACK QuitCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            QuitUserInterface (0);
            break;
   }
    return 0;
}
//***************************************************************************
//***************************************************************************
//***************************************************************************

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条