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

📄 snitchdlg.cpp

📁 Visual C++自动查询和智能代理程序设计书籍源码-Snitch
💻 CPP
字号:
// SnitchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Snitch.h"
#include "SnitchDlg.h"
#include "CRobot.h"
#include "CRobotMail.h"

#define	MAX_WINDOWS	1000

int g_nWindows;
CString g_sWindowName[MAX_WINDOWS];
CString g_sNewWindows;
BOOL g_bInTheOffice;

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

/////////////////////////////////////////////////////////////////////////////
// CSnitchDlg dialog

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

void CSnitchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSnitchDlg)
	DDX_Text(pDX, IDC_MAIL, m_mail);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSnitchDlg, CDialog)
	//{{AFX_MSG_MAP(CSnitchDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAY_NOTIFY, OnTrayNotify)
	ON_COMMAND(IDC_IN, CSnitchDlg::OnMenuIn)
	ON_COMMAND(IDC_OUT, CSnitchDlg::OnMenuOut)
	ON_COMMAND(IDC_CONFIGURE, CSnitchDlg::OnMenuConfigure)
	ON_COMMAND(IDC_EXIT, CSnitchDlg::OnMenuExit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnitchDlg message handlers

BOOL CSnitchDlg::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
	
	// Put agent in the Windows system tray

	m_nTrayData.cbSize = sizeof(NOTIFYICONDATA) ;
	m_nTrayData.hWnd = m_hWnd;
	m_nTrayData.uID = 0 ;
	m_nTrayData.hIcon = LoadIcon (AfxGetInstanceHandle(),
								  MAKEINTRESOURCE(IDI_IN));
	m_nTrayData.uCallbackMessage = WM_TRAY_NOTIFY;
	strcpy (m_nTrayData.szTip, "Snitch (In The Office)");
	m_nTrayData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	Shell_NotifyIcon (NIM_ADD, &m_nTrayData);

	LoadEmail();
	m_bHidden = false;
	g_bInTheOffice = true;

	if (m_mail != "")
	{
		ShowWindow(SW_MINIMIZE);
		SetTimer(1, 1000, NULL);
	} // End if
	
	return TRUE; // return TRUE unless you set the focus to a control
}

void CSnitchDlg::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 CSnitchDlg::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 CSnitchDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSnitchDlg::OnOK() 
{
	UpdateData(true);
	if (m_mail == "")
	{
		MessageBox("A pager e-mail address must be entered.",
				   "Pager E-mail Address Required", MB_ICONEXCLAMATION);
		GetDlgItem(IDC_MAIL)->SetFocus();
		return;
	} // End if
	ShowWindow(SW_HIDE);
	SaveEmail();
	SetTimer(1, 1000, NULL);
}

void CSnitchDlg::OnTimer(UINT nIDEvent) 
{
	KillTimer(1);
	if (!m_bHidden)
	{
		ShowWindow(SW_HIDE);
		m_bHidden = true;
	} // End if
	CheckUsage();
	SetTimer(1, 60 * 1000, NULL);
	
	CDialog::OnTimer(nIDEvent);
}

// ******************
// *                *
// *  OnTrayNotify  *
// *                *
// ******************
// Tray notification handler

LRESULT CSnitchDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
	// ---- Right-button down: Pop-up menu ----
	if (lParam == WM_RBUTTONDOWN) {
		CMenu menu;
		VERIFY(menu.LoadMenu(IDR_MENU1));
		CMenu* pPopup = menu.GetSubMenu(0);
		ASSERT(pPopup != NULL); 
		CRect screen;
		GetDesktopWindow()->GetWindowRect(screen);
		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
							   screen.right,
							   screen.bottom,
							   AfxGetMainWnd());
	} // End if
	return LRESULT(0);
}

// *********************
// *                   *
// *  EnumWindowsProc  *
// *                   *
// *********************
// Description:	Called by EnumWindows function for each window

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
	char name[500];
	BOOL bFound;
	GetWindowText(hwnd, name, 499);
	CString sName(name);
	sName.TrimLeft();
	sName.TrimRight();
	if (sName != "")
	{
		if (g_bInTheOffice)
		{
			g_sWindowName[g_nWindows] = sName;
			if (g_nWindows<MAX_WINDOWS) g_nWindows++;
		} // End if
		else
		{
			bFound = false;
			for (int w = 0; w < g_nWindows; w++)
			{
				if (sName == g_sWindowName[w])
				{
					bFound = true;
					w = g_nWindows;
				} // End if
			} // End for
			if (!bFound)
			{
				g_sNewWindows += sName + "\n";
				g_sWindowName[g_nWindows] = sName;
				if (g_nWindows < MAX_WINDOWS) g_nWindows++;
			} // End if
		} // End else
	} // End if
	return true;
}

// ****************
// *              *
// *  CheckUsage  *
// *              *
// ****************
// Description:	If the user is out of the office, check for
//				application usage. If any is found, send an
//				e-mail to the user reporting the activity.

void CSnitchDlg::CheckUsage()
{
	if (g_bInTheOffice)
		g_nWindows = 0;
	else
		g_sNewWindows = "";
	EnumWindows(EnumWindowsProc, NULL);
	if (!g_bInTheOffice && g_sNewWindows != "")
	{
		CRobotMail mail;
		CString sMsg = "These applications/tasks are active "
					   "on your computer:\n\n" + g_sNewWindows;
		mail.SendMessage(m_mail, "Snitch alert", sMsg, "");
	} // End if
}

// ***************
// *             *
// *  LoadEmail  *
// *             *
// ***************
// Description:	Loads e-mail address from the previous session.
//				Opens file Snitch.dat and reads e-mail address from
//				the first line.
//
// Outputs:		m_mail ... Dialog member variable for e-mail address

void CSnitchDlg::LoadEmail()
{
	CStdioFile file;
	CString sLine;

	if (file.Open("Snitch.dat", CFile::modeRead))
	{
		file.ReadString(m_mail);
		UpdateData(false);
		file.Close();
	} // End if
}

// ***************
// *             *
// *  SaveEmail  *
// *             *
// ***************
// Description:	Saves e-mail address for the sake of future sessions
//
// Inputs:		m_mail ... Dialog member variable for e-mail address

void CSnitchDlg::SaveEmail()
{
	CStdioFile file;

	file.Open("Snitch.dat", CFile::modeCreate | CFile::modeWrite);
	file.WriteString(m_mail + "\n");
	file.Close();
}

void CSnitchDlg::OnMenuIn()
{
	g_bInTheOffice = true;
	SetTray(IDI_IN, "In The Office");
}

void CSnitchDlg::OnMenuOut()
{
	g_bInTheOffice = false;
	SetTray(IDI_OUT, "Out Of The Office");
}

void CSnitchDlg::OnMenuConfigure()
{
	KillTimer(1);
	ShowWindow(SW_NORMAL);
}

void CSnitchDlg::OnMenuExit()
{
	Shell_NotifyIcon(NIM_DELETE, &m_nTrayData);
	EndDialog(0);
}

// *************
// *           *
// *  SetTray  *
// *           *
// *************
// Description: Changes icon and tooltip description

void CSnitchDlg::SetTray(int nWhichIcon, CString sStatus)
{
	m_nTrayData.hIcon = LoadIcon (AfxGetInstanceHandle(),
								  MAKEINTRESOURCE(nWhichIcon));
	strcpy (m_nTrayData.szTip, "Snitch - " + sStatus);
	m_nTrayData.uFlags = NIF_ICON | NIF_TIP;
	Shell_NotifyIcon (NIM_MODIFY, &m_nTrayData);
}

⌨️ 快捷键说明

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