《SystemTray》——— SystemTray的简单使用

SystemTray控件也就是显示电池信号量的区域空间(如图)

它可以做進度提示、广告等。

1 SystemTray.BackgroundColor="<背景顏色>"
2 SystemTray.ForegroundColor="<前景顏色>"
3 SystemTray.Opacity="<透明度>"
4 SystemTray.IsVisible="<啟動時是否顯示>">
5 SystemTray.ProgressIndicatorProperty="<设置进度指示器(ProgressIndicator)>"

下面是一个简单的使用例子

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using Microsoft.Phone.Shell;
14 using System.Windows.Threading;
15 namespace TestSystemTray
16 {
17     public partial class MainPage : PhoneApplicationPage
18     {
19         // 建構函式
20         public MainPage()
21         {
22             InitializeComponent();
23         }
24         private void button1_Click(object sender, RoutedEventArgs e)
25         {
26             ProgressIndicator _mangoIndicator = new ProgressIndicator();
27             DispatcherTimer _timer = new DispatcherTimer();
28             _timer.Interval = TimeSpan.FromSeconds(2);
29             _timer.Tick += (obj, args) =>
30             {
31                 _mangoIndicator.IsVisible = false;
32                 ClearValue(SystemTray.ProgressIndicatorProperty);
33                 ClearValue(SystemTray.OpacityProperty);
34                 ClearValue(SystemTray.BackgroundColorProperty);
35                 ClearValue(SystemTray.ForegroundColorProperty);
36                 (obj as DispatcherTimer).Stop();
37                 obj = null;
38             };
39             _mangoIndicator.IsVisible = true;
40             _mangoIndicator.Text = "不管你信不信,反正我信了";
41             _mangoIndicator.IsIndeterminate = true;
42             SetValue(SystemTray.ProgressIndicatorProperty, _mangoIndicator);
43             SetValue(SystemTray.OpacityProperty, 0.9);
44             SetValue(SystemTray.BackgroundColorProperty, Colors.Yellow);
45             SetValue(SystemTray.ForegroundColorProperty, Colors.Blue);
46             SystemTray.IsVisible = true;
47             _timer.Start();
48         }
49     }
50 }

运行效果

本文同步发表于:卤面网

http://www.hugwp.com/article-404-1.html

 

参考:http://blogs.msdn.com/b/ericsk/archive/2011/08/03/wp7-systemtray-progress-indication.aspx

转载于:https://www.cnblogs.com/qq278360339/archive/2012/06/20/2555944.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
制作系统托盘程序 VC 点击数:695 发布日期:2006-9-24 12:32:00 【收藏】 【评论】 【打印】 【编程爱好者论坛】 【关闭】 其实,在任务条上添加托盘比较容易实现,调用VC中的函数就可以解决,只是注意协调。接下来,我将给大家提供一个类,可以很容易的达到我们的目的,希望它能给大家一点帮助,如果你发现BUG也欢迎你和我联系。运行程序,左键双击或者右键单击任务条上的托盘,我们可以看到效果。   1. 有关类CsystemTray的说明:(文章的最后有本类的具体实现代码)   CSystemTray是CObject的扩展类,实现以下功能:    1、在任务条显示托盘图标。    2、设置提示Tip    3、设置图标的形状   主要函数说明: Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID);   功能:    生成一个图标。   参数说明:    pWnd:程序的主窗口,    uCallbackMessage:对应的消息映射,    szTip:鼠标停留时的提示文字,    icon:显示的图标,    uID:与之对应的菜单ID   · BOOL CSystemTray::SetIcon(HICON hIcon)     BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName)     BOOL CSystemTray::SetIcon(UINT nIDResource)     BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)     BOOL CSystemTray::SetStandardIcon(UINT nIDResource)    功能:更改托盘上的图标。   void CSystemTray::ShowIcon()    功能:显示图标。   · BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip)     BOOL CSystemTray::SetTooltipText(UINT nID)    功能:提示显示文字。   2. 使用本类的步骤如下:   第一步:在VC编程环境下,建立一个工程,基于对话框或者是单文档(或者是多文档)你随便,所有的选项都取默认值即可。   第二步:在mainfrm.h中定义变量CSystemTray m_TrayIcon;并添加函数声明:    afx_msg LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam);    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);   第三步:在StaAFX.h中定义消息:    #define WM_ICON_NOTIFY WM_USER + 1   第四步:自定义菜单IDR_POPUPMENU,其中最少包括一项:ID:ID_VIEW_MAIN_WINDOW,Caption为"显示主窗口",并为该项在类CmainFrame中添加消息映射函数,COMMAND和UPDATE_COMMAND_UI。   第五步:在Mainframe.cpp中添加以下内容: 1. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_COMMAND(ID_VIEW_MAIN_WINDOW, OnViewMainWindow) ON_UPDATE_COMMAND_UI(ID_VIEW_MAIN_WINDOW, OnUpdateViewMainWindow) ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification) ON_WM_SYSCOMMAND() //}}AFX_MSG_MAP END_MESSAGE_MAP() 2. void CMainFrame::OnViewMainWindow() { if(IsWindowVisible()) { ShowWindow(SW_SHOWMINIMIZED); ShowWindow(SW_HIDE); m_TrayIcon.SetIcon(IDI_ICON1); } else { ShowWindow(SW_SHOW); ShowWindow(SW_RESTORE); m_TrayIcon.SetIcon(IDI_ICON2); } } void CMainFrame::OnUpdateViewMainWindow(CCmdUI* pCmdUI) { pCmdUI->SetCheck(IsWindowVisible()); } 3. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; //创建托盘图标 if (!m_TrayIcon.Create(this, WM_ICON_NOTIFY,"大屏实时显示程序", NULL, IDR_POPUPMENU)) return -1; m_TrayIcon.SetIcon(IDI_ICON1); SetMenu(NULL); return 0; } 4. LRESULT CMainFrame::OnTrayNotification(WPARAM wParam, LPARAM lParam) { if (wParam != IDR_POPUPMENU) return 0L; CMenu menu, *pSubMenu; if (LOWORD(lParam) == WM_RBUTTONUP) { CPoint pos; GetCursorPos(&pos;); if (!menu.LoadMenu(IDR_POPUPMENU)) return 0; if (!(pSubMenu=menu.GetSubMenu(0))) return 0; ::SetMenuDefaultItem(pSubMenu->m_hMenu, 3, TRUE); SetForegroundWindow(); pSubMenu->TrackPopupMenu(TPM_RIGHTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, this); menu.DestroyMenu(); } else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) { if (!menu.LoadMenu(IDR_POPUPMENU)) return 0; if (!(pSubMenu = menu.GetSubMenu(0))) return 0; SetForegroundWindow(); //激活第2个菜单项 SendMessage(WM_COMMAND, pSubMenu->GetMenuItemID(1), 0); menu.DestroyMenu(); } return 0; } void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam) { if(nID==SC_MINIMIZE) { ShowWindow(SW_SHOWMINIMIZED); ShowWindow(SW_HIDE); } else CFrameWnd::OnSysCommand(nID, lParam); } 3. 附录:类的实现代码 CsystemTray的头文件 #ifndef _INCLUDED_SYSTEMTRAY_H_ #define _INCLUDED_SYSTEMTRAY_H_ ///////////////////////////////////////////////////////////////////////////// // CSystemTray window class CSystemTray : public CObject { // Construction/destruction public: CSystemTray(); CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID); virtual ~CSystemTray(); // Operations public: CFrameWnd * m_pFrame; BOOL Enabled() { return m_bEnabled; } BOOL Visible() { return !m_bHidden; } //Create the tray icon Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID); //Change or retrieve the Tooltip text BOOL SetTooltipText(LPCTSTR pszTooltipText); BOOL SetTooltipText(UINT nID); CString GetTooltipText() const; //Change or retrieve the icon displayed BOOL SetIcon(HICON hIcon); BOOL SetIcon(LPCTSTR lpIconName); BOOL SetIcon(UINT nIDResource); BOOL SetStandardIcon(LPCTSTR lpIconName); BOOL SetStandardIcon(UINT nIDResource); HICON GetIcon() const; void HideIcon(); void ShowIcon(); void RemoveIcon(); void MoveToRight(); //Change or retrieve the window to send notification messages to BOOL SetNotificationWnd(CWnd* pNotifyWnd); CWnd* GetNotificationWnd() const; //Default handler for tray notification message // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSystemTray) //}}AFX_VIRTUAL // Implementation protected: BOOL m_bEnabled; // does O/S support tray icon? BOOL m_bHidden; // Has the icon been hidden? NOTIFYICONDATA m_tnd; DECLARE_DYNAMIC(CSystemTray) }; #endif /////////////////////////////////////////////////// CsystemTray的实现文件 #include "stdafx.h" #include "SystemTray.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNAMIC(CSystemTray, CObject) ///////////////////////////////////////////////// // CSystemTray construction/creation/destruction CSystemTray::CSystemTray() { memset(&m_tnd, 0, sizeof(m_tnd)); m_bEnabled = FALSE; m_bHidden = FALSE; } CSystemTray::CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID) { Create(pWnd, uCallbackMessage, szToolTip, icon, uID); m_bHidden = FALSE; } BOOL CSystemTray::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID) { // this is only for Windows 95 (or higher) VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4); if (!m_bEnabled) return FALSE; //Make sure Notification window is valid VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd()))); if (!m_bEnabled) return FALSE; //Make sure we avoid conflict with other messages ASSERT(uCallbackMessage >= WM_USER); //Tray only supports tooltip text up to 64 characters ASSERT(_tcslen(szToolTip) <= 64); // load up the NOTIFYICONDATA structure m_tnd.cbSize = sizeof(NOTIFYICONDATA); m_tnd.hWnd = pWnd->GetSafeHwnd(); m_tnd.uID = uID; m_tnd.hIcon = icon; m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; m_tnd.uCallbackMessage = uCallbackMessage; strcpy (m_tnd.szTip, szToolTip); // Set the tray icon m_pFrame = (CFrameWnd*)pWnd; VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd)); return m_bEnabled; } CSystemTray::~CSystemTray() { RemoveIcon(); } ///////////////////////////////////////////// // CSystemTray icon manipulation void CSystemTray::MoveToRight() { HideIcon(); ShowIcon(); } void CSystemTray::RemoveIcon() { if (!m_bEnabled) return; m_tnd.uFlags = 0; Shell_NotifyIcon(NIM_DELETE, &m_tnd); m_bEnabled = FALSE; } void CSystemTray::HideIcon() { if (m_bEnabled && !m_bHidden) { m_tnd.uFlags = NIF_ICON; Shell_NotifyIcon (NIM_DELETE, &m_tnd); m_bHidden = TRUE; } } void CSystemTray::ShowIcon() { if (m_bEnabled && m_bHidden) { m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; Shell_NotifyIcon(NIM_ADD, &m_tnd); m_bHidden = FALSE; } } BOOL CSystemTray::SetIcon(HICON hIcon) { if (!m_bEnabled) return FALSE; m_tnd.uFlags = NIF_ICON; m_tnd.hIcon = hIcon; return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName) { HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName); return SetIcon(hIcon); } BOOL CSystemTray::SetIcon(UINT nIDResource) { HICON hIcon = AfxGetApp()->LoadIcon(nIDResource); return SetIcon(hIcon); } BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName) { HICON hIcon = LoadIcon(NULL, lpIconName); return SetIcon(hIcon); } BOOL CSystemTray::SetStandardIcon(UINT nIDResource) { HICON hIcon = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(nIDResource)); return SetIcon(hIcon); } HICON CSystemTray::GetIcon() const { HICON hIcon = NULL; if (m_bEnabled) hIcon = m_tnd.hIcon; return hIcon; } ////////////////////////////////////////////////// // CSystemTray tooltip text manipulation BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip) { if (!m_bEnabled) return FALSE; m_tnd.uFlags = NIF_TIP; _tcscpy(m_tnd.szTip, pszTip); return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } BOOL CSystemTray::SetTooltipText(UINT nID) { CString strText; VERIFY(strText.LoadString(nID)); return SetTooltipText(strText); } CString CSystemTray::GetTooltipText() const { CString strText; if (m_bEnabled) strText = m_tnd.szTip; return strText; } //////////////////////////////////////////////// // CSystemTray notification window stuff BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd) { if (!m_bEnabled) return FALSE; //Make sure Notification window is valid ASSERT(pWnd && ::IsWindow(pWnd->GetSafeHwnd())); m_tnd.hWnd = pWnd->GetSafeHwnd(); m_tnd.uFlags = 0; return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } CWnd* CSystemTray::GetNotificationWnd() const { return CWnd::FromHandle(m_tnd.hWnd); } 引用地址:http://blog.programfan.com/trackback.asp?id=18739
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值