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

📄 govtagentdlg.cpp

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

#include "stdafx.h"
#include "GovtAgent.h"
#include "GovtAgentDlg.h"
#include "CRobot.h"
#include "CRobotInternet.h"
#include "CRobotMail.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()

/////////////////////////////////////////////////////////////////////////////
// CGovtAgentDlg dialog

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

void CGovtAgentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGovtAgentDlg)
	DDX_Control(pDX, IDC_WORDS, m_words_ctl);
	DDX_Text(pDX, IDC_MAIL, m_mail);
	DDX_CBString(pDX, IDC_WORDS, m_words);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGovtAgentDlg, CDialog)
	//{{AFX_MSG_MAP(CGovtAgentDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAY_NOTIFY, OnTrayNotify)
	ON_COMMAND(IDC_UPDATE, CGovtAgentDlg::OnMenuUpdate)
	ON_COMMAND(IDC_CONFIGURE, CGovtAgentDlg::OnMenuConfigure)
	ON_COMMAND(IDC_EXIT, CGovtAgentDlg::OnMenuExit)
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////
// CGovtAgentDlg message handlers

BOOL CGovtAgentDlg::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
	
	nWords = 0;
	LoadWords();

	// 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_IDLE));
	m_nTrayData.uCallbackMessage = WM_TRAY_NOTIFY;
	strcpy (m_nTrayData.szTip, "Government Agent");
	m_nTrayData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	Shell_NotifyIcon (NIM_ADD, &m_nTrayData);

	bHidden = false;
	bBusy = false;

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

void CGovtAgentDlg::OnAdd() 
{
	// Validate the input

	UpdateData(true);

	if (m_words == "")
	{
		MessageBox("Please enter a word or phrase.",
				   "Word Required",
				   MB_ICONEXCLAMATION);
		return;
	} // End if

	if (nWords == MAX_WORDS)
	{
		MessageBox("The maximum number of words is in use.",
				   "Dictionary Full",
				   MB_ICONEXCLAMATION);
		return;
	} // End if

	// Add the word

	CWaitCursor wc;

	sWord[nWords] = m_words;

	m_words_ctl.AddString(m_words);

	nWords++;

	m_words = "";
	UpdateData(false);
	GetDlgItem(IDC_WORDS)->SetFocus();
}

void CGovtAgentDlg::OnRemove() 
{
	UpdateData(true);
	CString sWordToRemove = m_words;

	int nWordIndex = -1;
	for (int nWord = 0; nWord < nWords; nWord++)
	{
		if (sWord[nWord] == sWordToRemove)
		{
			nWordIndex = nWord;
			nWord = nWords;
		} // End if
	} // End for

	if (nWordIndex == -1)
	{
		MessageBox("You cannot remove "
				       + sWordToRemove
					   + " because it is not in the dictionary.",
				   "Not Found",
				   MB_ICONEXCLAMATION);
		return;
	} // End if

	CWaitCursor wc;

	// Delete word from dictionary

	for (nWord = nWordIndex; nWord < nWords - 1; nWord++)
	{
		sWord[nWord] = sWord[nWord + 1];
	} // End for
	sWord[nWords].Empty();
	nWords--;

	// Delete word from the list box

	m_words_ctl.DeleteString(m_words_ctl.GetCurSel());
}

void CGovtAgentDlg::OnOK() 
{
	UpdateData(true);
	if (m_mail == "")
	{
		MessageBox("An e-mail address must be entered.",
				   "E-mail Address Required",
				   MB_ICONEXCLAMATION);
		GetDlgItem(IDC_MAIL)->SetFocus();
		return;
	} // End if
	ShowWindow(SW_HIDE);
	SaveWords();
	SetTray(IDI_IDLE, "idle");
	SetTimer(1, 1000, NULL);
}

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

// ******************
// *                *
// *  ScanForBills  *
// *                *
// ******************
// Description: Retrieves congressional floor activity web page,
//				identifies bills, and searches bill descriptions for
//				keyword matches.
//				Sends mail messages about bills that fit the search
//				criteria.

void CGovtAgentDlg::ScanForBills()
{
	// Check web pages

	CRobotInternet internet;
	CRobotMail mail;
	CString sHTML;
	int nResult;
	CString sErrMsg;
	int nPos1, nPos2, nPos3, nPos4;
	CString sBillURL,
			sBillName,
			sBillDesc,
			sBillDescLower,
			sBillHTML;
	CString sFilename;
	CFileStatus fs;
	BOOL bScanning;
	BOOL bFoundBill;
	BOOL bMatch;
	CString sMatchingWords;
	CString sMsg;
	
	bBusy = true;
	SetTray(IDI_BUSY, "Active");

	// Expected form of bill references in HTML:
	//
	// <A HREF="{link}">S. nnn</A>
	// <A HREF="{link}">H.R. nnn</A>

	if (internet.httpGet("http://thomas.loc.gov/home/hot-week.html",
						 sHTML,
						 nResult,
						 sErrMsg))
	{
		// Read floor activity successfully - Now search it for bills
		bScanning = true;
		while(bScanning)
		{
			// Search for what might be a hyperlink to a bill
			nPos1 = sHTML.Find("<LI><a href=\"");
			if (nPos1 != -1)
				sHTML = sHTML.Mid(nPos1 + 13);
			nPos2 = sHTML.Find("\">");
			nPos3 = sHTML.Find("</a>");
			nPos4 = sHTML.Find("<LI>");
			if (nPos4 == -1)
				nPos4 = sHTML.Find("<EM>");
			if (nPos4 == -1)
				nPos4 = sHTML.GetLength();
			CString sMsg;
			if (nPos1 != -1 && nPos3 > nPos2)
			{
				// Separate bill name and URL
				sBillURL = sHTML.Left(nPos2);
				sBillName = sHTML.Mid(nPos2 + 2, nPos3 - nPos2 - 2);
				sBillDesc = sHTML.Mid(nPos3 + 4, nPos4 - nPos3 - 4);
				sHTML = sHTML.Mid(nPos3 + 4);
				bFoundBill = false;
				// Look for a Senate bill
				if (sBillName.Left(3) == "S. ")
				{
					bFoundBill = true;
				} // End if
				// Look for a House of Representatives bill
				if (sBillName.Left(5) == "H.R. ")
				{
					bFoundBill = true;
				} // End if
				if (bFoundBill)
				{
					// Remove trailing colon
					sBillName = sBillName.Left(sBillName.GetLength() 
											   - 1);
					
					sMatchingWords = "";
					sBillDescLower = sBillDesc;
					sBillDescLower.MakeLower();
					if (nWords == 0)
						bMatch = true;
					else
					{
						bMatch = false;
						for (int w = 0; w < nWords; w++)
						{
							if (sBillDesc.Find(sWord[w]) != -1
								|| sBillDescLower.Find(sWord[w])
								   != -1)
							{
								bMatch = true;
								if (sMatchingWords != "")
									sMatchingWords += "; ";
								sMatchingWords += sWord[w];
							} // End if
						} // End for
					} // End else

					if (bMatch)
					{
						sFilename = sBillName + ".htm";
						// If not already on file...
						if (!CFile::GetStatus(sFilename, fs))
						{
							if (internet.httpGetFile(sBillURL,
													 sFilename,
													 nResult,
													 sErrMsg))
							{
								if (sMatchingWords == "")
									sMsg = "A new congressional bill "
										   "has been detected."
										   "\r\n\r\n";
								else
									sMsg = "A new congressional bill"
										   " has been detected that "
										   "matches these keywords: "
										   " " + sMatchingWords 
										   + "\r\n\r\n";
								
								sMsg += "Bill name:  " + sBillName
										+ "\r\n\r\n";

								sMsg += "Bill link:  " + sBillURL
										+ "\r\n\r\n";

								sMsg += "Description: " + sBillDesc;

								sMsg += "---------\r\n";
								sMsg += "GovtAgent\r\n";
							
								mail.SendMessage(m_mail,
												 "New Bill "
												     + sBillName,
												 sMsg,
												 "");
									
							} // End if
							else
							{
								SetTray(IDI_ERR,
										"ERROR: "
										"Internet access failed");
							} // End else
						} // End if
					} // End if
				} // End if
			} // End if
			else
				bScanning = false;
		} // End while
		SetTray(IDI_IDLE, "Idle");
	} // End if
	else
	{
		SetTray(IDI_ERR, "ERROR: Internet Access Failed");
	} // End else
	sHTML.Empty();
	sBillHTML.Empty();
	bBusy = false;
}

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

LRESULT CGovtAgentDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
	if (bBusy) return 0;

	// ---- 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);
}

void CGovtAgentDlg::OnMenuUpdate()
{
	KillTimer(1);
	ScanForBills();
	SetTimer(1, 60 * 60 * 1000, NULL);
}

void CGovtAgentDlg::OnMenuConfigure()
{
	KillTimer(1);
	ShowWindow(SW_NORMAL);
	SetTray(IDI_ERR, "Suspended");
}

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

// ***************
// *             *
// *  LoadWords  *
// *             *
// ***************
// Description:	Loads e-mail address and dictionary (word list) from
//				the previous session.
//				Opens file GovtAgent.dat and reads e-mail address
//				from the first line, and one dictionary word per line
//				afterward.
//
// Outputs:		nWords ........ Number of words
//				sWord[] ....... Contains the actual words
//				m_mail ........ Dialog member variable for e-mail address

void CGovtAgentDlg::LoadWords()
{
	CStdioFile file;
	CString sLine;

	nWords = 0;
	m_words_ctl.ResetContent();

	if (file.Open("GovtAgent.dat", CFile::modeRead))
	{
		file.ReadString(m_mail);
		UpdateData(false);
		while (file.ReadString(sLine))
		{
			sWord[nWords] = sLine;
			nWords++;
			m_words_ctl.AddString(sLine);
		} // End while
		file.Close();
	} // End if
}

// ***************
// *             *
// *  SaveWords  *
// *             *
// ***************
// Description:	Saves e-mail address and dictionary (word list) for
//				the sake of future sessions.
//				Creates file GovtAgent.dat and writes e-mail address
//				to the first line, and one dictionary word per line
//				afterward.
//
// Inputs:		nWords ........ Number of words
//				sWord[] ....... Contains the actual words
//				m_mail ........ Dialog member variable for e-mail address

void CGovtAgentDlg::SaveWords()
{
	CStdioFile file;

	file.Open("GovtAgent.dat", CFile::modeCreate | CFile::modeWrite);
	file.WriteString(m_mail + "\n");
	for (int w = 0; w < nWords; w++)
	{
		sWord[w].TrimLeft();
		sWord[w].TrimRight();
		if (sWord[w] != "")
			file.WriteString(sWord[w] + "\n");
	} // End for
	file.Close();
}

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

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

⌨️ 快捷键说明

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