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

Posted on 2009-02-12
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 -;

	Gdiplus::FontFamily fam(_T("Some family"));

	Gdiplus::Font* fo = new Gdiplus::Font(&fam,th,FontStyleRegular,UnitPixel);


	Gdiplus::RectF r1(r.left,,r.right - r.left,r.bottom -;

	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; = 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
    LVL 3

    Accepted Solution

    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

    LVL 3

    Expert Comment

    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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now