定制任務欄托盤的 dll
羅朝輝(飄飄白云) 2009.06.08
http://www.shnenglu.com/kesalin
(轉(zhuǎn)載時請注明作者和出處。未經(jīng)許可,請勿用于商業(yè)用途)
源文件下載: 點擊這里
google 項目地址: http://code.google.com/p/luotasktray/
通過對這個 dll 進行定制(修改圖標和插入菜單,很簡單的替換工作),你就很容易地在新工程中使用定制的任務欄托盤,這樣就省去了每寫一個桌面程序就要整些桌面托盤的代碼。而且這個dll所創(chuàng)建的任務欄托盤與主窗口是非阻塞模式的。
效果如圖:
顯示任務欄托盤:

顯示右鍵菜單:

響應右鍵菜單事件:

1,下面來說說如何簡單地通過修改這個 dll 來定制自己需要的任務欄托盤,分兩步:替換圖標和修改菜單項。
step 1: 替換圖標
打開 LuoTaskTray 工程中的TaskTrayWindow.h,找到如下位置,修改成你自己的圖標就可以了。
// Load icon
hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
// TODO: Load your icon here!
//hIcon = ::LoadIcon((HINSTANCE) GetWindowLongPtr(GWLP_HINSTANCE), MAKEINTRESOURCE(IDI_ICON1));
step 2: 修改菜單項
打開 LuoTaskTray 工程中的TaskTrayWindow.h,找到如下位置,修改成你自己的菜單項目就可以了。
// create menu
WTL::CMenu popup;
popup.CreatePopupMenu();
// TODO: Insert your menu items here!
int i = 0;
popup.InsertMenu(i++, MF_BYPOSITION, LuoTaskTray::TTM_EXIT, _T("Exit"));
然后你將 LuoTaskTray
編譯生成就可以得到定制好的 dll 和 lib 文件了。
2,如何在自己的工程中使用剛剛生成的 dll 和 lib 文件。
step 1: 包含相關頭文件,并鏈接庫文件。
// Include header files for LuoTaskTray.
#include "include/LuoTaskTray.h"
#include "include/LuoTaskTrayImport.h"
// Import lib
#ifdef _DEBUG
#pragma comment(lib, "bin/LuoTaskTray_Debug.lib")
#else
#pragma comment(lib, "bin/LuoTaskTray.lib")
#endif
step 2: 程序開始的時候裝載 dll 并初始化,在程序退出時清理并卸載 dll。
LuoTaskTray::UActivator* g_pLuoTaskTray;
/**
* @brief : Initialize LuoTaskTray
* @param :
* @return : bool
*/
bool InitLuoTaskTray()
{
HRESULT hRslt = LuoTaskTray::Activate_import(
L"activator",
(void**)&g_pLuoTaskTray);
if (FAILED(hRslt)) {
return false;
}
hRslt = g_pLuoTaskTray->Initialize();
if (FAILED(hRslt)) {
g_pLuoTaskTray->Uninitialize();
g_pLuoTaskTray = NULL;
return false;
}
return true;
}
/**
* @brief : Uninitialize LuoTaskTray
* @param :
* @return :
*/
void UninitLuoTaskTray()
{
if (g_pLuoTaskTray != NULL) {
g_pLuoTaskTray->Uninitialize();
g_pLuoTaskTray = NULL;
}
}
step 3: 創(chuàng)建任務欄托盤。
if (g_pLuoTaskTray){
LuoTaskTray::UCreateParam param = {0, };
param.hwnd = *this;
param.CallbackMessage = LuoTaskTray::WM_TASK_TRAY_CALLBACK;
HRESULT hRslt = g_pLuoTaskTray->CreateTaskTray(¶m);
if (SUCCEEDED(hRslt)) {
::EnableWindow(GetDlgItem(ID_TEST), FALSE);
}
}
step 4: 響應回調(diào)消息并創(chuàng)建回調(diào)函數(shù)。
MESSAGE_HANDLER(LuoTaskTray::WM_TASK_TRAY_CALLBACK, OnTaskTrayCallback)
LRESULT OnTaskTrayCallback(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int cmd = (int)wParam;
HWND hwnd = (HWND) lParam;
switch (cmd) {
case LuoTaskTray::TTM_EXIT:
::MessageBox(*this, L"Menu Event \"Close\" from Tasktray", L"TrakTray", MB_OK);
CloseDialog(0);
break;
}
return 0;
}
大功告成!