MFC新建位图

2 篇文章 0 订阅

 

 

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();
			}
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值