我是靠谱客的博主 纯真砖头,最近开发中收集的这篇文章主要介绍MFC新建位图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

 

if (NULL == m_pTraceMemDC)
{
m_pTraceMemDC = new CDC;
if (NULL == m_pTraceMemDC)
{
return;
}
m_pTraceMemDC->CreateCompatibleDC(NULL);
}
//这时还不能绘图,因为没有地方画
//下面建立一个与屏幕显示兼容的位图,至于位图的大小嘛,可以用窗口的大小
if (NULL == m_pTraceMemBmp)
{
m_pTraceMemBmp = new CBitmap;
if (NULL == m_pTraceMemBmp)
{
return;
}
BOOL bCreateBitmap = m_pTraceMemBmp->CreateCompatibleBitmap(pDC, rc.right - rc.left, rc.bottom - rc.top);
if (FALSE == bCreateBitmap)
{
/*CCommonLog::GetCommonLogPtr()->WriteLog(COMMON_LOG_INFO, "func=[%s],line=[%d],位图=[%d][%d].", __FUNCTION__, __LINE__, rc.right - rc.left, rc.bottom - rc.top);
AfxMessageBox(_T("创建位图失败,请放大横向比例尺!"));
return;*/
BITMAPINFOHEADER bmih;
memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biPlanes = 1;
bmih.biWidth = rc.right - rc.left;
bmih.biHeight = rc.bottom - rc.top;
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader = bmih;
void* p;
HBITMAP hBitmap = ::CreateDIBSection(pDC->m_hDC, &bmi, DIB_RGB_COLORS, &p, NULL, 0);
if (NULL== hBitmap)
{
CCommonLog::GetCommonLogPtr()->WriteLog(COMMON_LOG_INFO, "func=[%s],line=[%d],位图=[%d][%d].", __FUNCTION__, __LINE__, rc.right - rc.left, rc.bottom - rc.top);
AfxMessageBox(_T("创建位图失败,请放大横向比例尺!"));
return;
}
m_pTraceMemBmp->Attach(hBitmap);
}
//将位图选入到内存显示设备中
//只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
m_pTraceMemDC->SelectObject(m_pTraceMemBmp);
//先用背景色将位图清除干净,这里我用的是白色作为背景
//你也可以用自己应该用的颜色
m_pTraceMemDC->FillSolidRect(0, 0, rc.right - rc.left, rc.bottom - rc.top, CWorkSpace::GetWorkSpacePtr()->GetViewTraceBackgroundColor());
//m_pTraceMemDC->FillSolidRect(0, 0, rc.right - rc.left, rc.bottom - rc.top, RGB(255, 255, 255));
POINT leftPoint;
leftPoint.x = rc.left;
leftPoint.y = rc.top + 40;
POINT rightPoint;
rightPoint.x = m_nPixel+20;
rightPoint.y = rc.top + 40;
m_pTraceMemDC->MoveTo(leftPoint);
m_pTraceMemDC->LineTo(rightPoint);
if (0 < CWorkSpace::GetWorkSpacePtr()->GetMarkScale())
{
//绘制刻度
int iMarkScale = rc.left;
while (iMarkScale < rc.right)
{
POINT topPoint;
//最上面的点
topPoint.x = iMarkScale;
topPoint.y = rc.top + 40 - 5;
POINT bottomPoint;
//最下面的点
bottomPoint.x = iMarkScale;
bottomPoint.y = rc.top + 40 + 5;
m_pTraceMemDC->MoveTo(topPoint);
m_pTraceMemDC->LineTo(bottomPoint);
CFont fontMesh;
fontMesh.CreateFont(13, 4, 0, 0, 150, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
VARIABLE_PITCH | FF_SWISS, _T("微软雅黑"));
CFont *pOldF = m_pTraceMemDC->SelectObject(&fontMesh);
COLORREF oldC = m_pTraceMemDC->SetTextColor(RGB(210, 105, 30));
CString strMarkValue("");
int iMarkValue = iMarkScale * CWorkSpace::GetWorkSpacePtr()->GetDistancePixelScale();
strMarkValue.Format(_T("%d"), iMarkValue);
m_pTraceMemDC->SetBkMode(TRANSPARENT);
m_pTraceMemDC->TextOut(topPoint.x + 2, topPoint.y - 6, strMarkValue);//绘制刻度值
m_pTraceMemDC->SelectObject(pOldF);
m_pTraceMemDC->SetTextColor(oldC);
iMarkScale = iMarkScale + CWorkSpace::GetWorkSpacePtr()->GetMarkScale() / CWorkSpace::GetWorkSpacePtr()->GetDistancePixelScale();
fontMesh.DeleteObject();
}
}
}

 

最后

以上就是纯真砖头为你收集整理的MFC新建位图的全部内容,希望文章能够帮你解决MFC新建位图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部