vicview.cpp

来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· C++ 代码 · 共 306 行

CPP
306
字号
// VicView.cpp : implementation of the CVicView class
//

#include "stdafx.h"
#include "DBExp.h"

#include "VicDoc.h"
#include "VicView.h"
#include "aliasview.h"
#include "mainfrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVicView

IMPLEMENT_DYNCREATE(CVicView, CListView)

BEGIN_MESSAGE_MAP(CVicView, CListView)
	//{{AFX_MSG_MAP(CVicView)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_NOTIFY_REFLECT(NM_RETURN, OnReturn)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVicView construction/destruction

//##ModelId=3C37413C006F
CVicView::CVicView()
{
	// TODO: add construction code here

}

//##ModelId=3C37413C021E
CVicView::~CVicView()
{
}

//##ModelId=3C37413C019B
BOOL CVicView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CVicView drawing

//##ModelId=3C37413C00FB
void CVicView::OnDraw(CDC* pDC)
{
	CVicDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CListCtrl& refCtrl = GetListCtrl();
	refCtrl.InsertItem(0, "Item!");
	// TODO: add draw code for native data here
}

//##ModelId=3C37413C01A6
void CVicView::OnInitialUpdate()
{
	InitListView();

	CListView::OnInitialUpdate();

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	// Gain a reference to the list control itself
	CListCtrl& theCtrl = GetListCtrl();

	// this code only works for a report-mode list view
	ASSERT(GetStyle() & LVS_REPORT);
	
	// Insert a column. This override is the most convenient.
	theCtrl.InsertColumn(0, _T("对象名称"), LVCFMT_LEFT);
	
	// Set reasonable widths for our columns
	theCtrl.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
}

/////////////////////////////////////////////////////////////////////////////
// CVicView printing

//##ModelId=3C37413C01B9
BOOL CVicView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

