Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I draw simple text using C#

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Rectangle pRect = new Rectangle(126, 35, 182, 59);

            StringFormat objStrFormat = new StringFormat();
            objStrFormat.Alignment = StringAlignment.Center;
            objStrFormat.LineAlignment = StringAlignment.Center;        
            
            e.Graphics.DrawRectangle(Pens.Black, pRect);
            e.Graphics.FillRectangle(new SolidBrush(Color.DarkOrange), pRect);

            Font pLOGFONT = new Font("Arial", 20, FontStyle.Bold);
            
            e.Graphics.DrawString("Sample Text", pLOGFONT, new SolidBrush(Color.Black), pRect, objStrFormat);
            
        }

Same as above code using MFC

CBrush* OldBrush;
	CFont* OldFont=NULL;
	CPen *OldPen=NULL;
	CPen BClrPen;
	CBrush BgBrush;
	CFont m_TextFont;

	CClientDC dc(this);

	COLORREF rgb(RGB(0,0,0));
	BClrPen.CreatePen(PS_SOLID,1,rgb);
	OldPen=dc.SelectObject(&BClrPen);

	BgBrush.CreateSolidBrush(RGB(123,150,0));

	OldBrush=dc.SelectObject(&BgBrush);
	dc.Rectangle(126,35,308,94);	
	dc.SelectObject(OldBrush);
	dc.SelectObject(OldPen);
	BClrPen.DeleteObject();	
	
	memset(&lf,0,sizeof(LOGFONT));	
	
	lf.lfHeight = 31;
	lf.lfWidth =  20;//Need to set 14.5

        lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = 700;
	lf.lfItalic = 0;
	lf.lfUnderline = 0;
	lf.lfStrikeOut = 0;
	lf.lfCharSet = 1;
	lf.lfOutPrecision = 0;
	lf.lfClipPrecision = 0;
	lf.lfQuality = 0;
	lf.lfPitchAndFamily = 0;
	lstrcpy(lf.lfFaceName, _T("Arial"));	

	m_TextFont.CreateFontIndirect(&lf);

dc.SetBkMode(TRANSPARENT);
	COLORREF m_cBgColor(RGB(50,20,100));
	dc.SetTextColor(m_cBgColor);
	OldFont = dc.SelectObject(&m_TextFont);
	dc.DrawText(_T("Sample Text"),11,CRect(126,35,308,94),DT_SINGLELINE | DT_CENTER | DT_VCENTER);			
	dc.SelectObject(OldFont);

In the above, in both cases the font size is set to 20 but in MFC font looks bigger than C# output. Got one difference is size (width parameter)is in logical pixel in MFC and Point in C#.

So how do I convert Point into logical pixel or any other suitable way because I am using C# application in MFC?
Posted
Updated 23-Dec-10 0:20am
v2
Comments
Slacker007 23-Dec-10 6:22am    
"bcoz" is not a word. Please don't use text speak here at CodeProject. Thank you.

1 solution

 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900