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

📄 stockreferview.cpp

📁 本程序是VC为平台开发的股票资讯系统
💻 CPP
字号:
// StockReferView.cpp : implementation of the CStockReferView class
//

#include "stdafx.h"
#include "StockRefer.h"

#include "StockReferDoc.h"
#include "StockReferView.h"

#include "MainFrm.h"
#include "NewsContent.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStockReferView

IMPLEMENT_DYNCREATE(CStockReferView, CEditView)

BEGIN_MESSAGE_MAP(CStockReferView, CEditView)
	//{{AFX_MSG_MAP(CStockReferView)
	ON_WM_SETFOCUS()
	ON_WM_CHAR()
	ON_WM_SETCURSOR()
	ON_WM_KEYDOWN()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_EDIT_COPYS, OnEditCopys)
	ON_COMMAND(ID_EDIT_PROPERTY, OnEditProperty)
	ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStockReferView construction/destruction

CStockReferView::CStockReferView()
{
	// TODO: add construction code here

}

CStockReferView::~CStockReferView()
{
}

BOOL CStockReferView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	BOOL bPreCreated = CEditView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);	// Enable word-wrapping

	return bPreCreated;
}

/////////////////////////////////////////////////////////////////////////////
// CStockReferView drawing

void CStockReferView::OnDraw(CDC* pDC)
{
	CStockReferDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CStockReferView printing

BOOL CStockReferView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default CEditView preparation
	return CEditView::OnPreparePrinting(pInfo);
}

void CStockReferView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView begin printing.
	CEditView::OnBeginPrinting(pDC, pInfo);
}

void CStockReferView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView end printing
	CEditView::OnEndPrinting(pDC, pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// CStockReferView diagnostics

#ifdef _DEBUG
void CStockReferView::AssertValid() const
{
	CEditView::AssertValid();
}

void CStockReferView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

CStockReferDoc* CStockReferView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStockReferDoc)));
	return (CStockReferDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStockReferView message handlers

void CStockReferView::OutFile(LPCTSTR lpszTitle)
{
	CString title = lpszTitle;
	if(title.IsEmpty())
	{
		strCurContext[iCurSel].Empty();
		UpdateString(_T(""));
	}
	else
	{
		CString	m_txt;
		CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
		ASSERT(pFrame);
		CContainView *pView = pFrame->GetContainView();
		ASSERT(pView);
		CNewsContent item(&pView->m_ContentDb);
		try
		{
			CString sql;
			sql.Format(_T("SELECT * FROM list%d WHERE col1 = '%s'"),
				iCurSel,title);

			item.Open(dbOpenDynaset,sql,0);
			if(!item.IsEOF())
			{
				m_txt = title;
				m_txt += _T("\n");
				m_txt += item.m_content;
				m_txt.Replace(_T("\n"),_T("\r\n"));
				strCurContext[iCurSel] = m_txt;
				UpdateString(m_txt);
			}
			item.Close();
		}
		catch(CDaoException *e)
		{
			if(item.IsOpen()) item.Close();
			e->Delete();

			AfxMessageBox(IDS_PROGRAM_ERROR,MB_ICONSTOP);
		}
	}
}

void CStockReferView::OnSetFocus(CWnd* pOldWnd) 
{
//	CEditView::OnSetFocus(pOldWnd);
	
	// TODO: Add your message handler code here
	
}

void CStockReferView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
//	CEditView::OnChar(nChar, nRepCnt, nFlags);
}

BOOL CStockReferView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
	
	return TRUE;
}

void CStockReferView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
    BOOL processed = FALSE;
	processed=KeyScroll(nChar);
}

void CStockReferView::FontChanged() 
{
	// TODO: Add your command handler code here
	CFont* pFont = GetFont();
	if (pFont != NULL)
		pFont->GetObject(sizeof(LOGFONT), &lfFont);
	else
		::GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lfFont);
	
	CFontDialog dlg(&lfFont, CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT);
	if(dlg.DoModal() == IDOK)
	{
		// switch to new font.
		m_font.DeleteObject();
		if (m_font.CreateFontIndirect(&lfFont))
		{
			CWaitCursor wait;
			SetFont(&m_font);
		}
	}
}

