www.pudn.com > NetPaw.rar > NetPawView.cpp
// NetPawView.cpp : CNetPawView 类的实现
//
#include "stdafx.h"
#include "NetPaw.h"
#include "NetPawDoc.h"
#include "mainfrm.h"
#include "memdc.h"
#include "downloadfile.h"
#include "fileview.h"
#include ".\netpawview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CNetPawView
IMPLEMENT_DYNCREATE(CNetPawView, CScrollView)
BEGIN_MESSAGE_MAP(CNetPawView, CScrollView)
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CNetPawView 构造/析构
CNetPawView::CNetPawView()
: m_nTotalCells(0)
, m_nLineCells(0)
, m_nCellWidth(10)
, m_nCellHeight(0)
{
// TODO: 在此处添加构造代码
m_clrBackgrnd = GetSysColor(COLOR_BTNFACE); // gray
SetScrollSizes(MM_TEXT, CSize(0, 0));
}
CNetPawView::~CNetPawView()
{
}
BOOL CNetPawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return CScrollView::PreCreateWindow(cs);
}
void CNetPawView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// 将位图加入图像列表中,为画图做准备
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BMPMETER);
m_ilMeters.Create(10, 11, ILC_MASK|ILC_COLOR32, 3, 1);
m_ilMeters.Add(&bitmap, RGB(255, 0, 255));
// 获取位图中单个图标的尺寸
IMAGEINFO imgInfo;
ZeroMemory(&imgInfo, sizeof(IMAGEINFO));
m_ilMeters.GetImageInfo(0, &imgInfo);
CRect rect = imgInfo.rcImage;
m_nCellWidth = rect.Width();
m_nCellHeight = rect.Height();
}
// CNetPawView 诊断
#ifdef _DEBUG
void CNetPawView::AssertValid() const
{
CScrollView::AssertValid();
}
void CNetPawView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CNetPawDoc* CNetPawView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNetPawDoc)));
return (CNetPawDoc*)m_pDocument;
}
#endif //_DEBUG
// CNetPawView 消息处理程序
void CNetPawView::DrawMeter(CDC* pDC)
{
CRect rect;
pDC->GetClipBox(&rect);
// fill back ground color, should at first (brush is logical?)
CBrush brush;
brush.CreateSolidBrush(m_clrBackgrnd);
pDC->FillRect(&rect, &brush);
brush.DeleteObject();
// download file length is unknown, just return
if( GetDocument()->GetSelFileLen() <= 0 )
{
return;
}
// change the coordinates to device viewpoint
pDC->LPtoDP(&rect);
// compute drawing parameters
ComputeCells(rect);
// draw Back cell and done cell
if( m_nLineCells > 0 )
{
DrawBackCells(pDC, rect);
DrawCell(pDC, rect);
}
}
void CNetPawView::ComputeCells(CRect& rect)
{
// 计算绘图图幅的长度,由下载文件长度和显示图标的大小确定
m_nLineCells = rect.Width() / m_nCellWidth;
if( m_nLineCells > 0 )
{
m_nTotalCells = (int)( (GetDocument()->GetSelFileLen() + 1023) / 1024 );
int nHeight = (m_nTotalCells + m_nLineCells - 1) / m_nLineCells * m_nCellHeight;
SetScrollSizes(MM_TEXT, CSize(rect.Width(), nHeight));
}
}
BOOL CNetPawView::OnEraseBkgnd(CDC* pDC)
{
// should do nothing here
return TRUE; //CScrollView::OnEraseBkgnd(pDC);
}
void CNetPawView::DrawBackCells(CDC* pDC, CRect& rectDC)
{
CPoint point;
// draw whole/partial cells
point.x = rectDC.left;
point.y = rectDC.top;
for(int nCell = 0; nCell < m_nTotalCells; nCell++)
{
if( point.x + m_nCellWidth > rectDC.right )
{
point.x = 0;
point.y += m_nCellHeight;
}
m_ilMeters.Draw(pDC, 0, point, ILD_NORMAL);
point.x += m_nCellWidth;
}
}
void CNetPawView::DrawCell(CDC* pDC, CRect& rectDC)
{
CNetPawDoc *pDoc = GetDocument();
CDownloadFile *pDlFile = pDoc->GetSelDownFile();
if( pDlFile )
{
pDlFile->DrawDownldFile(pDC, rectDC, this);
}
}
void CNetPawView::OnDraw(CDC* pDC)
{
// draw cells in memory dc
CMemDC mDC(pDC);
switch( GetCurrentClassID() )
{
case FOLDER_DOWNLDED:
DrawDone(&mDC);
break;
case FOLDER_FAVORITES:
DrawFavorites(&mDC);
break;
case FOLDER_DOWNLDING:
default:
DrawMeter(&mDC);
break;
}
}
void CNetPawView::DrawOneSegment(CDC* pDC, CRect& rectDC, int nFromCell, int nToCell, int nDoneCells)
{
CPoint point;
// draw whole/partial cells
point.x = rectDC.left + (nFromCell % m_nLineCells) * m_nCellWidth;
point.y = rectDC.top + (nFromCell / m_nLineCells) * m_nCellHeight;
for(int nCell = 0; nCell < nDoneCells; nCell++)
{
if( point.x + m_nCellWidth > rectDC.right )
{
point.x = 0;
point.y += m_nCellHeight;
}
// the last one use other color
if( ( nCell < (nDoneCells - 1) ) || ( nCell >= (nToCell - nFromCell - 1) ) )
{
m_ilMeters.Draw(pDC, 1, point, ILD_NORMAL);
}
else
{
m_ilMeters.Draw(pDC, 2, point, ILD_NORMAL);
}
point.x += m_nCellWidth;
}
}
void CNetPawView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
// TODO: 在此添加专用代码和/或调用基类
Invalidate();
}
int CNetPawView::GetCurrentClassID(void)
{
int nID = FOLDER_DOWNLDING;
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CFileView *pView = pFrame->GetRightPane();
if( pView )
{
nID = pView->m_nShowClassID;
}
return nID;
}
void CNetPawView::DrawDone(CDC* pDC)
{
RECT rect;
pDC->GetClipBox(&rect);
// fill back ground color, should at first (brush is logical?)
CBrush brush;
brush.CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
pDC->FillRect(&rect, &brush);
brush.DeleteObject();
pDC->SetBkMode(TRANSPARENT);
DONEITEM_S *pDoneIt = GetDocument()->GetSelDownloaded();
if( pDoneIt )
{
pDC->SetTextColor(RGB(0, 0, 255));
pDC->TextOut(rect.left + 5, rect.top + 10, pDoneIt->szWebInfo, (int)_tcslen(pDoneIt->szWebInfo));
}
}
void CNetPawView::DrawFavorites(CDC* pDC)
{
RECT rect;
pDC->GetClipBox(&rect);
// fill back ground color, should at first (brush is logical?)
CBrush brush;
brush.CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
pDC->FillRect(&rect, &brush);
brush.DeleteObject();
}