⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockdlg.cpp

📁 自动托盘代码
💻 CPP
字号:
// ClockDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Clock.h"
#include "ClockDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialog

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

void CClockDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClockDlg)
	DDX_Text(pDX, IDC_INTERVAL, m_interval);
	DDV_MinMaxInt(pDX, m_interval, 1, 150);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClockDlg, CDialog)
	//{{AFX_MSG_MAP(CClockDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_BN_CLICKED(IDC_HIDE, OnHide)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_NOTIFYICON,NotifyIcon)
	ON_WM_QUERYENDSESSION( )
	ON_WM_SYSCOMMAND( )
	ON_COMMAND(WM_DISPLAY, OnDisplay)
	ON_COMMAND(WM_MYCLOSE,OnMyClose)
	ON_COMMAND(WM_MYSET,OnMySet)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClockDlg message handlers

BOOL CClockDlg::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
	//初始化时间间隔
//    m_spin.SetRange32(1,150);
//
//    m_spin.SetPos(5);
//
//	m_interval = m_spin.GetPos();

	CSpinButtonCtrl * Spin;
	Spin = (CSpinButtonCtrl *)GetDlgItem(IDC_SPIN);
	Spin->SetRange(1,150);
	m_interval = 30;
    CDialog::OnInitDialog();
	// 设置定时器,每秒一次心跳
	SetTimer(1,1000, NULL);

	// 将图标放入系统托盘
   //  NOTIFYICONDATA nd;
     nd.cbSize = sizeof(NOTIFYICONDATA); //以字节计的结构大小,以适应不同版本。
	 nd.hWnd = m_hWnd; //接收Windows消息的窗口句柄
	 nd.uID = IDI_ICON;//托盘图标的ID。
	 nd.uFlags =  NIF_ICON|NIF_MESSAGE|NIF_TIP;//指示结构中的哪些成员包含有效数据,可选值:NIF_ICON, NIF_MESSAGE,NIF_TIP,NIF_STATE,NIF_INFO,NIF_GUID。
	 nd.uCallbackMessage = WM_NOTIFYICON;//回调消息ID,由用户自定义。与一个自定义的消息处理函数关联
	 nd.hIcon = m_hIcon;//托盘图标的句柄
	 strcpy(nd.szTip,"我的小闹钟");
	 
	 Shell_NotifyIcon(NIM_ADD,&nd);

//	 UpdateData(FALSE);
	// TODO: Add extra initialization here

   

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CClockDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
		// 屏蔽最大化(MFC Bug),将最小化重定向至隐藏窗口
	if (nID == SC_MAXIMIZE)
		return;
	if (nID == SC_MINIMIZE)
		ShowWindow(SW_HIDE);
	else
		CWnd::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 CClockDlg::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 CClockDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CClockDlg::OnApply() 
{
	// TODO: Add your control notification handler code here
	// 重置时间间隔
	UpdateData();

	OnTimer(IDT_APPLY);

	CClockDlg::OnHide();
}

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

void CClockDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
		// 释放定时器
	KillTimer(1);

	// 将图标从系统托盘中删除
	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;
	nd.hIcon = m_hIcon;
	Shell_NotifyIcon(NIM_DELETE, &nd);

	CDialog::OnCancel();
}

void CClockDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	static CString  strTemp;
	static int Count = 0;
	COleDateTime dtTime;
   	// 刷新时间
	dtTime = COleDateTime::GetCurrentTime();
    strTemp.Format("现在时间: %02i:%02i:%02i",dtTime.GetHour(),dtTime.GetMinute(),dtTime.GetSecond());
	GetDlgItem(IDC_CURTIME)->SetWindowText(strTemp);
    
	// 刷新剩余时间值
	if(nIDEvent == IDT_APPLY) Count = 0;
	 Count ++;
	 int RestTime = 0;
	 RestTime = m_interval - Count/60;
	 if(RestTime <= 0)
	 {
		 Count = 0;
		ShowMessage();
	 }
	 strTemp.Format("离下次提醒还差 %i 分钟",RestTime);
	 GetDlgItem(IDC_RESTTIME)->SetWindowText(strTemp);
	
	CDialog::OnTimer(nIDEvent);
}

afx_msg void CClockDlg::NotifyIcon(WPARAM wParam, LPARAM lParam)
{
  // 响应在托盘图标上的单击
	if ((wParam == IDI_ICON)&&(lParam == WM_LBUTTONDOWN))
		
		ShowWindow(SW_SHOWNORMAL);

	if((wParam == IDI_ICON) && (lParam == WM_RBUTTONDOWN))
	{
			//将窗口设置为前置窗口,防止托盘菜单一直显示
	this->SetForegroundWindow();
	CMenu menu;
	LPPOINT lpoint = new  tagPOINT;
	::GetCursorPos(lpoint);
	//创建弹出菜单
	menu.CreatePopupMenu();
	menu.AppendMenu(MF_STRING,WM_MYSET,"软件设置");
	menu.AppendMenu(MF_STRING,WM_DISPLAY, "显示主窗口");     //自定义消息
	menu.AppendMenu(MF_STRING, WM_MYCLOSE, "退出");  //自定义消息
		menu.TrackPopupMenu(TPM_LEFTALIGN, lpoint->x, lpoint->y, this );
	HMENU hmenu;
	hmenu = menu.Detach();
	menu.DestroyMenu();
	delete lpoint;
	}

   
}
void CClockDlg::OnDisplay()
{
	//显示主窗体
	this->ShowWindow(SW_SHOW);
}
void CClockDlg::OnMyClose()
{
	//销毁主窗体
	if(falg == 0 )
	{
	  int n = MessageBox("下次启动时,是否自动运行Clock ?","启动选项",MB_OKCANCEL|MB_ICONQUESTION);
      if(n == 1)
          falg = 1;
	}
//	int n = MessageBox("真的退出吗?","提示!",MB_OKCANCEL|MB_ICONEXCLAMATION);
//	if(n!=1)
//		return;

	CClockDlg::OnCancel();
}
BOOL CClockDlg::OnQueryEndSession()
{
	// 在用户退出Windows时自动退出应用程序

	CClockDlg::OnCancel();

	return TRUE;
}
void CClockDlg::OnMySet()
{
    setdlg.DoModal();
	
	return;
}
void CClockDlg::ShowMessage()
{
	::WinExec("C:\\INFOCD\\WINAMP\\WINAMP.EXE music.m3u",SW_HIDE);
	MessageBox("您该休息一会儿了......", "休息", MB_SYSTEMMODAL|MB_OK|MB_ICONEXCLAMATION|MB_ICONWARNING);
	ShellExecute(m_hWnd,"open","C:\\WINDOWS\\SYSTEM\\太空.scr", NULL,NULL,SW_SHOWNORMAL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -