• 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.

GDI+ DrawString() related problem

Messages
135
Country
us-kentucky
WARNING: Advanced C++ (take excedrin now) :D

*Deep breath*

Ok... I'm creating a class which will represent a line of text on a display. The goal is, of course, encapsulation and a desire to be able to use it like this:

Code:
  pTitleLine = new DisplayLine(rect1, pageTitle);	// Create title
  pTitleLine->AlignCenter();				// Center text

  pTitleLine->Draw(pGraphics);

The DisplayLine class has a constructor that looks like this:

Code:
DisplayLine ::DisplayLine (RectF rect, string str) 
    : strBrush(Color((int)(brightness * 2.55), 0, 255, 0))
{
	lineRect	= rect;   // RectF for DisplayString()
	lineText	= str;     // string to display

	pStrFormat = new StringFormat();
}

You'll note the brush has to be initialized like the above due to being a const.

Now, with the above, I can do this:

Code:
void DisplayLine ::Draw(Graphics *pGraphics)
{
  Font		font_big	(L"FIXEDSYS", 18, FontStyleBold, UnitPixel);
  Font		font_small (L"FIXEDSYS", 12, FontStyleBold, UnitPixel);
  //StringFormat	sFormat = new StringFormat();
  SolidBrush	gbrush(Color((int)(brightness * 2.55), 0, 255, 0));

  //sFormat.SetAlignment(StringAlignmentCenter);
  //sFormat.SetLineAlignment(StringAlignmentFar);

  wchar_t buf[20+1] = { 0 };
  MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lineText.c_str(), (int)lineText.length(), buf, 20);
  pGraphics->DrawString(buf, -1, &font_big, lineRect, pStrFormat, &gbrush);
}

And this will compile fine. However, it crashes in flight sim due to:

Code:
  pTitleLine->AlignCenter();	// Crashes FS

  // Coded like this:
  void DisplayLine::AlignCenter()
  {
    pStrFormat->SetAlignment(StringAlignmentCenter);
  }

Now, if I uncomment and use sFormat above (vs. pStrFormat), it works fine. Problem is, then I couldn't allow it to be adjusted. I'm baffled why this doesn't work. Any ideas fellow gurus?
 
Messages
135
Country
us-kentucky
I figured out one way around the problem, and finally also realized what I was "missing".

What I really want to do is set up a static StringFormat. I had forgotten that I could define these outside the constructor, so I'm going to try that. If not, I'm just going to construct one in the draw() function with enums from member vars.

It is a pain trying to switch back and forth between C, C++, and C#, haha. :eek: Scrambles the brain. :D
 

scruffyduck

Administrator
Staff member
FSDevConf team
Resource contributor
Messages
33,838
Country
unitedkingdom
It is a pain trying to switch back and forth between C, C++, and C#, haha. :eek: Scrambles the brain. :D

Bit like banging your head against a brick wall - it is so noce when you stop :)
 
Messages
135
Country
us-kentucky
It's interesting....

Although I've found another way, I have found that if I setup a static variable like this:
Code:
// .h file
class CduLine
{
	static		SolidBrush		s_solidBrush;
	...
};
and initialize it like this:
Code:
// .cpp file
	SolidBrush DisplayLine::s_solidBrush(Color(255, 0, 255, 0));
and then in the draw() routine do this:

Code:
void DisplayLine::Draw(Graphics *pGraphics)
{
	s_solidBrush.SetColor(Color(255, 0, 255, 0));

	// Draw the text
	pGraphics->DrawString(buf, -1, &font_big, lineRect, &sFormat, &s_solidBrush);
}

that the text will not draw due to the brush being black. Weird.

Anyone know what is going on here?
 
Top