• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX GDI : draw a simple string

Messages
51
Country
france
Hello.

Thanks to n4gix, I have a working template for GDI.

I created a new subgauge and want to display a simple string on a rectangular bitmap.

For some reason, I can't get to display this string and I can't find the error... :(

Is this a problem if PELEMENT_STATIC_IMAGE, Graphics... have the same name in different sub-gauges ?

Here's the whole code :

Code:
[COLOR="DarkGreen"]char test_gauge_name[]		= GAUGE_NAME;
extern PELEMENT_HEADER		test_list;
extern MOUSERECT			test_mouse_rect[];
GAUGE_CALLBACK Update;

GAUGE_HEADER_FS700( GAUGE_W, test_gauge_name, &test_list, \
						test_mouse_rect, 0, 0, 0, 0 );

/////////////////////////////////////////////////////////////////////////////
MAKE_STATIC
(
	test_image,
	BMP_ECRAN,
	NULL,
	NULL,
	IMAGE_USE_ERASE|IMAGE_USE_BRIGHT|IMAGE_CREATE_DIBSECTION,
	0,
	0,0
)

PELEMENT_HEADER     test_next[] = { &test_image.header, NULL };

MAKE_STATIC
(
	test_background,
	BMP_ECRAN,
	test_next,
	0,
	IMAGE_USE_TRANSPARENCY,
	0,
	0, 0
)

PELEMENT_HEADER		test_list	= &test_background.header;

/////////////////////////////////////////////////////////////////////////////
 
MOUSE_BEGIN( test_mouse_rect, HELP_NONE, 0, 0 )
MOUSE_END

//////////////////////AFFICHAGE GDI///////////////////////////////////////////
void FSAPI Update ( PGAUGEHDR pgauge, SINT32 service_id, UINT32 extra_data )
{

	switch (service_id)
	{
		case PANEL_SERVICE_CONNECT_TO_WINDOW:
			gdiPlusUsers++;
			if(!init_gdiPlus)
			{
				GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
				init_gdiPlus = true;
			}

		break;

		case PANEL_SERVICE_PRE_INITIALIZE:

		break;

		case PANEL_SERVICE_PRE_INSTALL:
		break;

		case PANEL_SERVICE_PRE_DRAW:
		{	PELEMENT_STATIC_IMAGE pelement = (PELEMENT_STATIC_IMAGE)(pgauge->elements_list[0]->next_element[0]);

			if (pelement)
			{
			        HDC hdc = pelement->hdc;
				PIXPOINT dim = pelement->image_data.final->dim;


                if (hdc)
	        {
						Graphics graphics(hdc);
						dx = dim.x / 448;
						dy = dim.y / 141;

						graphics.ScaleTransform(dx,dy); //scale/resize ONCE only!

						set_render_quality(&graphics);

						SolidBrush whiteBrush(Color(255, (255*brt)/100, (255*brt)/100, (255*brt)/100));
						//NOTE: you could also simply apply brt to the Alpha instead of each color!
						//NOTE: for multi-gauge creation, it would be better to declare the Brushes, Pens,
						//and Fonts used in the master .cpp file, then create them as need in each gauge.
					
						Color Black(0,0,0);
						graphics.Clear(Black);

						FontFamily	Quartz(L"Quartz");
						Font LargeNumbers(&Quartz, 33, FontStyleBoldItalic, UnitPixel);					

						PointF pointPower(1, 1);
						WCHAR string[] = L"Hello World";
						swprintf(string, L"");
						graphics.DrawString(string, -1, &LargeNumbers, pointPower, &whiteBrush);

					} //end gauge_draw else clause
				}
				SET_OFF_SCREEN (pelement);  //This is the command that sends the final drawing to the gauge display
			}
		break;

		case	PANEL_SERVICE_PRE_KILL:
		break;

		case    PANEL_SERVICE_DISCONNECT:
			gdiPlusUsers--;
			if (init_gdiPlus && gdiPlusUsers == 0)
				{
				GdiplusShutdown(gdiplusToken);
				init_gdiPlus = false;
				}
		break;
	}
}

/////////////////////////////////////////////////////////////////////////////
#undef GAUGE_NAME
#undef GAUGEHDR_VAR_NAME
#undef GAUGE_W[/COLOR]

I'm the kind of guy that spend 2 days searching for a missing coma so help is welcome ! ^^

Thanks
 
DrawString uses rectangles not points. Im guessing that with your single point it is more than likely trying to draw that entire string on a single pixel.

Here is the method i use. The only difference aside from the rect is that the swprintf() function has been replaced by StringCbPrintf() and i added a extra variable which deals with the size.

Code:
[COLOR="Green"]
WCHAR string[MAX_PATH];
size_t string_size(MAX_PATH * sizeof(WCHAR));

StringCbPrintf(string, string_size, L"BIT");
rect = RectF(72, 13, 80, 30);
graphics.DrawString( string, lstrlen(string), font, rect, &stringformat, brush);
[/COLOR]
 
Last edited:
Thank you for your help ;)

