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

📄 titleview.cpp

📁 本程序是VC为平台开发的股票资讯系统
💻 CPP
字号:
// TitleView.cpp : implementation file
//

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTitleView

CTreeCtrlEx CTitleView::m_TitleTree[CATEGORY_NUM];

IMPLEMENT_DYNCREATE(CTitleView, CView)

CTitleView::CTitleView()
{
	// Create the image list used by frame buttons.
	m_ImageList.Create(IDB_BITMAP_CLOSE, 16, 1, RGB(255, 0, 255));
}

CTitleView::~CTitleView()
{
	m_ImageList.DeleteImageList();
}


BEGIN_MESSAGE_MAP(CTitleView, CView)
	//{{AFX_MSG_MAP(CTitleView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
	ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_CAPT_BUTTON, OnCaptButton)
	ON_NOTIFY_RANGE(TVN_SELCHANGED, IDC_TITLE_TREE, IDC_TITLE_TREE + CATEGORY_NUM - 1, OnSelchangedTree)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTitleView drawing

void CTitleView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CTitleView diagnostics

#ifdef _DEBUG
void CTitleView::AssertValid() const
{
	CView::AssertValid();
}

void CTitleView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTitleView message handlers

int CTitleView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	if (!m_Caption.Create(this,_T("资讯列表")))
	{
		TRACE0( _T("Unable to create caption.\n"));
		return -1;
	}

	if (!m_CaptionButton.Create(NULL, WS_VISIBLE | BS_ICON | 
		BS_OWNERDRAW | BS_CENTER | BS_VCENTER, CRect(0,0,0,0), 
		this,IDC_CAPT_BUTTON))
	{
		TRACE0(_T("Unable to create caption button.\n"));
		return -1;
	}

	// set the caption buttons icon.
	m_CaptionButton.SetIcon(m_ImageList.ExtractIcon(0), CSize(16,15));
	
	m_tip.Create(this);
	m_tip.AddTool(&m_CaptionButton,IDS_BUTTON_CLOSE);
	m_tip.Activate(TRUE);

	// create the tree control.
	for(int i=0;i<CATEGORY_NUM;i++)
	{
		if (!m_TitleTree[i].Create(WS_VISIBLE, CRect(0,0,0,0), this, IDC_TITLE_TREE+i))
		{
			TRACE0( "Unable to create tree control.\n" );
			return -1;
		}
		m_TitleTree[i].ShowWindow(SW_HIDE);
	}

	return 0;
}

void CTitleView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if( m_Caption.GetSafeHwnd())
		m_Caption.MoveWindow( 0, 0, cx, 19 );

	if( m_CaptionButton.GetSafeHwnd()) 
		m_CaptionButton.MoveWindow( cx-18, 2, 16, 15 );

	for(int i=0;i<CATEGORY_NUM;i++)
	{
		if( m_TitleTree[i].GetSafeHwnd()) 
		{
			m_TitleTree[i].MoveWindow( 0, 19, cx, cy-19 );
		}
	}
}

BOOL CTitleView::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_tip.RelayEvent(pMsg);
	
	return CView::PreTranslateMessage(pMsg);
}

void CTitleView::OnCaptButton()
{
	AfxGetMainWnd()->SendMessage(WM_COMMAND,ID_VIEW_LIST);
}

CSize CTitleView::GetButtonTextSize(CFont *pFont, UINT nIDText)
{
	CString strText;
	CDC* pDC = GetDC();
	CFont* pOldFont = pDC->SelectObject(pFont);
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	strText.LoadString(nIDText);
	CSize size = pDC->GetTextExtent(strText,strText.GetLength());
	pDC->SelectObject(pOldFont);
	ReleaseDC(pDC);

	return size;
}

BOOL CTitleView::OnEraseBkgnd(CDC* pDC) 
{
	UNUSED_ALWAYS(pDC);

	return TRUE;
}

void CTitleView::OnSelchangedTree(UINT id, NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	try
	{
		int selnum,nImage,nSelectedImage;
		HTREEITEM m_tmpItem;
		CString title;
		selnum = m_TitleTree[iCurSel].GetSelectedCount();
		if(selnum == 1)
		{
			m_tmpItem = m_TitleTree[iCurSel].GetSelectedItem();
			m_TitleTree[iCurSel].GetItemImage(m_tmpItem,nImage,nSelectedImage);
			if(nImage == 1) title = m_TitleTree[iCurSel].GetItemText(m_tmpItem);
			CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
			ASSERT(pFrame);
			CStockReferView* pFileView = pFrame->GetStockReferView();
			ASSERT(pFileView);
			pFileView->OutFile(title);
		}
	}
	catch(...)
	{
	}

	*pResult = 0;
}

BOOL CTitleView::CanExport()
{
	int count = m_TitleTree[iCurSel].GetSelectedCount();
	if(count != 1) return FALSE;
	int nImg,nImgSel;
	HTREEITEM m_hSelectedItem = m_TitleTree[iCurSel].GetSelectedItem();
	if(m_hSelectedItem == NULL) return FALSE;
	if(!m_TitleTree[iCurSel].GetItemImage(m_hSelectedItem,nImg,nImgSel))
		return FALSE;
	if(nImg == 0)
		return FALSE;

	return TRUE;
}

BOOL CTitleView::CanDeleteOption()
{
	int count = m_TitleTree[iCurSel].GetSelectedCount();
	if (count == 0) return FALSE;
	if(count == 1)
	{
		int nImg,nImgSel;
		HTREEITEM m_hSelectedItem = m_TitleTree[iCurSel].GetSelectedItem();
		if(m_hSelectedItem == NULL) return FALSE;
		if(!m_TitleTree[iCurSel].GetItemImage(m_hSelectedItem,nImg,nImgSel))
			return FALSE;
		if(nImg == 0 &&
			!m_TitleTree[iCurSel].ItemHasChildren(m_hSelectedItem))
			return FALSE;

		return TRUE;
	}
	else
		return TRUE;
}

void CTitleView::ShowPopupMenu(CPoint &point, int nImage)
{
	if (point.x == -1 && point.y == -1)
	{
		//keystroke invocation
		CRect rect;
		GetClientRect(rect);
		ClientToScreen(rect);

		point = rect.TopLeft();
		point.Offset(5, 5);
	}

	CCJMenu menu;
	VERIFY(menu.LoadMenu(IDR_POPUP_TITLETREE));

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

	pPopup->LoadToolbar(IDR_MAINFRAME);

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

	CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
	ASSERT(pFrame);
	if(pFrame->m_IsThreadRunning)
	{
		pPopup->EnableMenuItem(ID_EDIT_DELETE,MF_BYCOMMAND|MF_GRAYED);
		pPopup->EnableMenuItem(ID_FILE_EXPORT,MF_BYCOMMAND|MF_GRAYED);
	}
	int count = m_TitleTree[iCurSel].GetSelectedCount();
	if(count > 1 || nImage == 0)
	{
		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);
	}
	if(!CanDeleteOption())
		pPopup->EnableMenuItem(ID_EDIT_DELETE,MF_BYCOMMAND|MF_GRAYED);

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

	menu.DestroyMenu();
}

void CTitleView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	UINT uFlags;
	CPoint ptTree = point;
	m_TitleTree[iCurSel].ScreenToClient(&ptTree);
	HTREEITEM htItem = m_TitleTree[iCurSel].HitTest(ptTree, &uFlags);

	int nImage,nSelectedImage;
	m_TitleTree[iCurSel].GetItemImage(htItem,nImage,nSelectedImage);
	if((htItem != NULL) && (uFlags & TVHT_ONITEM)) 
	{
		ShowPopupMenu(point,nImage);
		m_TitleTree[iCurSel].SetItemState(htItem, 0, TVIS_DROPHILITED);
	}
	else
		CView::OnContextMenu(pWnd, point);

	if(m_pOldSel != NULL)
	{
		m_TitleTree[iCurSel].Select(m_pOldSel, TVGN_DROPHILITE);
		m_pOldSel = NULL;
	}
}

