1 / 19

Windows Shell Programming Task Bar

Windows Shell Programming Task Bar. 重要參考資料 : ms-help://MS.MSDNQTR.2004JAN.1033/shellcc/platform/shell/programmersguide/ shell_int/shell_int_programming/taskbar.htm. To switch to a window, the user clicks its window button.

leif
Download Presentation

Windows Shell Programming Task Bar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows Shell Programming Task Bar 重要參考資料: ms-help://MS.MSDNQTR.2004JAN.1033/shellcc/platform/shell/programmersguide/ shell_int/shell_int_programming/taskbar.htm

  2. To switch to a window, the user clicks its window button. contains commands that can access programs, documents, and settings contains shortcuts to applications A single click on the application's icon in this area launches the application. Applications can put icons in the status area to indicate the status of an operation or to notify the user about an event Introduction ToolBar buttons Start Menu Quick Launch Bar Status area

  3. Introduction • Two display options: • Auto Hide and Always On Top. 1 2 Auto Hide Always On Top

  4. 取得 Toolbar 的顯示狀態 AppBar Message • use the ABM_GETSTATE message void CRetiveveTheStateofTaskbarDlg::OnBnClickedOk(){ // Step 1: 建立 APPBARDATA 結構以包含 system appbar message APPBARDATA abd; abd.cbSize=sizeof(APPBARDATA); // Step 2: Send appbar message 給系統 (要求取出 task bar 的狀態) UINT uState = (UINT) SHAppBarMessage(ABM_GETSTATE, &abd); switch(uState){ case ABS_ALWAYSONTOP: MessageBox("The taskbar is in the always-on-top state",\ "Taskbar status"); break; case ABS_AUTOHIDE: MessageBox("The taskbar is in the autohide state","Taskbar status"); break; default: MessageBox("Error (invalid args?)","Error"); } } 取得資料 #include <shellapi.h> Shell AppBar Status 取得目前 Task Bar 的顯示狀態

  5. Registering an Application Desktop Toolbar • 要接收 Task Bar 的訊息, 就必須先註冊 • 使用 ABM_NEW message • 移除註冊使用 • ABM_REMOVE 填入你的 window handle APPBARDATA abd; abd.cbSize=sizeof(APPBARDATA); abd.hWnd=hwnd; abd.uCallbackMessage= 1234; BOOL fRegistered = (BOOL) SHAppBarMessage (ABM_NEW, &abd); 等會收到 123 就是 來自 Task Bar 的訊息 向系統註冊 SHAppBarMessage(ABM_REMOVE, &abd);

  6. 使用範例 #include <shellapi.h> APPBARDATA abd; // Step 1: 向系統註冊 (系統會把 TaskBar Notification 訊息通知這個視窗) void CGetNotifyFromTaskBarDlg::OnBnClickedOk(){ abd.cbSize=sizeof(APPBARDATA); abd.hWnd=this->m_hWnd; abd.uCallbackMessage= 1234; BOOL fRegistered = (BOOL) SHAppBarMessage(ABM_NEW, &abd); } 訊息標示, 一般都用 WM_APP BEGIN_MESSAGE_MAP(CGetNotifyFromTaskBarDlg, CDialog) … // Step 2: Message 處理段落 ON_MESSAGE (1234 , OnTaskBarStatusChange) … END_MESSAGE_MAP() 自訂訊息處理 呼叫我們的 call back function 接下頁

  7. // Step 3:TaskBar message notification 處理程式 afx_msg LRESULT CGetNotifyFromTaskBarDlg::OnTaskBarStatusChange ( WPARAM wParam, LPARAM lParam){ switch(wParam){ case ABN_STATECHANGE: MessageBox("Task Bar 的顯示狀態改變了","Task Bar 訊息",MB_OK); } return TRUE; } 當 TaskBar 顯示狀態改變,會送這個過來 AppBar Notification // Step 4:取消註冊 void CGetNotifyFromTaskBarDlg::OnBnClickedCancel() { SHAppBarMessage(ABM_REMOVE, &abd); OnCancel(); } 取得 TaskBar Notification

  8. 自動設定 TaskBar 狀態 • use the ABM_SETSTATE message APPBARDATA abd; abd.cbSize=sizeof(APPBARDATA); abd.hWnd=this->m_hWnd; abd.lParam= ABS_AUTOHIDE | ABS_ALWAYSONTOP; SHAppBarMessage(ABM_SETSTATE, &abd); 向系統送 Set State 訊息 設定 Task Bar 的狀態 自動隱藏, 但是當出現時不會被其他視窗擋住

  9. TaskBar 的 Status Area • 一般應用程式利用 Status Area 顯示目前的狀態 WinMySQLadmin 1.4 顯示目前 SQL Server 的狀態 Norton AntiVirus 防護狀態 我們可以利用 Sell Notification function 為我們的應用程式加上一樣的功能

  10. Adding, Modifying, and Deleting Icons in the Status Area • Actions with status area icons • To add an icon to the taskbar's status area • To modify an icon's information • To delete an icon from the status area Step 1: 建立自己的 Icon Resource IDI_ICON1 IDI_ICON2

  11. To add an icon Icon Handle 段落 BOOL res; NOTIFYICONDATA tnid; tnid.cbSize = sizeof(NOTIFYICONDATA); // Step 1:系統藉由 hwnd 與 uID 來標示一個 Icon tnid.hWnd = hwnd; tnid.uID = uID; // Step 2:目前 CallBackMessage , hIcon 與 szTip 資料有效 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; tnid.uCallbackMessage = MYWM_NOTIFYICON; tnid.hIcon = hicon; 設定 Icon Handle, Tooltips ToolTips 段落 if (lpszTip) // 由 lpszTip 中複製 sizeof(tnid.szTip) 個bytes到 tnid.szTip 中 HRESULT hr = StringCbCopyN(tnid.szTip, sizeof(tnid.szTip), lpszTip, sizeof(tnid.szTip)); else tnid.szTip[0] = (TCHAR)'\0'; 設定 Icon到 Status Area // Step 3:設定 Icon BOOL res; res = Shell_NotifyIcon(NIM_ADD, &tnid); if (hicon) DestroyIcon(hicon);

  12. Add an Icon 完整範例 BOOL MyTaskBarAddIcon (HWND hwnd, UINT uID,HICON hicon, LPSTR lpszTip) { NOTIFYICONDATA tnid; // 定義傳給系統的資料結構 tnid.cbSize = sizeof(NOTIFYICONDATA); // Step 1: 系統藉由 hwnd 與 uID 來標示一個 Icon tnid.hWnd = hwnd; // 目前的 window handle tnid.uID = uID; // Step 2: 目前 CallBackMessage , hIcon 與 szTip 資料有效 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; tnid.uCallbackMessage = MYWM_NOTIFYICON; tnid.hIcon = hicon; if (lpszTip) // 由 lpszTip 中複製 sizeof(tnid.szTip) 個bytes到 tnid.szTip 中 HRESULT hr = StringCbCopyN(tnid.szTip, sizeof(tnid.szTip), lpszTip, sizeof(tnid.szTip)); else tnid.szTip[0] = (TCHAR)'\0'; // Step 3: 設定 Icon BOOL res= Shell_NotifyIcon(NIM_ADD, &tnid); return res; } // Step 1: 載入先前建立的 Icon HICON hicon; hicon=LoadIcon(theApp.m_hInstance, MAKEINTRESOURCE(IDI_ICON1)); // Step 2: 呼叫 MyTaskBarAddIcon(this->m_hWnd, IDI_ICON1,hicon, "這是一個簡單的 ToolTips") ; 指定 Icon ID 設定 Call Back Notification 使用範例 ToolTips 的部分 必要的 Include 檔 #include <shellapi.h> #include <strsafe.h> // for StringCbCopyN #pragma comment( lib, "strsafe.lib" ) 請你試試看

  13. Modify an Icon’s Information NOTIFYICONDATA tnid; // 定義傳給系統的資料結構 tnid.cbSize = sizeof(NOTIFYICONDATA); // Step 1: 標示要更改的 Icon tnid.hWnd = hwnd; // 目前的 window handle tnid.uID = uID; // 你指定的 Icon ID tnid.hIcon = hicon; // Step 3: 設定 Icon BOOL res= Shell_NotifyIcon(NIM_MODIFY, &tnid); 設定新的 Icon 圖樣 指定新的 Tool Tips 程式碼同上一頁 注意: 現在改成 MODIFY

  14. delete an icon BOOL res; NOTIFYICONDATA tnid; tnid.cbSize = sizeof(NOTIFYICONDATA); tnid.hWnd = hwnd; tnid.uID = uID; res = Shell_NotifyIcon(NIM_DELETE, &tnid); 利用 hwnd 與 uID 來標示想要 Delete 的 Icon 注意: 現在是 DELETE

  15. 全部合在一起 我們自訂的 Icon 現在變成 Online 恢復原狀 自訂 Icon 在 Status 區域

  16. 攔截 Mouse Clicked NOTIFYICONDATA tnid; // 定義傳給系統的資料結構 tnid.cbSize = sizeof(NOTIFYICONDATA); // Step 1: 系統藉由 hwnd 與 uID 來標示一個 Icon tnid.hWnd = hwnd; // 目前的 window handle tnid.uID = uID; // 你指定的 Icon ID // Step 2: 目前 CallBackMessage , hIcon 與 szTip 資料有效 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; tnid.uCallbackMessage = MYWM_NOTIFYICON; tnid.hIcon = hicon; tnid.szTip = _Text(“請按 Click 看看”); // Step 3: 設定 Icon BOOL res= Shell_NotifyIcon(NIM_MODIFY, &tnid); 當發生 Mouse Clicked 會產生的自訂訊息

  17. Window Message loop 訊息處理函式 Callback function 的處理 加入 Dialog Message-Loop #define MYWM_NOTIFYICON 1234 // Cadds_an_iconDlg 訊息處理常式 BEGIN_MESSAGE_MAP(Cadds_an_iconDlg, CDialog) ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDOK, OnBnClickedOk) ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2) // Step 1: Message 處理段落 ON_MESSAGE(MYWM_NOTIFYICON , OnIconEvent) END_MESSAGE_MAP() 程式架構流程 接下頁 這是我們自己的 function

  18. 定義我們自己的處理 // Step 2: 攔截使用者按 Clicked 動作 afx_msg LRESULT Cadds_an_iconDlg::OnIconEvent(WPARAM wParam, LPARAM lParam){ UINT uID; UINT uMouseMsg; uID = (UINT) wParam; uMouseMsg = (UINT) lParam; switch(uMouseMsg){ case WM_LBUTTONDOWN: // 你可以 display 應用程式, 提供使用者更多的選項 MessageBox("使用者按下滑鼠左鍵","Task Bar 訊息",MB_OK); break; case WM_RBUTTONDOWN: // 你可以在這裡產生一個 short cut Menu MessageBox("使用者按下滑鼠右鍵","Task Bar 訊息",MB_OK); break; case WM_LBUTTONDBLCLK: MessageBox("使用者按下滑鼠雙擊左鍵","Task Bar 訊息",MB_OK); break; //case WM_MOUSEMOVE: // MessageBox("滑鼠在 Icon 上面滑動","Task Bar 訊息",MB_OK); } return TRUE; } 注意: 我們是使用 lParam GetNotifyFrom_Status_Area

  19. End

More Related