Re: Can't get static SolidBrush to initialize... what am I missing
"Stick" <Stick@discussions.microsoft.com> wrote in message
news:1CBB4674-158A-42E5-9B8E-53D046054EB8@microsoft.com
John,
"John Carson" wrote:
To call GDI+ functions, you need to first initialize GDI+ with a
call to GdiplusStartup. I'm guessing that you do that in WinMain or
in some function that is called directly or indirectly from WinMain.
Since static variables are initialized before WinMain is entered,
you are therefore using GDI+ before the call to GdiplusStartup, so
the initialization of your brushes is failing.
Assuming my diagnosis is correct, you should be able to solve the
problem as follows.
Yeah, I already know all this. As I mentioned in my post, the code
works fine until I attempt to use static composition.
I think you have missed the point. I know that your code works fine without
static variables. That is because (assuming my diagnosis is correct) the
version of the code that doesn't use static variables is called *after*
GdiplusStartup is called, whereas the attempted initialization of the static
variables occurs *before* GdiplusStartup is called.
Also, FYI, you do not need an instantiation of the class to have the
static vars available. They are accessed with the class resolution
operator.
Here you have definitely missed the point. What I was saying was that you
should do something like the following. First declare:
struct GDIPlusManager
{
GDIPlusManager()
{
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
}
~GDIPlusManager()
{
Gdiplus::GdiplusShutdown(gdiplusToken);
}
private:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
};
(you can do other stuff to make this a singleton class, but I am keeping it
simple).
Then in the .cpp file, you have:
GDIPlusManager gdipm;
// and later
SolidBrush Cdu::brush_Mfd_Blk( Color(255, 0, 0, 0));
The constructor of gdipm will be called before the constructor of
Cdu::brush_Mfd_Blk, which means that GdiplusStartup will be called before
the constructor of Cdu::brush_Mfd_Blk, so the initialization should actually
work, unlike at present.
I have tested this in a simple example and it does work.
--
John Carson