How to draw text on screen C#
-
This is something that's poorly documented and yet pretty basic stuff.
Credit goes out to @M8T for providing the SHVDN2 code. Given DN2 is no longer supported, I've tweaked it for DN3.
This is the function which does the magic:
public TextElement(string caption, PointF position, float scale, Color color, Font font, Alignment alignment, bool shadow, bool outline, float wrapWidth);
Notes: The scale parameter is the font size. Any thing over 0.5 will be fairly large.
The font options themselves are very limited, going to research if window fonts are available.
Shadow refers to a text shadow, outline to text outline. Wrap width would be useful if you want one sentence for example to be wrapped rather than go all they way across the screen, just like the word wrap in notepad.
To add a new line (line break/carriage return in C# you use \n\n staying inside the quotation marks of your text.private void DrawText()
{
var pos = new Point(200, 200);
var TextString = "Lesson Topics Today: \n\n Part 1. Drawing Text \n\n Part 2. Customizations";
var Text4Screen = new GTA.UI.TextElement(TextString, pos, 0.5f, Color.White, GTA.UI.Font.ChaletComprimeCologne, GTA.UI.Alignment.Left, true, true, 1000); //last parameter is wrap width
Text4Screen.Enabled = true;
Text4Screen.Draw();
}This is the native equivalent, not as user friendly, slightly modified from a post by @ikt
/* Function.Call(Hash.SET_TEXT_FONT, 0);
Function.Call(Hash.SET_TEXT_SCALE, 0.0, 0.5f);
Function.Call(Hash.SET_TEXT_COLOUR, 0, 25, 70, 255); // r/g/b/alpha
Function.Call(Hash.SET_TEXT_CENTRE, false);
Function.Call(Hash.SET_TEXT_OUTLINE, true);
Function.Call(Hash.BEGIN_TEXT_COMMAND_DISPLAY_TEXT, "CELL_EMAIL_BCON"); //Used to be known as _SET_TEXT_ENTRY
Function.Call(Hash.ADD_TEXT_COMPONENT_SUBSTRING_PLAYER_NAME, "This is where the text goes on the screen");
Function.Call(Hash.END_TEXT_COMMAND_DISPLAY_TEXT, 0.3, 0.2); //Used to be known as _DRAW_TEXT
*/
-
These are the only fonts I could find for drawing text on the screen. Does anyone know if it's possible to expand this list without playing with the source code?
namespace GTA.UI
{
//
// Summary:
// An enumeration of fonts the game supports.
public enum Font
{
ChaletLondon = 0,
HouseScript = 1,
Monospace = 2,
ChaletComprimeCologne = 4,
Pricedown = 7
}
}
-
If you're using the built-in text drawing, those are the only fonts you can use. If you want different fonts, you'll have to hook DirectX and draw the text manually.
-
@Jitnaught hi. Don't follow what you mean by drawing the text manually. Do you mean text on a transparent sprite?
Actually since posting this i figured out how to display the text on screen from a text file, line by line, either at a certain position on the screen or feeding it to the help or notification functions. Ofc that doesn't help with the fonts. I was really curious as to how the game embeds the fonts, where they are located. I know from a Google search that it is possible to change the font for the big message but the approach used was confusing and poorly documented.
-
Drawing text via DirectX hook. Example: https://github.com/niemand-sec/DirectX11Hook/tree/master/DirectX11Hook
-
@Jitnaught Thanks for the links and for the suggestions. This template seems like a great addition to scripthook. Unfortunately, as a non-programmer, this is way above my pay grade. I was hoping just to extend the available fonts, but definitely a nice to have, not need to.
I did try to explore it even though it was for the VS 2017 platform. I wasn't able to open it in VS 2019, generated quite a few lines of errors including these warnings:
warning : The build tools for v141 cannot be found. Install v141 to build using the v141 build tools.
warning : Platform 'Win32' referenced in the project file 'DirectX11Hook' cannot be found.
warning : Platform 'x64' referenced in the project file 'DirectX11Hook' cannot be found.
I could downgrade to 2017 or try to install the v141 build tools - not even sure they would work with 2019, but again this is too complicated and only tutorial links i found were broken.