MFC实现最小化到托盘(二)

2014-11-24 09:46:34 · 作者: · 浏览: 1
us to a control
}

void CTimeWakeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
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 CTimeWakeDlg::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 CTimeWakeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

LRESULT CTimeWakeDlg::OnShowTask(WPARAM wParam,LPARAM lParam)
{
if (wParam != IDR_MAINFRAME)
{
return 1;
}
switch(lParam)
{
case WM_RBUTTONUP:
{
LPPOINT lpoint = new tagPOINT;
::GetCursorPos(lpoint);
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING,WM_DESTROY,"关闭");
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
HMENU hmenu = menu.Detach();
menu.DestroyMenu();
delete lpoint;
this->DestroyWindow();
}
break;
case WM_LBUTTONDBLCLK:
{
this->ShowWindow(SW_SHOW);
Shell_NotifyIcon(NIM_DELETE,&nid);
}
break;
}
return 0;
}

void CTimeWakeDlg::OnSize(UINT nType,int cx,int cy)
{
CDialog::OnSize(nType,cx,cy);
if (nType == SIZE_MINIMIZED)
{
ShowWindow(SW_HIDE);
}
}

void CTimeWakeDlg::OnCancel()
{
CExit mExit;
if (IDOK == mExit.DoModal())
{

}
if (mExit.m_Radio == FALSE)
{
// MessageBox("最小到托盘");
nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd = this->m_hWnd;
nid.uID = IDR_MAINFRAME;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_SHOWTASK;
nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"计划任务提醒");//
Shell_NotifyIcon(NIM_ADD,&nid);
this->ShowWindow(SW_HIDE);
}
else
{
// MessageBox("关闭");
CDialog::OnCancel();
}
}


TimeWakeDlg.h
[cpp]
// TimeWakeDlg.h : header file
//

#if !defined(AFX_TIMEWAKEDLG_H__0D6108FA_D2EB_4C1E_8013_5D4C6617D62E__INCLUDED_)
#define AFX_TIMEWAKEDLG_H__0D6108FA_D2EB_4C1E_8013_5D4C6617D62E__INCLUDED_

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

/////////////////////////////////////////////////////////////////////////////
// CTimeWakeDlg dialog

class CTimeWakeDlg : public CDialog