//构造函数中:加载图片
CString strImagePath = CfgInst.GetAppPath() + IMAGE_PATH;
_bstr_t bstr(strImagePath);
wchar_t *wstr = (wchar_t*)bstr;
m_pDeviceBoardImage = new Image(wstr);
void CDeviceBoard::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect(&rect); //图像显示区域 CRect rectImage(rect); m_lOffsetX = rect.left + (int)(rect.Width() * DEVICE_BOARD_PERCENT); m_lOffsetY = rect.top; rectImage.left = m_lOffsetX; rectImage.top = m_lOffsetY; rectImage.right = rectImage.left + (*m_pDeviceBoardImage).GetWidth(); rectImage.bottom = rectImage.top + (*m_pDeviceBoardImage).GetHeight(); //内存dc CDC dcMemDraw; dcMemDraw.CreateCompatibleDC(&dc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); CBitmap *pOldBit = dcMemDraw.SelectObject(&bitmap); //填充背景色为白色 CBrush brush; if (!brush.CreateSolidBrush(WHITE)) { return; } brush.UnrealizeObject(); dcMemDraw.FillRect(rect, &brush); brush.DeleteObject(); //画垂直分割线 CPen pen; if (!pen.CreatePen(PS_SOLID, LINE_WIDTH, LINECOR)) { return; } CPen *pPenOld = dcMemDraw.SelectObject(&pen); dcMemDraw.MoveTo((int)((float)rect.Width() * g_fLinePos), rect.top); dcMemDraw.LineTo((int)((float)rect.Width() * g_fLinePos), rect.bottom); Graphics myGraphics(dcMemDraw); myGraphics.DrawImage(m_pDeviceBoardImage, (int)rectImage.left, (int)rectImage.top, (int)(*m_pDeviceBoardImage).GetWidth(), (int)(*m_pDeviceBoardImage).GetHeight()); dcMemDraw.SelectObject(pPenOld); //绘制面板左上角文字 CFont font; VERIFY(font.CreateFont(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "宋体")); CFont* def_font = dcMemDraw.SelectObject(&font); dcMemDraw.SetBkMode(TRANSPARENT); dcMemDraw.TextOut(20, 20, "设备面板状态图"); dcMemDraw.SelectObject(def_font); //将内存dc中的图像拷贝到屏幕上 dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcMemDraw, 0, 0, rect.Width(), rect.Height(), SRCCOPY); //绘图完成后的清理 dcMemDraw.SelectObject(pOldBit); pen.DeleteObject(); bitmap.DeleteObject(); dcMemDraw.DeleteDC(); return; }