void CStockReferView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	CCJMenu menu;
	VERIFY(menu.LoadMenu(IDR_POPUP_NEWSVIEW));

	CCJMenu* pPopup = (CCJMenu*)menu.GetSubMenu(0);
	ASSERT(pPopup != NULL);

	pPopup->LoadToolbar(IDR_MAINFRAME);

	ASSERT_VALID(this);
	int nStartChar, nEndChar;
	GetEditCtrl().GetSel(nStartChar, nEndChar);
	if(nStartChar == nEndChar)
		pPopup->EnableMenuItem(ID_EDIT_COPYS,MF_BYCOMMAND|MF_GRAYED);
	if(GetWindowTextLength() == 0)
		pPopup->EnableMenuItem(ID_EDIT_SELECT_ALL,MF_BYCOMMAND|MF_GRAYED);
	ASSERT_VALID(this);

	CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
	ASSERT(pFrame);
	if(!pFrame->CanExport())
	{
		pPopup->EnableMenuItem(ID_FILE_EXPORT,MF_BYCOMMAND|MF_GRAYED);
		pPopup->EnableMenuItem(ID_FILE_PRINT,MF_BYCOMMAND|MF_GRAYED);
		pPopup->EnableMenuItem(ID_FILE_PRINT_PREVIEW,MF_BYCOMMAND|MF_GRAYED);
	}

	CWnd* pWndPopupOwner = this;
	while(pWndPopupOwner->GetStyle() & WS_CHILD)
		pWndPopupOwner = pWndPopupOwner->GetParent();

	pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON,
		point.x, point.y, this);

	menu.DestroyMenu();
}

BOOL CStockReferView::KeyScroll(UINT nChar)
{
	switch (nChar)
    {
		case VK_UP:
			OnVScroll(SB_LINEUP,0,NULL);
            break;
        case VK_DOWN:
            OnVScroll(SB_LINEDOWN,0,NULL);
            break;
        case VK_PRIOR:
            OnVScroll(SB_PAGEUP,0,NULL);
            break;
        case VK_NEXT:
            OnVScroll(SB_PAGEDOWN,0,NULL);
            break;
        default:
            return FALSE; // not for us
                          // and let the default class
                          // process it.
	}

	return TRUE;
}

void CStockReferView::UpdateString(LPCTSTR lpszString)
{
	GetEditCtrl().SetWindowText(lpszString);
}

void CStockReferView::UpdateNewsContent(int iCurNewsItem)
{
	UpdateString(strCurContext[iCurNewsItem]);
}

void CStockReferView::EditCopys()
{
	GetEditCtrl().Copy();
}

BOOL CStockReferView::CanActivateCopy()
{
	ASSERT_VALID(this);
	int nStartChar, nEndChar;
	GetEditCtrl().GetSel(nStartChar, nEndChar);
	ASSERT_VALID(this);
	if(nStartChar == nEndChar)
		return FALSE;
	else
		return TRUE;
}

void CStockReferView::OnEditCopys() 
{
	// TODO: Add your command handler code here
	EditCopys();
}

BOOL CStockReferView::CanExport()
{
	CString m_strText;
	GetEditCtrl().GetWindowText(m_strText);

	if(m_strText.IsEmpty()) return FALSE;
	else
		return TRUE;
}

void CStockReferView::OnEditProperty() 
{
	// TODO: Add your command handler code here
	FontChanged();
}


BOOL CStockReferView::AcceptBody(int iCurNewsItem, LPCTSTR lpszBody)
{
	CString stoday,title,tofind,body;
	int i;

	body = lpszBody;
	i = body.Find(_T("\n"));
	if(i == -1) return FALSE;
	stoday = body.Left(i);
	body = body.Mid(i + 1);
	i = body.Find(_T("\n"));
	if(i == 0) return FALSE;
	title = body.Left(i);
	body = body.Mid(i + 1);
	body.Replace(_T("|"),_T("\7c"));

	CNewsContent item(&m_ContentData);
	try
	{
		CString sql;
		sql.Format(_T("SELECT * FROM list%d ORDER BY col3"),
			iCurNewsItem + 1);
		item.Open(dbOpenDynaset,sql,0);
		tofind.Format(_T("col1 = '%s'"),title);
		if(!item.FindFirst(tofind))
		{
			sql.Format(_T("INSERT INTO list%d(col1,col2,col3) VALUES('%s','%s',#%s#)"),
				iCurNewsItem + 1,title,body,stoday);
			item.m_pDatabase->Execute(sql);
			CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
			ASSERT(pFrame);
			CString str;
			str.Format(IDS_RECEIVE_NEWS,title);
			pFrame->SetPaneText(1,str);
			pFrame->GetContainView()->AddTreeItem(iCurNewsItem,title);
			item.Close();

			return TRUE;
		}
		else
		{
			item.Close();
		
			return FALSE;
		}
	}
	catch(CDaoException *ex)
	{
		if(item.IsOpen()) item.Close();
		ex->Delete();

		return FALSE;
	}
}

