绘制视图背景
1.导入ico资源
2.改写代码:
void CDocView::OnDraw(CDC* pDC)
{
CDocDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap(IDB_BITMAP1);
CDC memDC; //设备上下文
memDC.CreateCompatibleDC(pDC); //创建内存设备上下文
memDC.SelectObject(&m_Bitmap); //将位图选入设备上下文
BITMAP m_Bmp;
m_Bitmap.GetBitmap(&m_Bmp); //获得位图信息
int x = m_Bmp.bmWidth;
int y = m_Bmp.bmHeight;
CRect rect; //声明区域对象
GetClientRect(rect); //获得编辑框客户区域
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,x,y,SRCCOPY); //绘制位图背景
memDC.DeleteDC(); //释放内存设备上下文
}
======
分割文档区域
public:
CSplitterWnd m_wndSplitter; //声明对象
virtual ~CMainFrame();
--
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
m_wndSplitter.CreateStatic(this, 2, 2);
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
m_wndSplitter.CreateView(1, 1, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
return TRUE;
//return CFrameWnd::OnCreateClient(lpcs, pContext);
}
=========
在子窗口中嵌入控件:
在CMainFrame中:
public:
CSplitterWnd m_wndSplitter;
建立函数:
protected:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
-------
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
m_wndSplitter.CreateStatic(this,1,2);
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CViewCtrlView),CSize(200,100),pContext);
m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CClientView),CSize(200,100),pContext);
return TRUE;
//return CFrameWnd::OnCreateClient(lpcs, pContext);
}
引入头文件:MainFrm.cpp中
#i nclude "stdafx.h"
#i nclude "ViewCtrl.h"
#i nclude "ClientView.h"
#i nclude "ViewCtrlView.h"
#i nclude "MainFrm.h"
建立:CClientView view 类
并添加虚拟函数:OnInitialUpdate()
void CClientView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CRect rect;
GetClientRect(rect);
m_List.Create(LVS_REPORT|LVS_SINGLESEL|LVS_SHOWSELALWAYS|WS_BORDER,
rect,this,10001); //创建列表视图控件
//设置列表视图的扩展风格
m_List.SetExtendedStyle(LVS_EX_FLATSB //扁平风格显示滚动条
|LVS_EX_FULLROWSELECT //允许整行选中
|LVS_EX_HEADERDRAGDROP //允许整列拖动
|LVS_EX_ONECLICKACTIVATE //单击选中项
|LVS_EX_GRIDLINES); //画出网格线
m_List.ShowWindow(SW_SHOW); //显示控件
//设置表头
m_List.InsertColumn(0,"姓名",LVCFMT_LEFT,100,0); //设置姓名列
m_List.InsertColumn(1,"所属朝代",LVCFMT_LEFT,100,1); //设置所属国家列
m_List.InsertItem(0,""); //插入第0行
m_List.SetItemText(0,0,"关羽"); //向第0列插入数据
m_List.SetItemText(0,1,"三国"); //向第1列插入数据
m_List.InsertItem(1,""); //插入第1行
m_List.SetItemText(1,0,"秦琼"); //向第0列插入数据
m_List.SetItemText(1,1,"隋唐"); //向第1列插入数据
m_List.InsertItem(2,""); //插入第2行
m_List.SetItemText(2,0,"陈近南"); //向第0列插入数据
m_List.SetItemText(2,1,"清朝"); //向第1列插入数据
}
========
视图窗口高级应用
1.建立单文档,中第6步将基类改为CListView
2.声明变量:
public:
CDragViewDoc* GetDocument();
int m_ItmIndex;
CImageList* m_pDrgImg;
BOOL m_Drag;
3.
void CDragViewView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl* pListCtl=&GetListCtrl();
pListCtl->ModifyStyle(0L,LVS_REPORT);
pListCtl->SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT
|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE
|LVS_EX_GRIDLINES);
pListCtl->InsertColumn(0,"员工编号",LVCFMT_LEFT,100);
pListCtl->InsertColumn(1,"员工姓名",LVCFMT_LEFT,100);
pListCtl->InsertColumn(2,"文化程度",LVCFMT_LEFT,100);
pListCtl->InsertItem(0,"0001");
pListCtl->SetItemText(0,1,"赵一");
pListCtl->SetItemText(0,2,"专科");
pListCtl->InsertItem(1,"0002");
pListCtl->SetItemText(1,1,"钱二");
pListCtl->SetItemText(1,2,"本科");
pListCtl->InsertItem(2,"0003");
pListCtl->SetItemText(2,1,"孙三");
pListCtl->SetItemText(2,2,"研究生");
----加入windows handle: OnBegindra
void CDragViewView::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
POINT pt;
m_ItmIndex = ((NM_LISTVIEW *)pNMHDR)->iItem;
int pos = GetListCtrl().GetSelectionMark();
m_pDrgImg = GetListCtrl().CreateDragImage(m_ItmIndex,&pt);
m_pDrgImg->BeginDrag(0,pt);
m_pDrgImg->DragEnter(this,pt);
m_Drag = TRUE;
*pResult = 0;
}
----加入 onMouseMove
void CDragViewView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_Drag)
{
CPoint pt;
pt.x = point.x;
pt.y = point.y + (m_ItmIndex + 1) * 15;
m_pDrgImg->DragMove(pt);
}
CListView::OnMouseMove(nFlags, point);
}
------鼠标左键
void CDragViewView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_Drag)
{
m_pDrgImg->EndDrag();
m_Drag = FALSE;
char name[256];
LV_ITEM lvi;
CString subitem[3];
for(int i=2;i>=0;i--)
{
ZeroMemory(&lvi,sizeof(LV_ITEM));
lvi.iItem = m_ItmIndex;
lvi.iSubItem = i;
lvi.mask = LVIF_IMAGE | LVIF_TEXT;
lvi.pszText = name;
lvi.cchTextMax = 255;
GetListCtrl().GetItem(&lvi);
subitem[i].Format("%s",name);
}
GetListCtrl().InsertItem(&lvi);
GetListCtrl().SetItemText(m_ItmIndex,1,subitem[1]);
GetListCtrl().SetItemText(m_ItmIndex,2,subitem[2]);
}
CListView::OnLButtonUp(nFlags, point);
}
=======
视图中浏览网页
1.建立单文档 将CHtmlView设为基类