[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Create the GDI+ font based on the given character height/width

Posted on 2009-02-12
Medium Priority
Last Modified: 2013-12-03
Hello there.

I have a font I want to use it for drawing, and I want to center a character of the font in a specific RECT I have. How do I create the font so when I draw the text, the character fits exactly (or almost) in the drawn area ? This is score drawing so the notes must be placed at exact positions.

This is the function I 've been using so far, but it either creates a very small font, or it doesn't center. The function has to work with any font character.

Best Regards.

RECT OutputNoteText(Gdiplus::Graphics* g,const wchar_t* x,int s,RECT& r,int th,Gdiplus::Brush* bx)
	RECT A = {0};
	// Create the note font
	if (th == 0)
		th = r.bottom - r.top;
	Gdiplus::FontFamily fam(_T("Some family"));
	Gdiplus::Font* fo = new Gdiplus::Font(&fam,th,FontStyleRegular,UnitPixel);
	Gdiplus::RectF r1(r.left,r.top,r.right - r.left,r.bottom - r.top);
	Gdiplus::RectF r2;
	Gdiplus::StringFormat sf;
	Gdiplus::SolidBrush br(Color(0,0,0));
	g->DrawString(x,s,fo,r2,&sf,bx ? bx : &br);
	A.left = r2.X;
	A.top = r2.Y;
	A.right = r2.X + r2.Width;
	A.bottom = r2.Y + r2.Height;
	delete fo;
	return A;

Open in new window

Question by:WxW
  • 2

Accepted Solution

Blomholm earned 375 total points
ID: 23622250
I've done this in c#, where I drew a number in the center of a rectangle.
Hope this helps

// C#
// here sectorWidth and sectorHeight would be your rectangles height and width
SizeF size = g.MeasureString(number.ToString(), font);
g.DrawString(number.ToString(), font, brush, new PointF(x * sectorWidth + sectorWidth / 2 - size.Width / 2, y * sectorHeight + sectorHeight / 2 - size.Height / 2));

Open in new window


Expert Comment

ID: 23622289
One note to the above, x and y here was row and column number in a grid. You should replace x* sectorWidth and y * sectorHeight with your X and Y position for the rectangle.
Something like this:
g.DrawString(number.ToString(), font, brush, new PointF( your_X_position + sectorWidth / 2 - size.Width / 2, your_Y_position + sectorHeight / 2 - size.Height / 2));

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question