Solved Center Text in Managed Directx

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

HexMurder

Arcane Hacker
Dank Tier VIP
Dank Tier Donator
Jun 7, 2012
319
7,988
22
Really didn't think i would get stumped by such a simple problem... I am just not understanding what could be wrong here lol.
Simply trying to center text over my esp box.

here the code i'm using. (changed it about 40 times but here's what i have currently lol)
C++:
            Rectangle tempRect = new Rectangle();
            tempRect.X = box[i].x + (int)box[i].width/2;
            tempRect.Y = box[i].y;
            tempRect.Width = (int)box[i].width;

            DrawTextFormat flags = DrawTextFormat.NoClip | DrawTextFormat.Center;

            fontSmall.DrawText(null, text, tempRect, flags, color);

 
Last edited:

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,073
78,998
2,371
Center text horizontally:
C++:
//This function returns a float value, this return value is the x coordinate where you want to start drawing your text
//It takes into consideration the possibility that your text may be wider than the esp box and makes it look dank regardless
float Font::centerText(float x, float width, float textWidth)
{
	if (width > textWidth)
	{
		float difference = width - textWidth;
		return (x + (difference / 2));
	}

	else
	{
		float difference = textWidth - width;
		return (x - (difference / 2));
	}
}
The arguments are:
x = top left vertex of ESP box
width = width of the ESP box
textWidth = width in pixels of the string you want to print, so you do fontWidth * strlen(player.name) to get this value

So you want to do something like this:
C++:
float textX = Font::centerText(espBox.x, espBox.width, fontWidth * strlen(player.name));
fontSmall.DrawText(null, player.name, textX, flags, color);
This function centers text on X and Y axis:
C++:
vec3_t Font::centerText(float x, float y, float width, float height, float textWidth, float textHeight)
{
	vec3_t text;
	text.x = x + (width - textWidth) / 2;
	text.y = y + textHeight;
	return text;
}
 

Lovelace

Jr.Coder
Full Member
Nobleman
Oct 23, 2015
83
938
5
Shouldn't it be like this instead?

C++:
tempRect.X = box[i].x;
Since the width of your tempRect is dependent on the box and your text format is horizontally centered.
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods