少了硬編碼 代碼確實好看了很多...
上個例子... 滾動視圖部分的
修改前的代碼
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) );
}
修改過后的代碼
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這個函數簡直是丑陋無比 而且是設備相關的(換了分辨率就變樣了...)
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();
簡直就是war3里面的憎惡... 用肉縫起來的代碼 后面還有大量的
CPoint pointScorll = GetScrollPosition();
rectTemp.bottom += pointScorll.y;
rectTemp.top += pointScorll.y;
哎 今天花了一早上更改代碼 現在的代碼看著要舒服多了 還是聽感謝侯捷的<深入淺出MFC> 昨晚無意間翻到的CScorllView這一節 一下就看明白代碼的設備相關性的問題出在哪了... 看來還是要時不時翻翻書
posted on 2009-11-09 14:42
李佳 閱讀(476)
評論(0) 編輯 收藏 引用 所屬分類:
WIN32 應用開發