void CStockReferView::SaveToFile()
{
	char ch = '%';
	static char BASED_CODE szFilter[] = _T("Web 页 (*.htm)|*.htm||");
	CString str;
	GetEditCtrl().GetWindowText(str);
	if(str.IsEmpty())
	{
		AfxMessageBox(_T("请选择被保存的资讯。"));
		return;
	}
	CFileDialog fdlg(FALSE,_T("htm"),NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	if(fdlg.DoModal() == IDOK)
	{
		CString title;
		FILE *fp;
		int ki;
		fp = fopen(fdlg.GetPathName(),_T("w"));
		if(fp == NULL){
			AfxMessageBox(_T("保存文件时出错。"),MB_ICONSTOP);
			return;
		}
		fprintf(fp,_T("<html>\n\n"));
		fprintf(fp,_T("<head>\n"));
		ki = str.Find('\n');
		if(ki == -1){
			AfxMessageBox(_T("保存文件时出错。"),MB_ICONSTOP);
			return;
		}

		title = str.Left(ki);
		fprintf(fp,_T("<title>%s</title>\n"),title);
		str = str.Mid(ki + 1);
		fprintf(fp,_T("</head>\n\n"));
		fprintf(fp,_T("<body>\n\n"));
		fprintf(fp,_T("<table border=\"0\" width=\"100%c\" cellpadding=\"2\">\n"),ch);
		fprintf(fp,_T("  <tr>\n"));
		fprintf(fp,_T("    <td width=\"20%c\"></td>\n"),ch);
		fprintf(fp,_T("    <td width=\"60%c\">\n"),ch);
		fprintf(fp,_T("      <p align=\"center\"><b>%s</b></td>\n"),title);
		fprintf(fp,_T("    <td width=\"20%c\"></td>\n"),ch);
		fprintf(fp,_T("  </tr>\n"));
		fprintf(fp,_T("  <tr>\n"));
		fprintf(fp,_T("    <td width=\"20%c\"></td>\n"),ch);
		fprintf(fp,_T("    <td width=\"60%c\">\n"),ch);
		fprintf(fp,_T("      <p align=\"left\">\n"));
		ki = str.Find('\n');
		while(ki != -1){
			fprintf(fp,_T("%s<br>\n"),str.Left(ki));
			str = str.Mid(ki + 1);
			ki = str.Find('\n');
		}
		fprintf(fp,_T("      </td>\n"));
		fprintf(fp,_T("    <td width=\"20%c\"></td>\n"),ch);
		fprintf(fp,_T("  </tr>\n"));
		fprintf(fp,_T("  <tr>\n"));
		fprintf(fp,_T("    <td width=\"20%c\" height=\"20\"></td>\n"),ch);
		fprintf(fp,_T("    <td width=\"60%c\" height=\"20\"></td>\n"),ch);
		fprintf(fp,_T("    <td width=\"20%c\" height=\"20\"></td>\n"),ch);
		fprintf(fp,_T("  </tr>\n"));
		fprintf(fp,_T("  <tr>\n"));
		fprintf(fp,_T("    <td width=\"20%c\" rowspan=\"2\"></td>\n"),ch);
		fprintf(fp,_T("    <td width=\"60%c\">\n"),ch);
		fprintf(fp,_T("      <p align=\"center\"><font size=\"2\">Copyright &copy; 2000-2001 All Rights \n"));
		fprintf(fp,_T("      Reserved</font></td>\n"));
		fprintf(fp,_T("    <td width=\"20%c\" rowspan=\"2\"></td>\n"),ch);
		fprintf(fp,_T("  </tr>\n"));
		fprintf(fp,_T("  <tr>\n"));
		fprintf(fp,_T("    <td width=\"60%c\">\n"),ch);
		fprintf(fp,_T("      <p align=\"center\"><font size=\"2\">新易信息产业有限公司 \n"));
		fprintf(fp,_T("      版权所有 &copy; 2000-2001&nbsp;</font></td>\n"));
		fprintf(fp,_T("  </tr>\n"));
		fprintf(fp,_T("</table>\n"));
		fprintf(fp,_T("<p>&nbsp;</p>\n"));
		fprintf(fp,_T("<p><br>\n"));
		fprintf(fp,_T("<br>\n"));
		fprintf(fp,_T("</p>\n"));
		fprintf(fp,_T("\n"));
		fprintf(fp,_T("</body>\n"));
		fprintf(fp,_T("\n"));
		fprintf(fp,_T("</html>\n"));
		fclose(fp);
	}
}

void CStockReferView::ExportToFile()
{
	if(!HAS_REG)
	{
		AfxMessageBox(_T("您的软件还没有注册,该功能只对注册用户开放。"));
		return;
	}
	
	SaveToFile();
}

void CStockReferView::OnFileExport() 
{
	// TODO: Add your command handler code here
	ExportToFile();
}

void CStockReferView::UpdateInfoContent()
{
	UpdateString(strCurContext[iCurSel]);
}

void CStockReferView::FilePrint()
{
	OnFilePrint();
}

void CStockReferView::FilePrintPreview()
{
	OnFilePrintPreview();
}

⌨️ 快捷键说明

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