TrayIconDlgDlg.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:11k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // TrayIconDlgDlg.cpp : implementation file
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "TrayIconDlg.h"
  22. #include "TrayIconDlgDlg.h"
  23. #include "AboutDlg.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CTrayIconDlgDlg dialog
  31. CTrayIconDlgDlg::CTrayIconDlgDlg(CWnd* pParentWnd /*=NULL*/)
  32. : CXTResizeDialog(CTrayIconDlgDlg::IDD, pParentWnd)
  33. {
  34. //{{AFX_DATA_INIT(CTrayIconDlgDlg)
  35. m_bAnimateIcon = FALSE;
  36. m_bHideIcon = FALSE;
  37. m_bShowIcon = TRUE;
  38. m_strBalloonMsg = _T("");
  39. m_strBalloonTitle = _T("");
  40. m_iTimeOut = 10;
  41. m_strToolTip = _T("Power Tools for MFC!");
  42. m_iBalloonIcon = 1;
  43. //}}AFX_DATA_INIT
  44. m_strBalloonMsg.LoadString(IDS_BALLOON_MESSAGE);
  45. m_strBalloonTitle.LoadString(IDS_BALLOON_TITLE);
  46. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  47. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  48. m_bMinimized = false;
  49. // Enable Ofice XP themes
  50. XTThemeManager()->SetTheme(xtThemeOfficeXP);
  51. }
  52. void CTrayIconDlgDlg::DoDataExchange(CDataExchange* pDX)
  53. {
  54. CXTResizeDialog::DoDataExchange(pDX);
  55. //{{AFX_DATA_MAP(CTrayIconDlgDlg)
  56. DDX_Control(pDX, IDC_TEXT_INFO, m_txtInfo);
  57. DDX_Control(pDX, IDC_TXT_TOOLTIP, m_txtToolTip);
  58. DDX_Control(pDX, IDC_TXT_TIMEOUT, m_txtTimeOut);
  59. DDX_Control(pDX, IDC_TXT_BALLOONTITLE, m_txtBalloonTitle);
  60. DDX_Control(pDX, IDC_TXT_BALLOONMSG, m_txtBalloonMsg);
  61. DDX_Control(pDX, IDC_TXT_BALLOONICON, m_txtBalloonIcon);
  62. DDX_Control(pDX, IDC_EDIT_TOOLTIP, m_editTooltip);
  63. DDX_Control(pDX, IDC_EDIT_TIMEOUT, m_editTimeout);
  64. DDX_Control(pDX, IDC_EDIT_BALLOONTITLE, m_editBalloonTitle);
  65. DDX_Control(pDX, IDC_COMBO_BALLOONICON, m_comboBalloonIcon);
  66. DDX_Control(pDX, IDC_CHK_SHOWICON, m_chkShowIcon);
  67. DDX_Control(pDX, IDC_CHK_HIDEICON, m_chkHideIcon);
  68. DDX_Control(pDX, IDC_CHK_ANIMATEICON, m_chkAnimateIcon);
  69. DDX_Control(pDX, IDC_BTN_TOOLTIP, m_btnTooltip);
  70. DDX_Control(pDX, IDC_BTN_SHOWBALLOON, m_btnShowBalloon);
  71. DDX_Control(pDX, IDC_BTN_MINIMIZETRAY, m_btnMinimizeToTray);
  72. DDX_Control(pDX, IDC_TXT_BALLOONTIP, m_txtBalloonTip);
  73. DDX_Control(pDX, IDC_GBOX_BALLOONTIP, m_gboxBalloonTip);
  74. DDX_Check(pDX, IDC_CHK_ANIMATEICON, m_bAnimateIcon);
  75. DDX_Check(pDX, IDC_CHK_HIDEICON, m_bHideIcon);
  76. DDX_Check(pDX, IDC_CHK_SHOWICON, m_bShowIcon);
  77. DDX_Text(pDX, IDC_EDIT_BALLOONMSG, m_strBalloonMsg);
  78. DDV_MaxChars(pDX, m_strBalloonMsg, 255);
  79. DDX_Text(pDX, IDC_EDIT_BALLOONTITLE, m_strBalloonTitle);
  80. DDV_MaxChars(pDX, m_strBalloonTitle, 63);
  81. DDX_Text(pDX, IDC_EDIT_TIMEOUT, m_iTimeOut);
  82. DDV_MinMaxInt(pDX, m_iTimeOut, 10, 30);
  83. DDX_Text(pDX, IDC_EDIT_TOOLTIP, m_strToolTip);
  84. DDX_CBIndex(pDX, IDC_COMBO_BALLOONICON, m_iBalloonIcon);
  85. DDX_Control(pDX, IDC_EDIT_BALLOONMSG, m_editBalloonMsg);
  86. //}}AFX_DATA_MAP
  87. }
  88. BEGIN_MESSAGE_MAP(CTrayIconDlgDlg, CXTResizeDialog)
  89. //{{AFX_MSG_MAP(CTrayIconDlgDlg)
  90. ON_WM_SYSCOMMAND()
  91. ON_WM_PAINT()
  92. ON_WM_QUERYDRAGICON()
  93. ON_WM_DESTROY()
  94. ON_BN_CLICKED(IDC_CHK_SHOWICON, OnChkShowicon)
  95. ON_BN_CLICKED(IDC_CHK_HIDEICON, OnChkHideicon)
  96. ON_BN_CLICKED(IDC_CHK_ANIMATEICON, OnChkAnimateicon)
  97. ON_EN_CHANGE(IDC_EDIT_TOOLTIP, OnChangeEditTooltip)
  98. ON_BN_CLICKED(IDC_BTN_TOOLTIP, OnBtnTooltip)
  99. ON_EN_CHANGE(IDC_EDIT_BALLOONTITLE, OnChangeEditBalloontitle)
  100. ON_EN_CHANGE(IDC_EDIT_TIMEOUT, OnChangeEditTimeout)
  101. ON_EN_CHANGE(IDC_EDIT_BALLOONMSG, OnChangeEditBalloonmsg)
  102. ON_CBN_SELENDOK(IDC_COMBO_BALLOONICON, OnSelendokComboBalloonicon)
  103. ON_BN_CLICKED(IDC_BTN_SHOWBALLOON, OnBtnShowBalloon)
  104. ON_BN_CLICKED(IDC_BTN_MINIMIZETRAY, OnBtnMinimizetray)
  105. ON_COMMAND(IDR_RESTORE, OnRestore)
  106. //}}AFX_MSG_MAP
  107. END_MESSAGE_MAP()
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CTrayIconDlgDlg message handlers
  110. static UINT icoArray[] =
  111. {
  112. IDI_ICON2,
  113. IDI_ICON3,
  114. IDI_ICON4,
  115. IDI_ICON5
  116. };
  117. BOOL CTrayIconDlgDlg::OnInitDialog()
  118. {
  119. CXTResizeDialog::OnInitDialog();
  120. // Add "About..." menu item to system menu.
  121. // IDM_ABOUTBOX must be in the system command range.
  122. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  123. ASSERT(IDM_ABOUTBOX < 0xF000);
  124. CMenu* pSysMenu = GetSystemMenu(FALSE);
  125. if (pSysMenu != NULL)
  126. {
  127. CString strAboutMenu;
  128. strAboutMenu.LoadString(IDS_ABOUTBOX);
  129. if (!strAboutMenu.IsEmpty())
  130. {
  131. pSysMenu->AppendMenu(MF_SEPARATOR);
  132. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  133. }
  134. }
  135. // Set the icon for this dialog.
  136. SetIcon(m_hIcon, TRUE);         // Set big icon
  137. SetIcon(m_hIcon, FALSE);        // Set small icon
  138. // Set control resizing.
  139. SetResize(IDC_EDIT_TOOLTIP,      SZ_TOP_LEFT,     SZ_TOP_RIGHT);
  140. SetResize(IDC_BTN_TOOLTIP,       SZ_TOP_RIGHT,    SZ_TOP_RIGHT);
  141. SetResize(IDC_BTN_MINIMIZETRAY,  SZ_TOP_RIGHT,    SZ_TOP_RIGHT);
  142. SetResize(IDOK,                  SZ_TOP_RIGHT,    SZ_TOP_RIGHT);
  143. SetResize(IDC_TEXT_INFO,         SZ_TOP_LEFT,     SZ_TOP_RIGHT);
  144. SetResize(IDC_GBOX_FEATURES,     SZ_TOP_LEFT,     SZ_TOP_RIGHT);
  145. SetResize(IDC_GBOX_BALLOONTIP,   SZ_TOP_LEFT,     SZ_TOP_RIGHT);
  146. SetResize(IDC_EDIT_BALLOONTITLE, SZ_TOP_LEFT,     SZ_TOP_RIGHT);
  147. SetResize(IDC_TXT_TIMEOUT,       SZ_TOP_RIGHT,    SZ_TOP_RIGHT);
  148. SetResize(IDC_EDIT_TIMEOUT,      SZ_TOP_RIGHT,    SZ_TOP_RIGHT);
  149. SetResize(IDC_EDIT_BALLOONMSG,   SZ_TOP_LEFT,     SZ_BOTTOM_RIGHT);
  150. SetResize(IDC_TXT_BALLOONICON,   SZ_BOTTOM_LEFT,  SZ_BOTTOM_LEFT);
  151. SetResize(IDC_COMBO_BALLOONICON, SZ_BOTTOM_LEFT,  SZ_BOTTOM_RIGHT);
  152. SetResize(IDC_BTN_SHOWBALLOON,   SZ_BOTTOM_RIGHT, SZ_BOTTOM_RIGHT);
  153. // Load window placement
  154. LoadPlacement(_T("CTrayIconDlgDlg"));
  155. m_txtBalloonTip.SetFont(&XTAuxData().fontBold);
  156. EnableControls();
  157. // display the balloon tooltip.
  158. OnBtnShowBalloon();
  159. // Create the tray icon.
  160. if (!m_TrayIcon.Create(
  161. _T("Power Tools for MFC!"), // Toolktip text
  162. this,                       // Parent window
  163. IDI_TRAYICON,               // Icon resource ID
  164. IDR_POPUP_TRAY,             // Resource ID of popup menu
  165. IDR_RESTORE,                // Default menu item for popup menu
  166. false))                     // True if default menu item is located by position
  167. {
  168. TRACE0("Failed to create tray iconn");
  169. return -1;
  170. }
  171. m_TrayIcon.SetAnimationIcons(
  172. icoArray, _countof(icoArray));
  173. return TRUE;  // return TRUE unless you set the focus to a control
  174.               // EXCEPTION: OCX Property Pages should return FALSE
  175. }
  176. void CTrayIconDlgDlg::OnDestroy()
  177. {
  178. // Save window placement
  179. SavePlacement(_T("CTrayIconDlgDlg"));
  180. CXTResizeDialog::OnDestroy();
  181. }
  182. void CTrayIconDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
  183. {
  184. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  185. {
  186. CAboutDlg dlgAbout;
  187. dlgAbout.DoModal();
  188. }
  189. else
  190. {
  191. CXTResizeDialog::OnSysCommand(nID, lParam);
  192. }
  193. }
  194. // If you add a minimize button to your dialog, you will need the code below
  195. //  to draw the icon.  For MFC applications using the document/view model,
  196. //  this is automatically done for you by the framework.
  197. void CTrayIconDlgDlg::OnPaint()
  198. {
  199. if (IsIconic())
  200. {
  201. CPaintDC dc(this); // device context for painting
  202. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  203. // Center icon in client rectangle
  204. int cxIcon = GetSystemMetrics(SM_CXICON);
  205. int cyIcon = GetSystemMetrics(SM_CYICON);
  206. CRect rect;
  207. GetClientRect(&rect);
  208. int x = (rect.Width() - cxIcon + 1) / 2;
  209. int y = (rect.Height() - cyIcon + 1) / 2;
  210. // Draw the icon
  211. dc.DrawIcon(x, y, m_hIcon);
  212. }
  213. else
  214. {
  215. CXTResizeDialog::OnPaint();
  216. }
  217. }
  218. // The system calls this to obtain the cursor to display while the user drags
  219. //  the minimized window.
  220. HCURSOR CTrayIconDlgDlg::OnQueryDragIcon()
  221. {
  222. return (HCURSOR) m_hIcon;
  223. }
  224. void CTrayIconDlgDlg::OnChkShowicon()
  225. {
  226. UpdateData();
  227. if (m_bShowIcon) {
  228. m_TrayIcon.AddIcon();
  229. m_btnMinimizeToTray.EnableWindow();
  230. }
  231. else {
  232. m_TrayIcon.RemoveIcon();
  233. m_btnMinimizeToTray.EnableWindow(FALSE);
  234. }
  235. }
  236. void CTrayIconDlgDlg::OnChkHideicon()
  237. {
  238. UpdateData();
  239. if (m_bHideIcon) {
  240. m_TrayIcon.HideIcon();
  241. m_btnMinimizeToTray.EnableWindow(FALSE);
  242. }
  243. else {
  244. m_TrayIcon.ShowIcon();
  245. m_btnMinimizeToTray.EnableWindow();
  246. }
  247. }
  248. void CTrayIconDlgDlg::OnChkAnimateicon()
  249. {
  250. UpdateData();
  251. if (m_bAnimateIcon) {
  252. m_TrayIcon.StartAnimation();
  253. }
  254. else {
  255. m_TrayIcon.StopAnimation();
  256. }
  257. }
  258. void CTrayIconDlgDlg::OnChangeEditTooltip()
  259. {
  260. UpdateData();
  261. }
  262. void CTrayIconDlgDlg::OnBtnTooltip()
  263. {
  264. UpdateData();
  265. m_TrayIcon.SetTooltipText(m_strToolTip);
  266. }
  267. void CTrayIconDlgDlg::OnChangeEditBalloontitle()
  268. {
  269. UpdateData();
  270. }
  271. void CTrayIconDlgDlg::OnChangeEditTimeout()
  272. {
  273. }
  274. void CTrayIconDlgDlg::OnChangeEditBalloonmsg()
  275. {
  276. UpdateData();
  277. }
  278. void CTrayIconDlgDlg::OnSelendokComboBalloonicon()
  279. {
  280. UpdateData();
  281. }
  282. UINT balloonIcon[] =
  283. {
  284. NIIF_ERROR,  // Error icon.
  285. NIIF_INFO,   // Information icon.
  286. NIIF_NONE,   // No icon.
  287. NIIF_WARNING // Warning icon.
  288. };
  289. void CTrayIconDlgDlg::OnBtnShowBalloon()
  290. {
  291. if (!UpdateData())
  292. return;
  293. m_TrayIcon.ShowBalloonTip(m_strBalloonMsg, m_strBalloonTitle,
  294. balloonIcon[m_iBalloonIcon], m_iTimeOut);
  295. }
  296. void CTrayIconDlgDlg::MinMaxWindow()
  297. {
  298. m_bMinimized = !m_bMinimized;
  299. if (m_bMinimized)
  300. {
  301. m_TrayIcon.MinimizeToTray(this);
  302. m_btnMinimizeToTray.SetWindowText(_T("&Maximize from Tray..."));
  303. }
  304. else
  305. {
  306. m_TrayIcon.MaximizeFromTray(this);
  307. m_btnMinimizeToTray.SetWindowText(_T("&Minimize to Tray..."));
  308. }
  309. }
  310. void CTrayIconDlgDlg::OnBtnMinimizetray()
  311. {
  312. MinMaxWindow();
  313. }
  314. void CTrayIconDlgDlg::EnableControls()
  315. {
  316. bool bEnable = XTOSVersionInfo()->IsWin2KOrGreater();
  317. // Requirements:
  318. // Windows 2000 or later, VC6 with platform SDK or
  319. // later to use these features.
  320. m_chkHideIcon.      EnableWindow(bEnable);
  321. m_txtBalloonTip.    EnableWindow(bEnable);
  322. m_txtBalloonTitle.  EnableWindow(bEnable);
  323. m_editBalloonTitle. EnableWindow(bEnable);
  324. m_txtTimeOut.       EnableWindow(bEnable);
  325. m_editTimeout.      EnableWindow(bEnable);
  326. m_txtBalloonMsg.    EnableWindow(bEnable);
  327. m_editBalloonMsg.   EnableWindow(bEnable);
  328. m_txtBalloonIcon.   EnableWindow(bEnable);
  329. m_comboBalloonIcon. EnableWindow(bEnable);
  330. m_btnShowBalloon.   EnableWindow(bEnable);
  331. m_editBalloonMsg.SetBackColor(bEnable ?
  332. GetXtremeColor(COLOR_WINDOW) : GetXtremeColor(COLOR_3DFACE));
  333. }
  334. void CTrayIconDlgDlg::OnRestore()
  335. {
  336. MinMaxWindow();
  337. }