我在 View 里面的OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) ,pDC->SetMapMode(MM_LOMETRIC) ;但是在View的OnDraw绘制文字时,文字是倒的,该怎么办?
18 个解决方案
MM_ANISOTROPIC Logical units are converted to arbitrary units with arbitrarily scaled axes. Setting the mapping mode to MM_ANISOTROPIC does not change the current window or viewport settings. To change the units, orientation, and scaling, call the SetWindowExt and SetViewportExt member functions.
MM_HIENGLISH Each logical unit is converted to 0.001 inch. Positive x is to the right; positive y is up.
MM_HIMETRIC Each logical unit is converted to 0.01 millimeter. Positive x is to the right; positive y is up.
MM_ISOTROPIC Logical units are converted to arbitrary units with equally scaled axes; that is, 1 unit along the x-axis is equal to 1 unit along the y-axis. Use the SetWindowExt and SetViewportExt member functions to specify the desired units and the orientation of the axes. GDI makes adjustments as necessary to ensure that the x and y units remain the same size.
MM_LOENGLISH Each logical unit is converted to 0.01 inch. Positive x is to the right; positive y is up.
MM_LOMETRIC Each logical unit is converted to 0.1 millimeter. Positive x is to the right; positive y is up.
MM_TEXT Each logical unit is converted to 1 device pixel. Positive x is to the right; positive y is down.
MM_TWIPS Each logical unit is converted to 1/20 of a point. (Because a point is 1/72 inch, a twip is 1/1440 inch.) Positive x is to the right; positive y is up.
再贴一下测试代码:
OnDraw:
using namespace Gdiplus;
Graphics graphics(pDC->GetSafeHdc());
CRect rect(600,-700,800,-900);
Matrix matrix;
const PointF p((rect.left+rect.right)/2,(rect.top+rect.bottom)/2);
matrix.RotateAt(0,p);
graphics.SetTransform(&matrix);
graphics.SetSmoothingMode(SmoothingModeHighQuality);
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.SetPixelOffsetMode(PixelOffsetModeHighQuality);
Pen pNewPen(Color::Blue,3);
// HatchBrush newBrush(HatchStyleCross,Color(255, 0, 255, 0),Color(255, 0, 0, 255));
WCHAR string2[] = L"矩形";
GraphicsPath gp ;
RectF rf(rect.left,rect.top,rect.Width(),-rect.Height());
gp.AddRectangle(rf) ;
SolidBrush solidBrush(Color(255, 0, 0, 255));
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 24, FontStyleBold, UnitPoint);
StringFormat stringFormat;
stringFormat.SetFormatFlags(StringFormatFlagsDirectionRightToLeft);
RectF rectf1(rect.left,rect.top,rect.Width(),-rect.Height()) ;
gp.AddString(string2, (INT)wcslen(string2), &fontFamily,FontStyleBold,36,rf, &stringFormat);//, &solidBrush);
graphics.DrawPath(&pNewPen,&gp) ;
OnPrepareDC:
CScrollView::OnPrepareDC(pDC, pInfo);
pDC->SetMapMode(MM_LOMETRIC) ;
WCHAR string2[] = L"矩形";
////////////////////////////
MM_TEXT
to wangk(倒之) : 我需要用的是MM_LOMETRIC,如果在OnDraw的时候临时修改映射模式为是可以的,但是这样一来,用MM_TEXT输出的字体比MM_LOMETRIC大得多,为了跟其他的图元大小相匹配,还需要把字体缩小等等操作,不是更加麻烦了吗?
Use the SetWindowExtEx() and SetViewportExtEx() functions to specify the units, orientation, and scaling requiredt.
application must call the SetWindowExtEx function before calling SetViewportExtEx
再顶,重贴一下代码大家想放这个问题过去吗?还是没有人懂?
void CXXXView::OnDraw(CDC* pDC)
{
CXXXDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
using namespace Gdiplus;
Graphics graphics(pDC->GetSafeHdc());
CRect rect(600,-700,800,-900);
const PointF p((rect.left+rect.right)/2,(rect.top+rect.bottom)/2);
GraphicsPath gp ;
RectF rf(rect.left,rect.top,rect.Width(),-rect.Height());
gp.AddRectangle(rf) ;
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 24, FontStyleBold, UnitPoint);
StringFormat stringFormat;
stringFormat.SetFormatFlags(StringFormatFlagsDirectionRightToLeft);
RectF fontRect(rect.left,rect.top,rect.Width(),-rect.Height()) ;
WCHAR str[] = L"TEST";
gp.AddString(str, (INT)wcslen(str), &fontFamily,FontStyleBold,36,fontRect, &stringFormat);//, &solidBrush);
SolidBrush solidBrush(Color(0, 0, 0, 0));
Pen pNewPen(Color::Black,3);
graphics.DrawPath(&pNewPen,&gp) ;
}
void CXXXView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::OnPrepareDC(pDC, pInfo);
pDC->SetMapMode(MM_LOMETRIC) ;
}
changing MM_TEXT temporary,then using SetWindowExtEx and SetViewportExtEx adjust.
也许可以试一下:
ScaleTransform(1, -1)
楼主说的这个问题的确是一个很严重的问题,弄了一个下午终于找了一个不算是很好的办法。
代码根据我自己的环境 调试的时候有写数据进行了更改:
Graphics graphics( pDC->GetSafeHdc( ) );
CRect rect( 100, -200, 300, -400 );
PointF p( ( rect.left + rect.right )/2,( rect.top + rect.bottom )/2 );
GraphicsPath gp;
RectF rf( rect.left, rect.top, rect.Width( ), -rect.Height( ) );
gp.AddRectangle( rf );
FontFamily fontFamily( L"Arial");
Font font( &fontFamily, 24, FontStyleBold, UnitPoint );
StringFormat stringFormat;
stringFormat.SetFormatFlags( StringFormatFlagsDirectionRightToLeft );
RectF fontRect( rect.left, rect.top, rect.Width( ), rect.Height( ) ) ;
WCHAR str[ ] = L"TEST KVAP";
gp.AddString( str, ( INT )wcslen( str ), &fontFamily, FontStyleBold, 48, fontRect, &stringFormat );
//对绘图平面实施坐标变换
graphics.TranslateTransform( 100, -200 ); //先将实施变换的点切换到绘图位置
graphics.ScaleTransform( 1, -1 ); //镜像翻转图像就是正确的效果了
SolidBrush br( Color( 255,0,0,0 ) );
// graphics.DrawString( str, 9, &font, PointF( 100, -200 ) , &br );
Pen pNewPen( Color::Black, 1 );
graphics.DrawPath( &pNewPen, &gp );
上面我的代码绘制出来的文字等都正过来了,但是位置还需要楼主根据自己的情况去调整。
楼主的文字是被镜像了,这个问题要想仔细研究可以找《精通GDI+编程》这本书,CSDN、PUDN上有下载。
祝你好运!