📄 containview.cpp
字号:
// ContainView.cpp : implementation file
//
#include "stdafx.h"
#include "stockrefer.h"
#include "ContainView.h"
#include "MainFrm.h"
#include "NewsContent.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CContainView
IMPLEMENT_DYNCREATE(CContainView, CView)
CContainView::CContainView()
{
NONCLIENTMETRICS ncm;
memset(&ncm, 0, sizeof(NONCLIENTMETRICS));
ncm.cbSize = sizeof(NONCLIENTMETRICS);
VERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &ncm, 0));
ncm.lfMessageFont.lfWeight = 700;
ncm.lfMessageFont.lfHeight = 16;
m_CaptionFont.CreateFontIndirect(&ncm.lfMessageFont);
m_CurDate = CTime::GetCurrentTime();
iCurShowItem = 0;
}
CContainView::~CContainView()
{
m_TreeIconList.DeleteImageList();
}
BEGIN_MESSAGE_MAP(CContainView, CView)
//{{AFX_MSG_MAP(CContainView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CContainView drawing
void CContainView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CContainView diagnostics
#ifdef _DEBUG
void CContainView::AssertValid() const
{
CView::AssertValid();
}
void CContainView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CContainView message handlers
BOOL CContainView::PreCreateWindow(CREATESTRUCT& cs)
{
if(!CView::PreCreateWindow(cs))
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.dwExStyle |= WS_EX_STATICEDGE;
return TRUE;
}
int CContainView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if (!m_Caption.Create(this, NULL))
{
TRACE0( "Failed to caption window.\n" );
return -1;
}
// set the caption colors.
m_Caption.SetCaptionColors(::GetSysColor(COLOR_BTNFACE),
::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_WINDOW));
// set the caption style.
m_Caption.ModifyCaptionStyle(4, &m_CaptionFont, NULL, NULL);
// The context information is passed on from the framework
CCreateContext *pContext = (CCreateContext*)lpCreateStruct->lpCreateParams;
// Create the splitter window with two columns
if (!m_wndSplitter.CreateStatic(this, 1, 2))
{
TRACE0("Failed to create splitter window\n");
return FALSE;
}
if (!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CTitleView),
CSize(0, 0), pContext))
{
m_wndSplitter.DestroyWindow();
return -1;
}
// now create the two views inside the nested splitter
if (!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CStockReferView),
CSize(0, 0), pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
// Set TitleView Width
int m_NewsListLength = AfxGetApp()->GetProfileInt(_T("Base"),_T("NewsListLength"),200);
m_wndSplitter.SetColumnInfo(0,m_NewsListLength,0);
m_wndSplitter.RecalcLayout();
return 0;
}
CStockReferView* CContainView::GetStockReferView()
{
CWnd* pWnd;
if(!m_bViewList)
pWnd = m_wndSplitter.GetPane(0,0);
else
pWnd = m_wndSplitter.GetPane(0,1);
return (CStockReferView*)pWnd;
}
void CContainView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(m_ContentDb.IsOpen()) m_ContentDb.Close();
// Save SplitterWnd states
AfxGetApp()->WriteProfileInt(_T("Base"),_T("ViewList"),m_bViewList);
if(m_bViewList)
{
int m_NewsListLength,cxMix;
m_wndSplitter.GetColumnInfo(0,m_NewsListLength,cxMix);
AfxGetApp()->WriteProfileInt(_T("Base"),_T("NewsListLength"),m_NewsListLength);
}
// HistoryNews Set
AfxGetApp()->WriteProfileInt(_T("Base"),_T("HistoryNewsSet"),m_SaveNewsChk);
AfxGetApp()->WriteProfileInt(_T("Base"),_T("SaveDays"),m_SaveDays);
}
BOOL CContainView::InitColum(LPCTSTR lpszTitle, HICON hIcon)
{
m_SaveNewsChk = AfxGetApp()->GetProfileInt(_T("Base"),_T("HistoryNewsSet"),0);
m_SaveDays = AfxGetApp()->GetProfileInt(_T("Base"),_T("SaveDays"),HISTORYNEWS_DAY);
m_bViewList = AfxGetApp()->GetProfileInt(_T("Base"),_T("ViewList"),1);
m_Caption.UpdateCaption(lpszTitle,hIcon);
if(!OpenContentDb()) return FALSE;
for(int i=0;i<CATEGORY_NUM;i++) HasInit[i] = FALSE;
SetTitleTree();
BOOL m_bRtn = TRUE;
if(!m_bViewList)
m_wndSplitter.HideColumn(0);
m_bRtn = InitTitleTree();
iCurShowItem = iCurSel;
GetTitleTree(iCurSel).ShowWindow(SW_SHOW);
return m_bRtn;
}
BOOL CContainView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
UNUSED_ALWAYS(pDC);
return FALSE;
}
void CContainView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if( m_wndSplitter.GetSafeHwnd())
{
int nCX = ::GetSystemMetrics(SM_CXEDGE);
int nCY = ::GetSystemMetrics(SM_CYEDGE);
// move and grow view to clip border
m_wndSplitter.MoveWindow(-nCX, 30, cx+(nCX*2), cy+(nCY*2)-34);
}
if( m_Caption.GetSafeHwnd())
m_Caption.MoveWindow(0,0,cx,31);
}
CTreeCtrlEx& CContainView::GetTitleTree(int iTitleTree)
{
return CTitleView::m_TitleTree[iTitleTree];
}
BOOL CContainView::OpenContentDb()
{
try
{
m_ContentDb.Open(CONTENT_DATABASE,TRUE,FALSE,_T(";PWD=新_NS_???_dat!09股"));
}
catch(CDaoException *ex)
{
if(m_ContentDb.IsOpen()) m_ContentDb.Close();
ex->Delete();
return FALSE;
}
return TRUE;
}
void CContainView::SetTitleTree()
{
CBitmap bitmap;
m_TreeIconList.Create(20, 16, ILC_COLOR24 | ILC_MASK, 2, 0);
bitmap.LoadBitmap(IDB_TITLE_TREE);
m_TreeIconList.Add(&bitmap,RGB(255,0,255));
for(int i = 0; i < CATEGORY_NUM; i++)
GetTitleTree(i).SetImageList(&m_TreeIconList, TVSIL_NORMAL);
for(i = 0; i < 10; i++)
strParents[i].LoadString(IDS_PARENT_TITLE0 + i);
CTime tmpday = m_CurDate;
weekday = m_CurDate.GetDayOfWeek();
weekday--;
if(weekday == 0) weekday = 7;
CString strFmtDate;
for(i = 2 + weekday; i > 3; i--)
{
tmpday -= CTimeSpan(1,0,0,0);
strToday = tmpday.Format(IDS_DATE_FORMAT);
strParents[i] += strToday;
}
strToday.LoadString(IDS_PARENT_TODAY);
strFmtDate.LoadString(IDS_DATE_FORMAT);
strToday = strToday + strFmtDate;
strToday = m_CurDate.Format(strToday);
strParents[3+weekday] = strToday;
}
BOOL CContainView::DeleteHistoryNews(int m_iHistoryTime)
{
CNewsContent item(&m_ContentDb);
try
{
CString sql;
sql.Format(_T("SELECT * FROM list%d ORDER BY col3"),iCurSel);
item.Open(dbOpenDynaset,sql,0);
if(!item.IsEOF())
{
CTime delday = m_CurDate - CTimeSpan(m_iHistoryTime,0,0,0);
sql.Format(_T("DELETE FROM list%d WHERE col3 < #%s#;"),iCurSel,delday.Format(_T("%Y-%m-%d")));
item.m_pDatabase->Execute(sql);
}
item.Close();
return TRUE;
}
catch(CDaoException *ex)
{
if(item.IsOpen()) item.Close();
ex->Delete();
return FALSE;
}
}
void CContainView::AddTreeItem(int iCurNewsItem, LPCTSTR lpszTitle, int pos/* = -1*/)
{
HTREEITEM m_tmpItem;
if(pos == -1) pos = 3 + weekday;
CString title = lpszTitle;
if(!HasCreate[pos])
{
curTreeItem.hParent = NULL;
curTreeItem.hInsertAfter = TVI_LAST;
curTreeItem.item.iImage = 0;
curTreeItem.item.iSelectedImage = 0;
curTreeItem.item.pszText = strParents[pos].GetBuffer(strParents[pos].GetLength());
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
m_hisItem[iCurNewsItem][pos] = GetTitleTree(iCurNewsItem).InsertItem(&curTreeItem);
HasCreate[pos] = TRUE;
}
curTreeItem.hParent = m_hisItem[iCurNewsItem][pos];
curTreeItem.hInsertAfter = TVI_LAST;
curTreeItem.item.iImage = 1;
curTreeItem.item.iSelectedImage = 1;
curTreeItem.item.pszText = title.GetBuffer(title.GetLength());
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
m_tmpItem = GetTitleTree(iCurNewsItem).InsertItem(&curTreeItem);
if(pos == 3 + weekday)
GetTitleTree(iCurNewsItem).Expand(m_hisItem[iCurNewsItem][pos],TVE_EXPAND);
}
BOOL CContainView::InitTitleTree()
{
int m_iHistoryTime;
if(m_SaveNewsChk == 0) m_iHistoryTime = m_SaveDays;
else if(m_SaveNewsChk == 1) m_iHistoryTime = 0;
else m_iHistoryTime = -1;
if(m_iHistoryTime >= 0)
{
if(!DeleteHistoryNews(m_iHistoryTime))
return FALSE;
}
CTime curTime,hisTime;
CTimeSpan ts;
int sepday,i;
for(i = 0; i < 11; i++) HasCreate[i] = FALSE;
CNewsContent item(&m_ContentDb);
try
{
CString sql;
sql.Format(_T("SELECT * FROM list%d WHERE col3 <= %s ORDER BY col3"),iCurSel,
(m_CurDate+1).Format(_T("#%m/%d/%Y#")));
item.Open(dbOpenDynaset,sql,0);
while(!item.IsEOF())
{
curTime = CTime(m_CurDate.GetYear(),m_CurDate.GetMonth(),m_CurDate.GetDay(),0,0,0);
hisTime = CTime(item.m_today.GetYear(),item.m_today.GetMonth(),item.m_today.GetDay(),0,0,0);
ts = curTime - hisTime;
sepday = ts.GetDays();
if(sepday == 0) i = weekday + 3;
else if(sepday >= weekday)
{
sepday = sepday - weekday;
i = 3 - sepday/7;
if(i<0) i=0;
}
else if(sepday < weekday)
i = weekday - sepday + 3;
AddTreeItem(iCurSel,item.m_title,i);
item.MoveNext();
}
item.Close();
i = 3 + weekday;
if(!HasCreate[i])
{
curTreeItem.hParent = NULL;
curTreeItem.hInsertAfter = TVI_LAST;
curTreeItem.item.iImage = 0;
curTreeItem.item.iSelectedImage = 0;
curTreeItem.item.pszText = strParents[i].GetBuffer(strParents[i].GetLength());
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
m_hisItem[iCurSel][i] = GetTitleTree(iCurSel).InsertItem(&curTreeItem);
HasCreate[i] = TRUE;
}
HasInit[iCurSel] = TRUE;
return TRUE;
}
catch(CDaoException *ex)
{
if(item.IsOpen()) item.Close();
ex->Delete();
return FALSE;
}
}
BOOL CContainView::OnCateogryChanged(LPCTSTR lpszTitle, HICON hIcon)
{
BOOL m_bRtn = TRUE;
m_Caption.UpdateCaption(lpszTitle,hIcon);
if(!HasInit[iCurSel]) m_bRtn = InitTitleTree();
GetTitleTree(iCurShowItem).ShowWindow(SW_HIDE);
iCurShowItem = iCurSel;
GetTitleTree(iCurSel).ShowWindow(SW_SHOW);
return m_bRtn;
}
void CContainView::ShowCaptButton()
{
m_bViewList = !m_bViewList;
if(m_bViewList)
m_wndSplitter.ShowColumn();
else
m_wndSplitter.HideColumn(0);
}
BOOL CContainView::CanExport()
{
BOOL m_bEnable;
if(m_bViewList)
{
CTitleView *pView = (CTitleView *)m_wndSplitter.GetPane(0,0);
ASSERT(pView);
m_bEnable = pView->CanExport();
}
else
m_bEnable = GetStockReferView()->CanExport();
return m_bEnable;
}
void CContainView::DeleteOption()
{
CTitleView *pView = (CTitleView*)m_wndSplitter.GetPane(0,0);
pView->DeleteOption(&m_ContentDb,strToday);
}
BOOL CContainView::CanDeleteOption()
{
if(m_bViewList)
return ((CTitleView*)m_wndSplitter.GetPane(0,0))->CanDeleteOption();
else
return FALSE;
}
BOOL CContainView::AcceptBody(int iCurItem, 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_ContentDb);
try
{
CString sql;
sql.Format(_T("SELECT * FROM list%d ORDER BY col3"),iCurItem);
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#)"),
iCurItem,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);
AddTreeItem(iCurItem,title);
item.Close();
return TRUE;
}
else
{
item.Close();
return FALSE;
}
}
catch(CDaoException *ex)
{
if(item.IsOpen()) item.Close();
ex->Delete();
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -