时钟程序(转)

// TrayTestDlg.h : header file
//

#if !defined(AFX_TRAYTESTDLG_H__F0C8E785_F475_41A7_B2AF_B4B4BD7614D7__INCLUDED_)
#define AFX_TRAYTESTDLG_H__F0C8E785_F475_41A7_B2AF_B4B4BD7614D7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/
// CTrayTestDlg dialog

class CTrayTestDlg : public CDialog
{
// Construction
public:
 CTrayTestDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
 //{{AFX_DATA(CTrayTestDlg)
 enum { IDD = IDD_TRAYTEST_DIALOG };
 CSpinButtonCtrl m_spin;
 int  m_ITime;
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CTrayTestDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 HICON m_hIcon;

 // Generated message map functions
 //{{AFX_MSG(CTrayTestDlg)
 virtual BOOL OnInitDialog();
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 afx_msg void OnHide();
 afx_msg void OnTimer(UINT nIDEvent);
 afx_msg void OnApply();
 virtual void OnCancel();
 afx_msg void OnAppopen();
 afx_msg void OnChangeEdit1();
 //}}AFX_MSG
 afx_msg void OnNotifyIcon(WPARAM wParam,
  LPARAM lParam);
 afx_msg BOOL OnQueryEndSession();
 DECLARE_MESSAGE_MAP()

public:
 void ShowMessage(void);
private:
 int m_nCount;
 NOTIFYICONDATA nd;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TRAYTESTDLG_H__F0C8E785_F475_41A7_B2AF_B4B4BD7614D7__INCLUDED_)

// TrayTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TrayTest.h"
#include "TrayTestDlg.h"
#include <afxpriv.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CAboutDlg dialog used for App About
#define IDI_ICON  0x0005     //图标ID
#define WM_NOTIFYICON WM_USER+5  //自定义消息的ID
#define IDT_APPLY       WM_USER+6 //Timer自定义消息

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 //{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 //{{AFX_MSG(CAboutDlg)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{{AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{{AFX_MSG_MAP(CAboutDlg)
  // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTrayTestDlg dialog

CTrayTestDlg::CTrayTestDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CTrayTestDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CTrayTestDlg)
 m_ITime = 0;
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTrayTestDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CTrayTestDlg)
 DDX_Control(pDX, IDC_SPIN1, m_spin);
 DDX_Text(pDX, IDC_EDIT1, m_ITime);
 DDV_MinMaxInt(pDX, m_ITime, 5, 155);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTrayTestDlg, CDialog)
 //{{AFX_MSG_MAP(CTrayTestDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(ID_HIDE, OnHide)
 ON_WM_TIMER()
 ON_BN_CLICKED(IDC_APPLY, OnApply)
 ON_COMMAND(IDD_APPOPEN, OnAppopen)
 ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
 //}}AFX_MSG_MAP
 ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)
 ON_WM_QUERYENDSESSION()
END_MESSAGE_MAP()

/
// CTrayTestDlg message handlers

BOOL CTrayTestDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon
 
//  CString strTemp;
// // static int Count = 0;
//  SYSTEMTIME sTime;
//  ::GetLocalTime(&sTime);
//  strTemp.Format(_T("现在时间 %02d:%02d:%02d"), sTime.wHour,sTime.wMinute,sTime.wSecond);
//  SetDlgItemText(IDC_CURRENTTIME, (LPCTSTR)strTemp);
 // TODO: Add extra initialization here
 m_ITime = 30;
 SetTimer(1, 500, NULL);
 
 nd.cbSize = sizeof(NOTIFYICONDATA);
 nd.hWnd = m_hWnd;
 nd.uID = IDR_MENU1;
 nd.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
 nd.uCallbackMessage = WM_NOTIFYICON;
 nd.hIcon = m_hIcon;
// AfxLoadString(IDR_MENU2,nd.szTip, sizeof(nd.szTip));
 strncpy(nd.szTip, "闹钟/n日常提示", sizeof("闹钟/n日常提示"));
 Shell_NotifyIcon(NIM_ADD, &nd);
 m_nCount = m_ITime;
 m_spin.SetRange(5, 155);
 m_spin.SetBuddy(GetDlgItem(IDC_EDIT1));
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTrayTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  if (nID == SC_MAXIMIZE)
  {
   return;
  }
  else if (nID == SC_MINIMIZE)
  {
   ShowWindow(SW_HIDE);
  }
  else
   CDialog::OnSysCommand(nID, lParam);
 }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTrayTestDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTrayTestDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CTrayTestDlg::OnHide()
{
 // TODO: Add your control notification handler code here
 OnSysCommand(SC_MINIMIZE, 0x0000);
 return;
}