void CTitleView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	UINT uFlags;
	HTREEITEM htItem = m_TitleTree[iCurSel].HitTest(point, &uFlags);
	if ((htItem != NULL) && (uFlags & TVHT_ONITEM)) 
	{
		m_pOldSel = m_TitleTree[iCurSel].GetSelectedItem();
		m_TitleTree[iCurSel].Select(htItem, TVGN_DROPHILITE);
	}
}

void CTitleView::DeleteOption(CDaoDatabase *m_pData, LPCTSTR lpszToday) 
{
	// TODO: Add your command handler code here
	int count,i;
	CString itemtxt,sql,todel;
	HTREEITEM childitem,*tmpItem;
	
	if(AfxMessageBox(IDS_DELETE_SURE,MB_YESNO | 
		MB_ICONINFORMATION | MB_DEFBUTTON2) == IDYES)
	{
		count = m_TitleTree[iCurSel].GetSelectedCount();
		tmpItem = new HTREEITEM[count];
		tmpItem[0] = m_TitleTree[iCurSel].GetFirstSelectedItem();
		for(i=1;i<count;i++)
			tmpItem[i] = m_TitleTree[iCurSel].GetNextSelectedItem(tmpItem[i-1]);
		for(i=0;i<count;i++)
		{
			while(m_TitleTree[iCurSel].ItemHasChildren(tmpItem[i])) 
			{
				childitem = m_TitleTree[iCurSel].GetChildItem(tmpItem[i]);
				itemtxt = m_TitleTree[iCurSel].GetItemText(childitem);
				todel += _T("'") + itemtxt + _T("' OR col1 = ");
				m_TitleTree[iCurSel].DeleteItem(childitem); 
			}
			itemtxt = m_TitleTree[iCurSel].GetItemText(tmpItem[i]);
			if(itemtxt != lpszToday)
			{
				todel += _T("'") + itemtxt + _T("' OR col1 = ");
				m_TitleTree[iCurSel].DeleteItem(tmpItem[i]); 
			}
		}
		delete tmpItem;
	}
	
	if(!todel.IsEmpty())
	{
		CNewsContent item(m_pData);
		try
		{
			int len = todel.GetLength();
			todel = todel.Left(len - 11);
			sql.Format(_T("SELECT * FROM list%d ORDER BY col3"),iCurSel);
			item.Open(dbOpenDynaset,sql,0);
			sql.Format(_T("DELETE FROM list%d WHERE col1 = %s;"),iCurSel,todel);
			item.m_pDatabase->Execute(sql);
			item.Close();
		}
		catch(CDaoException *e)
		{
			if(item.IsOpen()) item.Close();
			e->Delete();
			
			AfxMessageBox(IDS_PROGRAM_ERROR,MB_ICONSTOP);
		}
	}
	m_TitleTree[iCurSel].SetFocus();
}

void CTitleView::OnEditDelete() 
{
	// TODO: Add your command handler code here
	CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
	ASSERT(pFrame);
	CContainView *pView = (CContainView*)pFrame->GetContainView();
	ASSERT(pView);
	pView->DeleteOption();
}

void CTitleView::OnFileExport() 
{
	// TODO: Add your command handler code here
	CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
	ASSERT(pFrame);
	CStockReferView *pView = (CStockReferView*)pFrame->GetStockReferView();
	ASSERT(pView);
	pView->ExportToFile();
}

void CTitleView::OnFilePrintPreview() 
{
	// TODO: Add your command handler code here
	CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
	ASSERT(pFrame);
	CStockReferView *pView = (CStockReferView*)pFrame->GetStockReferView();
	ASSERT(pView);
	pView->FilePrintPreview();
}

void CTitleView::OnFilePrint() 
{
	// TODO: Add your command handler code here
	CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
	ASSERT(pFrame);
	CStockReferView *pView = (CStockReferView*)pFrame->GetStockReferView();
	ASSERT(pView);
	pView->FilePrint();
}

⌨️ 快捷键说明

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