哎 代码改的优雅多了...
少了硬编码 代码确实好看了很多...
上个例子... 滚动视图部分的
修改前的代码
void
CTransportWnd::OnPaint()
{
CPaintDC dc( this );
m_tooltip.RemoveAllTools(); // 清空所有标记
CRect rect;
GetClientRect(rect);
DocToClient(rect);
HDC dcMem;
HBITMAP bm;
bm = CreateCompatibleBitmap(dc.m_hDC, rect.Width(), rect.Height());
dcMem = CreateCompatibleDC(dc);
SelectObject(dcMem, bm);
//////////////////////// //
Graphics graphics(dcMem);
graphics.SetSmoothingMode(SmoothingModeHighQuality);
// background
graphics.FillRectangle( & SolidBrush(Color( 255 , 255 , 255 , 255 )), rect.left, rect.top, rect.Width(), rect.Height());
// concrete flow
DrawAllPlaces(graphics, rect);
BitBlt(dc, 0 , 0 , rect.Width(), rect.Height(), dcMem, 0 , 0 , SRCCOPY);
DeleteObject(bm);
DeleteDC(dcMem);
// 更改滚动条
GetClientRect( & rect);
int iCount = m_vecPlaces.size();
int iTotalHeight = (iCount / 2 ) * (FLOW_HEIGHT + 10 );
SetScrollSizes( MM_LOENGLISH, CSize(rect.Width() - 20 , iTotalHeight) );
}
{
CPaintDC dc( this );
m_tooltip.RemoveAllTools(); // 清空所有标记
CRect rect;
GetClientRect(rect);
DocToClient(rect);
HDC dcMem;
HBITMAP bm;
bm = CreateCompatibleBitmap(dc.m_hDC, rect.Width(), rect.Height());
dcMem = CreateCompatibleDC(dc);
SelectObject(dcMem, bm);
//////////////////////// //
Graphics graphics(dcMem);
graphics.SetSmoothingMode(SmoothingModeHighQuality);
// background
graphics.FillRectangle( & SolidBrush(Color( 255 , 255 , 255 , 255 )), rect.left, rect.top, rect.Width(), rect.Height());
// concrete flow
DrawAllPlaces(graphics, rect);
BitBlt(dc, 0 , 0 , rect.Width(), rect.Height(), dcMem, 0 , 0 , SRCCOPY);
DeleteObject(bm);
DeleteDC(dcMem);
// 更改滚动条
GetClientRect( & rect);
int iCount = m_vecPlaces.size();
int iTotalHeight = (iCount / 2 ) * (FLOW_HEIGHT + 10 );
SetScrollSizes( MM_LOENGLISH, CSize(rect.Width() - 20 , iTotalHeight) );
}
修改过后的代码
void
CWorksiteWnd::OnDraw(CDC
*
pDC)
{
// TODO: 在此处为本机数据添加绘制代码
m_tooTip.RemoveAllTools(); // 清空所有标记
CRect rect;
GetClientRect(rect);
// 绘图
Graphics graphics( pDC -> m_hDC );
DrawAllPlaces(graphics, rect);
// 更新滚动条
GetClientRect( & rect);
int iCount = m_vecPlaces.size();
int iTotalHeight = iCount * (FLOW_HEIGHT + 10 ) + 20 ;
SetScrollSizes( MM_TEXT, CSize(rect.Width() - 20 , iTotalHeight) );
}
单单抛开双缓冲不说 那个DocToClient这个函数简直是丑陋无比 而且是设备相关的(换了分辨率就变样了...)
{
// TODO: 在此处为本机数据添加绘制代码
m_tooTip.RemoveAllTools(); // 清空所有标记
CRect rect;
GetClientRect(rect);
// 绘图
Graphics graphics( pDC -> m_hDC );
DrawAllPlaces(graphics, rect);
// 更新滚动条
GetClientRect( & rect);
int iCount = m_vecPlaces.size();
int iTotalHeight = iCount * (FLOW_HEIGHT + 10 ) + 20 ;
SetScrollSizes( MM_TEXT, CSize(rect.Width() - 20 , iTotalHeight) );
}
void CTransportWnd::DocToClient(CRect & rect)
{
CClientDC dc( this );
OnPrepareDC( & dc, NULL);
dc.LPtoDP(rect);
rect.NormalizeRect();
// rect.bottom += 5000;
// rect.top -= 198;
}
void CTransportWnd::DrawAllPlaces(Graphics & graphics, CRect rect)
{
// rect.top += 188;
CRect rectTemp;
GetClientRect( & rectTemp);
rect.top += rectTemp.bottom;
// rect.top = 0;
vector < PLACE_STATION *> ::iterator it = m_vecPlaces.begin();
CPoint pointScorll
=
GetScrollPosition();
rectTemp.bottom += pointScorll.y;
rectTemp.top += pointScorll.y;
哎 今天花了一早上更改代码 现在的代码看着要舒服多了 还是听感谢侯捷的<深入浅出MFC> 昨晚无意间翻到的CScorllView这一节 一下就看明白代码的设备相关性的问题出在哪了... 看来还是要时不时翻翻书
rectTemp.bottom += pointScorll.y;
rectTemp.top += pointScorll.y;