//##ModelId=3C37413C01CF
void CVicView::OnBeginPrinting(CDC *pDC /*pDC*/, CPrintInfo *pInfo /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

//##ModelId=3C37413C0200
void CVicView::OnEndPrinting(CDC *pDC /*pDC*/, CPrintInfo *pInfo /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CVicView diagnostics

#ifdef _DEBUG
//##ModelId=3C37413C0228
void CVicView::AssertValid() const
{
	CListView::AssertValid();
}

//##ModelId=3C37413C023D
void CVicView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

//##ModelId=3C37413C00F1
CVicDoc* CVicView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVicDoc)));
	return (CVicDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CVicView message handlers
//##ModelId=3C37413C0264
void CVicView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
{
	//TODO: add code to react to the user changing the view style of your window
}

//##ModelId=3C5CC9C6028F
void CVicView::InitImageList()
{
		//请在下面的这个数组中加入新的图标的ID
	const int ImgIDCount = 4;
	const int IconSize[3][2] = {{32, 32},
								{16, 16},
								{16, 16}
								};
	static ImageIDBuf[3][ImgIDCount] = {
		{IDI_LV_PACKAGE,
			IDI_LV_TABLE,
			IDI_LV_VIEW,
			IDI_LV_SP
		},//Normal
		{IDI_LV_SM_PACKAGE,
			IDI_LV_SM_TABLE,
			IDI_LV_SM_VIEW,
			IDI_LV_SM_SP
		},//Small
		{IDI_LV_SM_PACKAGE,
			IDI_LV_SM_TABLE,
			IDI_LV_SM_VIEW,
			IDI_LV_SM_SP
		}//State
	};


	HICON hIcon;

	int i = 0, j = 0;

	for(j = 0; j < 3; j++)
	{
		m_ImageList[j].Create(IconSize[j][0],
			IconSize[j][1],
			TRUE,   // list does include masks
			_count_of(ImageIDBuf),
			0 );   // list will not grow
		
		for(i = 0; i < _count_of(ImageIDBuf[j]); i++)
		{
			hIcon = ::LoadIcon (AfxGetResourceHandle(), 
				MAKEINTRESOURCE(ImageIDBuf[j][i]));
			ASSERT(NULL != hIcon);
			
			VERIFY( -1 != m_ImageList[j].Add(hIcon));
			::DeleteObject(hIcon);
			
		}
	}
}

//##ModelId=3C5CC9C60285
void CVicView::InitListView()
{
	//修改TreeView中的树的风格。
	InitImageList();

	//设置TreeView中的TreeCtrl相关连的ImageList
	GetListCtrl().SetImageList(&m_ImageList[0], LVSIL_NORMAL);
	GetListCtrl().SetImageList(&m_ImageList[1], LVSIL_SMALL);
	GetListCtrl().SetImageList(&m_ImageList[2], LVSIL_STATE);

	ModifyStyle(LVS_TYPEMASK, LVS_REPORT | LVS_AUTOARRANGE);

}

//##ModelId=3C6215DC0105
void CVicView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CListCtrl *pList = (CListCtrl *)CListCtrl::FromHandle(pNMHDR->hwndFrom);

	ASSERT(pList);
	pList->AssertValid();

 	//在选择的那个Item上弹出菜单

	//get current cursor's screen pos
	CPoint point;
	::GetCursorPos(&point);
	//convert the screen pos to tree control relative pos
	pList->ScreenToClient(&point);

 	UINT uFlags;
	//get item over which the cursor stop...
 	int nIndex = pList->HitTest(point, &uFlags);
 
 
	if(-1 == nIndex)
	{
		//光标停在了空白区,这样,可以弹出"在右视中没有弹出的
		//子菜单提示“大图标,小图标,列表,详细情况”
		::GetCursorPos(&point);

		CMenu menu;
		menu.LoadMenu(IDR_MENU_RIGHT_VIEW);
		menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,
			point.x, point.y,AfxGetMainWnd());
		return;
	}

 	DWORD dwData;
 	if ((-1 != nIndex) && (LVHT_ONITEM & uFlags))
 	{
 		dwData = pList->GetItemData(nIndex);		
 	}
 	if(0 == dwData)
 		return;
 
	/*************************/
	//call popup menu...........
	/**************************/

 	CDBTreeNode *pDBTreeNode = NULL;
 	pDBTreeNode = (CDBTreeNode *)dwData;
 
 	ASSERT(pDBTreeNode);
 	pDBTreeNode->AssertValid();
 	
 	DBTREE_NODE_CONTEXT context;
 	//DBTREE_NODE_CONTEXT中要放入的有
 	//m_pos //当前的位置
 	//m_NodeType//当前的数据结点的类型。
 	//m_pParentWnd//当前的父窗口
 	
 	GetCursorPos(&context.m_pos);
 	context.m_pParentWnd = this;
 
 	pDBTreeNode->PopupContextMenu(context);	
	*pResult = 0;	
}

//##ModelId=3C6215DC0187
void CVicView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CAliasView *pTreeView = ((CMainFrame *)AfxGetMainWnd())->GetLeftPane();
	pTreeView->GetTreeCtrl().Expand(pTreeView->GetTreeCtrl().GetSelectedItem()
		,TVE_EXPAND
		);
	CListCtrl *pListCtrl = &this->GetListCtrl();
	NM_LISTVIEW *pNM_ListView = (NM_LISTVIEW *)pNMHDR;
	CDBTreeNode *pTreeNode = (CDBTreeNode *)pListCtrl->GetItemData(pNM_ListView->iItem);
	ASSERT(pTreeNode);
	pTreeView->GetTreeCtrl().Expand(pTreeNode->Get_hItem(), TVE_EXPAND);
	pTreeView->GetTreeCtrl().Select(pTreeNode->Get_hItem(), TVGN_CARET);
	*pResult = 0;
}

//##ModelId=3C6215DC01EC
void CVicView::OnReturn(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	OnDblclk(pNMHDR, pResult);
	*pResult = 0;
}

⌨️ 快捷键说明

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