August 8, 200619 yr I designed a gauge using GDI+ to display text. The font size is calculated by the height of the rectangle.PELEMENT_STATIC_IMAGE pelement = (PELEMENT_STATIC_IMAGE)(pgauge->elements_list[0]->next_element[0]);PIXPOINT dim = pelement->image_data.final->dim;Font myFont(L"Lucida Console", 0.23*(float)dim.y);graphics.DrawString(string1,-1,&myFont,origin1,&format,&textBrush);This works fine in 2D panel, but in the VC the text seems to be stretched. The height of a character is ok, but it's too wide, so the text does not fit into the rectangle.Is there a possibility to reduce the width according to the width of the rectangle without reducing the font height?
August 8, 200619 yr you need to call ScaleTransform Method of the graphics object prior the call to the DrawString Method call..Example:PELEMENT_STATIC_IMAGE pelement = (PELEMENT_STATIC_IMAGE)(pgauge->elements_list<0>->next_element<0>);PIXPOINT dim = pelement->image_data.final->dim;Font myFont(L"Lucida Console", 1);graphics.ScaleTransform(dim.x*dblWidth,dim.y*dblHeight);graphics.DrawString(string1,-1,&myFont,origin1,&format,&textBrush);You can play i little bit with dblWidth and dblHeight to find the desired values needed .. or you can use the following routine to draw a text in a pelement.VOID DrawString( PELEMENT_STATIC_IMAGE pelement, LPSTR szText, FLOAT x, FLOAT y, LPSTR szFont, FLOAT flScaleX, FLOAT flScaleY, Gdiplus::Color color, int FontStyle){ CA2W wszFontFamily(szFont); CA2W wszString(szText); Gdiplus::Font font(wszFontFamily,1,FontStyle); Gdiplus::PointF origin(0,0); Gdiplus::SolidBrush brush(color); Gdiplus::Graphics graphics(pelement->hdc); /*REMOVE THE NEXT LINE TO DISABLE ANTIALIAS*/ graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAlias); graphics.TranslateTransform(x,y); graphics.ScaleTransform(flScaleX,flScaleY); graphics.DrawString(wszString,-1,&font,origin,&brush); graphics.ResetTransform();}How to call DrawString..VOID DrawTRInfo(PELEMENT_STATIC_IMAGE pelement){ /* size is relative to the size of the pelement so if the pelement is stretched the text is stretch too */ FLOAT pos_x=pelement->image_data.final->dim.x * 0.000; FLOAT pos_y=pelement->image_data.final->dim.y * 0.800; FLOAT scale_x=pelement->image_data.final->dim.x * 0.025; FLOAT scale_y=pelement->image_data.final->dim.y * 0.025; DrawString(pelement, "TERR", pos_x, pos_y, "Arial", scale_x, scale_y, Gdiplus::Color(0x52,0xCE,0xFF), Gdiplus::FontStyleBold ); };I Hope this would help
Create an account or sign in to comment