void CTrayTestDlg::OnTimer(UINT nIDEvent)
{
 // TODO: Add your message handler code here and/or call default
 CString strTemp;
 static int Count = 0;
 SYSTEMTIME sTime;
 ::GetLocalTime(&sTime);
 strTemp.Format(_T("现在时间 %02d:%02d:%02d"), sTime.wHour,sTime.wMinute,sTime.wSecond);
 SetDlgItemText(IDC_CURRENTTIME, (LPCTSTR)strTemp);
 if (nIDEvent == IDT_APPLY)
 {
  m_nCount = m_ITime;
  Count = 0;
 }
 Count++;
 if ((m_nCount - Count/60) <= 0)
 {
  Count = 0;
  ShowMessage();
 }
 strTemp.Format(_T("离下次提醒还差 %i 分钟"), (m_nCount - Count/60));
 SetDlgItemText(IDC_RESTTIME, (LPCTSTR)strTemp);

 //add incon
 if (Count %2 == 0)
 {
  
  nd.hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
  Shell_NotifyIcon(NIM_MODIFY, &nd);
 }
 else 
 {
//   NOTIFYICONDATA nd;
//   nd.cbSize = sizeof(NOTIFYICONDATA);
//   nd.hWnd = m_hWnd;
//   nd.uID = IDI_ICON;
//   nd.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
//   nd.uCallbackMessage = WM_NOTIFYICON;
//   
//   strncpy(nd.szTip, "闹钟", sizeof("闹钟"));
  nd.hIcon = m_hIcon;
  Shell_NotifyIcon(NIM_MODIFY, &nd);
 }
 CDialog::OnTimer(nIDEvent);
}

void CTrayTestDlg::OnApply()
{
 // TODO: Add your control notification handler code here
 if (UpdateData())
 {
  OnTimer(IDT_APPLY);
 }
}

void CTrayTestDlg::ShowMessage( void )
{
 MessageBox(_T("HEHE"));
}

void CTrayTestDlg::OnCancel()
{
 // TODO: Add extra cleanup here
 KillTimer(1);
 CDialog::OnCancel();
}

void CTrayTestDlg::OnNotifyIcon( WPARAM wParam, LPARAM lParam )
{
 if (wParam == IDR_MENU1 )
 {
  if (lParam == WM_LBUTTONDOWN)
  {
   ShowWindow(SW_SHOWNORMAL);
  }
  else
  {
   CMenu menu;
   if (!menu.LoadMenu(nd.uID))
   {
    return;
   }
   CMenu* pSubMenu = menu.GetSubMenu(0);
   if (!pSubMenu)
   {
    return;
   }
   if (lParam == WM_RBUTTONUP)
   {
    ::SetMenuDefaultItem(pSubMenu->m_hMenu,0,TRUE);
    CPoint mouse;
    GetCursorPos(&mouse);
    ::SetForegroundWindow(nd.hWnd);
    ::TrackPopupMenu(pSubMenu->m_hMenu,
     0,mouse.x,mouse.y, 0,
     nd.hWnd,NULL);
   }
  }
 }
}

BOOL CTrayTestDlg::OnQueryEndSession()
{
 CTrayTestDlg::OnCancel();
 return TRUE;
}

void CTrayTestDlg::OnAppopen()
{
 // TODO: Add your command handler code here
 ShowWindow(SW_SHOWNORMAL);
}

void CTrayTestDlg::OnChangeEdit1()
{
 // TODO: If this is a RICHEDIT control, the control will not
 // send this notification unless you override the CDialog::OnInitDialog()
 // function and call CRichEditCtrl().SetEventMask()
 // with the ENM_CHANGE flag ORed into the mask.
 // TODO: Add your control notification handler code here
}

 

现在就 图盘图标消息映射到m_hwnd不是很理解。

ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)

 

[3月3号]

1. 修改源程序bug 1:当间隔时间超过指定范围时,正常应该弹出越界提示框,然后要求用户重新输入数值,此时ontimer里边离下次提醒还差%d分钟,不应采用当前越界数值m_ITime.

引起原因:当调用UpdateData(TRUE)验证间隔时间超过指定范围时m_ITime也会跟随变化,尽管已经超出其指定范围。

解决办法,增加一整形成员变量m_nCount 替代m_ITime,初始化值为m_ITime,当且仅当用户按下“应用”键,并且数值验证无误情况下,才更新m_nCount值(m_nCOunt=m_ITime).

 

2. 新增右键弹出menu功能,只需把uID参数指定到Menu Item的对应ID即可

 

[3月4号]

1. 修改源程序bug 2:当点击任务栏图标时, 主程序会隐藏

引起原因: 隐藏按钮 向窗体发送消息,nid 为SC_MINIMIZE, 在OnSysCommand响应函数中会通过该标示id,将窗体隐藏。 当点击任务栏图标是,主程序同样收到系统发来的nid也为SC_MINIMIZE消息,因而导致主窗体隐藏。

解决办法: 更改点击隐藏按钮时,向主程序传送的nID值。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值