DrawString uses rectangles not points. Im guessing that with your single point it is more than likely trying to draw that entire string on a single pixel.

Are you sure of that ? The GDI+ documentation would then be erroneous ?
There are several DrawString methods, some take RectF, some take PointF :

Graphics::DrawString(WCHAR*,INT,Font*,PointF&,StringFormat*,Brush*)

http://msdn.microsoft.com/en-us/library/ms535993%28v=vs.85%29
 
Well in that case, I think it requires an array of points rather than a single point ;) . FYI you can create rectangles with point arrays as well. However the point array must make up some sort of box shape for the text to fit in.

EDIT: Looking at things again, may i recommend making the PointF values look like this:

PointF pointPower(1.0f, 1.0f);

However overall rectangles are a bit more reliable.
 
Last edited:
I think it just takes the point as the top-left corner of the text box.

However, I tried to use a RectF and it doesn't display my text either. :(

Code:
FontFamily	Quartz(L"Quartz");
Font LargeNumbers(&Quartz, 33, FontStyleBoldItalic, UnitPixel);					

RectF rect = RectF(0.0f, 0.0f, 100.0f, 100.0f);
WCHAR string[] = L"Hello World";
swprintf(string, L"");
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
graphics.DrawString(string, -1, &LargeNumbers, rect, &format, &whiteBrush);

This code should display a "Hello World" in a 100x100 pixels rectangle at the top left corner of my bmp right ?
 
Last edited:
First, using a single point to define where GDI starts rendering a string of characters is just fine. It doesn't require a rectangle, nor does it require an array of points. I have never found a real need to use a rectangle when using GDI+ to render a string.

Second... this code:

Code:
WCHAR string[] = L"Hello World";
swprintf(string, L"");
graphics.DrawString(string, -1, &LargeNumbers, pointPower, &whiteBrush);

...will indeed not draw what you're expecting.
First you declared a string variable and preset it to "Hello World".
Then you set the contents of your string variable to "".
Then you told the graphics object to render your string variable, which is empty.
 
Blimey im blind....Guess i have been using the ESP template for too long as well....yes the WCHAR string variable is only the buffer. Not the actual string. If you just have a fixed bit of text you want to work with you can do it this way:

Code:
swprintf(string, L"Hello World");

If you have a float/double you want to display you can do this
Code:
float number = 123.456;
swprintf(string, L"%3.3f", number);

Integers:

Code:
int number = 20;
swprintf(string, L"%1d", number); //Should return a "20"
//or
swprintf(string, L"%01d", number); //Should return a "020". Same thing can be 
//done with floats by adding that lead 0 to the number formating

Or variable strings:
http://www.fsdeveloper.com/forum/showthread.php?t=251128
 
Last edited:
This:
Code:
swprintf(string, L"%01d", number); //Should return a "020". Same thing can be 
//done with floats by adding that lead 0 to the number formating
will return '20'... not '020'. You told it you want it to format an integer value with a minimum length of 1 character filled in with zeros from the left. To get '020' it would need to be:

Code:
swprintf(string, L"%03d", number); //Should return a "020". Same thing can be 
//done with floats by adding that lead 0 to the number formating
 
Code:
FontFamily	Quartz(L"Quartz");
Font LargeNumbers(&Quartz, 33, FontStyleBoldItalic, UnitPixel);					

RectF rect = RectF(0.0f, 0.0f, 100.0f, 100.0f);
WCHAR string[] = L"Hello World";
swprintf(string, L"[B]Hello World[/B]");
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
graphics.DrawString(string, -1, &LargeNumbers, rect, &format, &whiteBrush);
 
Try this instead of swprintf and try initializing the buffer like this:

WCHAR string[MAX_PATH];
size_t string_size(MAX_PATH * sizeof(WCHAR));

StringCbPrintf(string, string_size, L"BIT");
 
Did it, no change :(

Code:
FontFamily	Quartz(L"Quartz");
Font LargeNumbers(&Quartz, 33, FontStyleBoldItalic, UnitPixel);					

WCHAR string[MAX_PATH];
size_t string_size(MAX_PATH * sizeof(WCHAR));
 
RectF rect = RectF(0.0f, 0.0f, 100.0f, 100.0f);
StringCbPrintf(string, string_size, L"BIT");
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
graphics.DrawString(string, -1, &LargeNumbers, rect, &format, &whiteBrush);
 
try changing the second parameter in the DrawString function from -1 to lstrlen(string) ?
 
Hmmm...if I were you, I would try first with :

GAUGE_HEADER_FS700( GAUGE_W, test_gauge_name, &test_list, \
test_mouse_rect, Update, 0, 0, 0 );


Tom
 
:facepalm: Without telling the gauge what callback function to use it would never do anything....
 
AAAAaaaaaah !!! :eek:
Copy paste too quickly...

I told you I was good at missing things that can't be missed... :rolleyes:

Sorry for this and thank you for the help. :D
 
Since i spend most of my time in the actual callback function rather than the rest of the template (and since i use the ESP example as a template which is VERY different) and my template already has all these things done for me i tend to miss them....
 
Back
Top