📄 anyquotedlg.cpp
字号:
// AnyQuoteDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AnyQuote.h"
#include "AnyQuoteDlg.h"
#include "CRobot.h"
#include "CRobotInternet.h"
#include "ParseHtml.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()
////////////////////////////////////////////////////////////////////
// CAnyQuoteDlg dialog
CAnyQuoteDlg::CAnyQuoteDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAnyQuoteDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAnyQuoteDlg)
m_urls = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAnyQuoteDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAnyQuoteDlg)
DDX_Control(pDX, IDC_URLS, m_urls_ctl);
DDX_CBString(pDX, IDC_URLS, m_urls);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAnyQuoteDlg, CDialog)
//{{AFX_MSG_MAP(CAnyQuoteDlg)
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, CAnyQuoteDlg::OnMenuUpdate)
ON_COMMAND(IDC_CONFIGURE, CAnyQuoteDlg::OnMenuConfigure)
ON_COMMAND(IDC_EXIT, CAnyQuoteDlg::OnMenuExit)
END_MESSAGE_MAP()
////////////////////////////////////////////////////////////////////
// CAnyQuoteDlg message handlers
BOOL CAnyQuoteDlg::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
nSites = 0;
LoadURLs();
// Put agent in the Windows system tray
nTrayData.cbSize = sizeof(NOTIFYICONDATA) ;
nTrayData.hWnd = m_hWnd;
nTrayData.uID = 0 ;
nTrayData.hIcon = LoadIcon (AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME));
nTrayData.uCallbackMessage = WM_TRAY_NOTIFY;
strcpy (nTrayData.szTip, "AnyQuote");
nTrayData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
Shell_NotifyIcon (NIM_ADD, &nTrayData);
bHidden = false;
if (nSites > 0)
{
ShowWindow(SW_MINIMIZE);
SetTimer(1, 1000, NULL);
} // End if
return TRUE; // return TRUE unless you set the focus to a control
}
void CAnyQuoteDlg::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 CAnyQuoteDlg::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 CAnyQuoteDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAnyQuoteDlg::OnAdd()
{
// Validate the input
UpdateData(true);
int nPos = m_urls.Find(" ");
if (m_urls == "" || nPos == -1)
{
MessageBox("Please enter a ticker symbol,"
" a space, and a URL.",
"Ticker and URL Required",
MB_ICONEXCLAMATION);
return;
} // End if
if (nSites == MAX_SITES)
{
MessageBox("The maximum number of sites is in use.",
"Out of Room",
MB_ICONEXCLAMATION);
return;
} // End if
// Add the site
CWaitCursor wc;
sSiteSymbol[nSites] = m_urls.Left(nPos);
sSiteURL[nSites] = m_urls.Mid(nPos + 1);
m_urls_ctl.AddString(sSiteSymbol[nSites] + " "
+ sSiteURL[nSites]);
nSites++;
m_urls = "";
UpdateData(false);
GetDlgItem(IDC_URLS)->SetFocus();
}
void CAnyQuoteDlg::OnRemove()
{
CString sSymbol, sURL;
UpdateData(true);
int nPos = m_urls.Find(" ");
if (nPos == -1) return;
sSymbol = m_urls.Left(nPos);
sURL = m_urls.Mid(nPos + 1);
int nSiteIndex = -1;
for (int nSite = 0; nSite < nSites; nSite++)
{
if (sSiteSymbol[nSite] == sSymbol && sSiteURL[nSite] == sURL)
{
nSiteIndex = nSite;
nSite = nSites;
} // End if
} // End for
if (nSiteIndex == -1)
{
MessageBox("You cannot remove " + sSymbol + " / " + sURL
+ " because it is not in the list.",
"Not Found",
MB_ICONEXCLAMATION);
return;
} // End if
CWaitCursor wc;
// Delete pair of ticker symbol and URL from site variables
for (nSite = nSiteIndex; nSite < nSites-1; nSite++)
{
sSiteSymbol[nSite] = sSiteSymbol[nSite + 1];
sSiteURL[nSite] = sSiteURL[nSite + 1];
} // End for
sSiteSymbol[nSites].Empty();
sSiteURL[nSites].Empty();
nSites--;
// Delete pair of ticker symbol and URL from the list box
m_urls_ctl.DeleteString(m_urls_ctl.GetCurSel());
}
void CAnyQuoteDlg::OnOK()
{
if (nSites == 0)
{
MessageBox("You must enter one or more "
"symbols and URLs first.",
"No Symbol-URL Pairs Entered",
MB_ICONEXCLAMATION);
return;
} // End if
ShowWindow(SW_HIDE);
SaveURLs();
SetTimer(1, 1000, NULL);
}
void CAnyQuoteDlg::OnTimer(UINT nIDEvent)
{
KillTimer(1);
if (!bHidden)
{
ShowWindow(SW_HIDE);
bHidden = true;
} // End if
GetQuotes();
SetTimer(1, 5 * 60 * 1000, NULL);
CDialog::OnTimer(nIDEvent);
}
// ***************
// * *
// * GetQuotes *
// * *
// ***************
// Description: Retrieves stock quote pages, interprets them, and
// constructs new tooltip text for the system tray icon
void CAnyQuoteDlg::GetQuotes()
{
// Check web pages
CRobotInternet internet;
CString sHTML;
int nResult;
CString sErrMsg;
CString sTooltip = "AnyQuote - ";
for (int nSite = 0; nSite < nSites; nSite++)
{
if (internet.httpGet(sSiteURL[nSite],
sHTML,
nResult,
sErrMsg))
{
sTooltip += sSiteSymbol[nSite] + "="
+ InterpretQuotePage(sHTML) + " ";
} // End if
} // End for nSite
nTrayData.cbSize = sizeof(NOTIFYICONDATA) ;
nTrayData.hWnd = m_hWnd;
nTrayData.uID = 0 ;
if (sTooltip.GetLength() > 63)
sTooltip = sTooltip.Left(60) + "...";
strcpy (nTrayData.szTip, sTooltip);
nTrayData.uFlags = NIF_TIP;
Shell_NotifyIcon (NIM_MODIFY, &nTrayData);
}
// ************************
// * *
// * InterpretQuotePage *
// * *
// ************************
// Description: Finds last price information from an HTML page
// and returns it
CString CAnyQuoteDlg::InterpretQuotePage(const CString& sHTML)
{
// Load table cells into an array
#define MAX_ROWS 200
#define MAX_COLS 200
CString sCell[MAX_ROWS][MAX_COLS];
CString sHtml = sHTML;
sHtml.MakeUpper();
ParseHtml(sHtml);
CString sItem;
BOOL bInTable = false;
int nRows = 0;
int nCols = 0;
int nRow = -1;
int nCol = -1;
int nTables = 0;
for (int e = 0; e < nElements; e++)
{
sItem = sElement[e];
if (sItem.Left(1) == "<")
{
if (sItem.Left(6) == "<TABLE")
{
bInTable = true;
nTables++;
} // End if
if (sItem.Left(7) == "</TABLE")
{
nTables--;
if (nTables == 0)
bInTable = false;
}
if (bInTable)
{
if (sItem.Left(3) == "<TR")
{
nRow++;
if (nRow + 1 > nRows)
nRows = nRow + 1;
nCol = -1;
} // End if
if (sItem.Left(3) == "<TD" || sItem.Left(3) == "<TH")
{
nCol++;
if (nCol + 1 > nCols)
nCols = nCol + 1;
sCell[nRow][nCol] = "";
} // End if
} // End if bInTable
} // End if
else
{
if (bInTable)
{
sItem.TrimLeft();
sItem.TrimRight();
sCell[nRow][nCol] += sItem;
} // End if
} // End else
} // End for e
// Table cells are now stored in sCell[0][0] through
// sCell[nRows-1][nCols-1]
// Search for Last Price
CString sValue1, sValue2, sValue3;
for (int r = 0; r < nRows; r++)
{
for (int c = 0; c < nCols; c++)
{
sItem = sCell[r][c];
if (sItem.Find("LAST") != -1 &&
sItem.Find("TIME") == -1 &&
sItem.Find("DELAY") == -1)
{
sValue1 = PriceOnly(sItem);
sValue2 = PriceOnly(sCell[r][c + 1]);
sValue3 = PriceOnly(sCell[r + 1][c]);
if (sValue1 != "") return sValue1;
if (sValue2 != "") return sValue2;
if (sValue3 != "") return sValue3;
} // End if
} // End for c
} // End for r
return "?";
}
// ***************
// * *
// * PriceOnly *
// * *
// ***************
// Description: Returns a copy of a string
// with nonprice characters removed
CString CAnyQuoteDlg::PriceOnly(CString sCell)
{
CString sTemp = sCell;
// Remove nonnumeric leading characters
while (sTemp != ""
&& (sTemp.GetAt(0) < '0' || sTemp.GetAt(0) > '9'))
sTemp = sTemp.Mid(1);
while (sTemp != ""
&& (sTemp.GetAt(sTemp.GetLength() - 1) < '0'
|| sTemp.GetAt(sTemp.GetLength()-1) > '9'))
sTemp = sTemp.Left(sTemp.GetLength() - 1);
if (sTemp == "")
return "";
char c = sTemp.GetAt(0);
if (c < '0' || c > '9')
return "";
else
return sTemp;
}
// ******************
// * *
// * OnTrayNotify *
// * *
// ******************
// Tray notification handler
LRESULT CAnyQuoteDlg::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);
}
void CAnyQuoteDlg::OnMenuUpdate()
{
KillTimer(1);
GetQuotes();
SetTimer(1, 5 * 60 * 1000, NULL);
}
void CAnyQuoteDlg::OnMenuConfigure()
{
KillTimer(1);
ShowWindow(SW_NORMAL);
}
void CAnyQuoteDlg::OnMenuExit()
{
Shell_NotifyIcon (NIM_DELETE, &nTrayData);
EndDialog(0);
}
// **************
// * *
// * LoadURLs *
// * *
// **************
// Description: Loads list of ticker symbols and URLs
// from AnyQuote.dat
//
// Outputs: nSites ........ Number of ticker symbols and URLs
// sSiteSymbol[] . Array of ticker symbols
// sSiteURL[] .... Array of URLs
void CAnyQuoteDlg::LoadURLs()
{
CStdioFile file;
CString sLine;
nSites = 0;
m_urls_ctl.ResetContent();
if (file.Open("AnyQuote.dat", CFile::modeRead))
{
while (file.ReadString(sLine))
{
sSiteSymbol[nSites] = sLine;
file.ReadString(sSiteURL[nSites]);
m_urls_ctl.AddString(sSiteSymbol[nSites] + " "
+ sSiteURL[nSites]);
nSites++;
} // End while
file.Close();
} // End if
}
// **************
// * *
// * SaveURLs *
// * *
// **************
// Description: Saves list of ticker symbols and URLs for the sake
// of future sessions.
// Creates the file AnyQuote.dat and writes one ticker
// symbol to a line and one URL to a line per site.
//
// Inputs: nSites ........ Number of URLs
// sSiteURL[] .... Contains the actual URLs
void CAnyQuoteDlg::SaveURLs()
{
CStdioFile file;
file.Open("AnyQuote.dat", CFile::modeCreate|CFile::modeWrite);
for (int u = 0; u < nSites; u++)
{
file.WriteString(sSiteSymbol[u] + "\n");
file.WriteString(sSiteURL[u] + "\n");
} // End for
file.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -