//绘制镜像字体
void Draw_Mirror_Text(CDC *pDst_DC,//目标设备句柄
CPoint dst_Point,//目标位置
LPCTSTR drawText,//目标字符串
COLORREF textColor, //字体颜色
bool Mirror_X,//X轴镜像
bool Mirror_Y //Y轴镜像
)
{
if(pDst_DC && pDst_DC->m_hDC) //设备句柄有效
{
//创建兼容内存设备
CDC memDC;
memDC.CreateCompatibleDC(pDst_DC);
int saveDC = memDC.SaveDC(); //保存设备状态
//获取字符串大小
CSize text_size = memDC.GetTextExtent(drawText);
CRect rect(0,0,text_size.cx,text_size.cy);
//创建兼容位图
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDst_DC,rect.Width(),rect.Height());
CBitmap *pBitmap = memDC.SelectObject(&bitmap); //选入位图
//填充白色背景色
CBrush brush(RGB(255,255,255));
memDC.FillRect(rect,&brush);
//正常模式绘制字体
int saveMode = memDC.SetMapMode(MM_TEXT);
int savebkMode = memDC.SetBkMode(TRANSPARENT);//不更改背景色
COLORREF saveTextColor = memDC.SetTextColor(textColor);//设置字体颜色
memDC.TextOut(0,0,drawText);//绘制字体
memDC.SetTextColor(saveTextColor);//恢复字体颜色
memDC.SetBkMode(savebkMode);//恢复映射模式
//主要是用SetViewportExt 和 SetViewportOrg 做坐标翻转
memDC.SetMapMode(MM_ISOTROPIC);
CSize saveWindowExt = memDC.SetWindowExt(1,1);
int vp_cx = 1,vp_cy = 1;
if(Mirror_X) vp_cx = -1;
if(Mirror_Y) vp_cy = -1;
CSize saveViewport = memDC.SetViewportExt(vp_cx,vp_cy);
int vporg_x = 0,vporg_y = 0;
if(Mirror_X)vporg_x = rect.Width()-1;
if(Mirror_Y)vporg_y = rect.Height()-1;
CPoint saveViewportOrg = memDC.SetViewportOrg(vporg_x,vporg_y);
//贴图到目标设备的目标位置
pDst_DC->BitBlt(dst_Point.x,dst_Point.y,rect.Width(),rect.Height(),&memDC,0,0,SRCAND);
//恢复内存设备信息
memDC.SetViewportOrg(saveViewportOrg);
memDC.SetViewportExt(saveViewport);
memDC.SetWindowExt(saveWindowExt);
memDC.SetMapMode(saveMode);
//释放对象 销毁内存设备
memDC.SelectObject(pBitmap);
bitmap.DeleteObject();
memDC.RestoreDC(saveDC);
memDC.DeleteDC();
}
}