Discussion:
DrawString with a background colour
(too old to reply)
Dok
2006-02-16 01:10:27 UTC
Permalink
I am converting some code from GDI to GDI+ as GDI+ supports alpha.

However I have ran into an issue with DrawSting in GDI+.

In my old code wth GDI I used 'SetBkColor' if I wanted the text to have a
background colour applied to it.

Is there a way to do something like this in GDI+ with DrawSting?

I first though of just doing a clear in the rect that the text is to go in,
but after quick testng I realised this would not work. Because the GDI
Drawtext only draws that background where the text goes ie. if you use
DT_BOTTOM. Where the clear would work on the whole rect which I am not after.

Thanks in advance,
Adam
David Ching
2006-02-16 02:00:13 UTC
Permalink
Post by Dok
I am converting some code from GDI to GDI+ as GDI+ supports alpha.
However I have ran into an issue with DrawSting in GDI+.
In my old code wth GDI I used 'SetBkColor' if I wanted the text to have a
background colour applied to it.
Is there a way to do something like this in GDI+ with DrawSting?
The Graphics object is associated with a DC:

Graphics graphics(hDC);

So what happens if you still set the bkmode and bkcolor of the assoicated
DC, before creating the graphics object?

SetBkMode (hDC, OPAQUE);
SetBkColor (hDC, RGB(255,0,0)); // red
Graphics graphics(hDC);

Now that the graphics object is set up, call graphics.DrawString(). Does
that work?

-- David
Dok
2006-02-16 02:23:27 UTC
Permalink
Post by David Ching
So what happens if you still set the bkmode and bkcolor of the assoicated
DC, before creating the graphics object?
SetBkMode (hDC, OPAQUE);
SetBkColor (hDC, RGB(255,0,0)); // red
Graphics graphics(hDC);
Now that the graphics object is set up, call graphics.DrawString(). Does
that work?
I gave it a go, both before the GraphicsObject is created and after and
DrawString appears if it just ignore the call to SetBKMode. Even though most
of GDI+ just wraps the GDI I have a feeling the GDI+ text functions are all
new going by some of the other features they have. They seem to have just
forgotten about setting the background colour for text.

I think I have discovered a way around this issue and I am just implimenting
it now. I found as a MeasureString function in GDI+ that will give you the
rect back of where the string will go. I can then do a clear with the
background colour at that rect and then call DrawText. A bit convulted and
probably alot slower than a call to GDI as it has to work out the string
coordiates twice, but it should work.

Adam
Post by David Ching
Post by Dok
I am converting some code from GDI to GDI+ as GDI+ supports alpha.
However I have ran into an issue with DrawSting in GDI+.
In my old code wth GDI I used 'SetBkColor' if I wanted the text to have a
background colour applied to it.
Is there a way to do something like this in GDI+ with DrawSting?
Graphics graphics(hDC);
So what happens if you still set the bkmode and bkcolor of the assoicated
DC, before creating the graphics object?
SetBkMode (hDC, OPAQUE);
SetBkColor (hDC, RGB(255,0,0)); // red
Graphics graphics(hDC);
Now that the graphics object is set up, call graphics.DrawString(). Does
that work?
-- David
Kellie Fitton
2006-02-16 03:28:19 UTC
Permalink
Hi,

You can try someThing like this:

PointF origin(0, 0);
RectF boundRect;

String MyString = "My Sample Text";
Font drawFont = new Font("Arial", 21);
SolidBrush drawBrush = new SolidBrush(Color.Black);

graphics.FillRectangle(&drawBrush, boundRect);

graphics.MeasureString(MyString, wcslen(MyString),
&drawFont, origin, &boundRect);

graphics.DrawString(MyString, wcslen(MyString), &drawFont,
origin, &format, &drawBrush);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsFillRectangleMethods/FillRectangle.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsMeasureStringMethods/MeasureString_88string_length_font_origin_boundingBox.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsDrawStringMethods/DrawString.asp

Hope these information helps,

Kellie.
Dok
2006-02-16 03:35:28 UTC
Permalink
Thanks, thats the way I ended up doing it.

Adam
Post by Kellie Fitton
Hi,
PointF origin(0, 0);
RectF boundRect;
String MyString = "My Sample Text";
Font drawFont = new Font("Arial", 21);
SolidBrush drawBrush = new SolidBrush(Color.Black);
graphics.FillRectangle(&drawBrush, boundRect);
graphics.MeasureString(MyString, wcslen(MyString),
&drawFont, origin, &boundRect);
graphics.DrawString(MyString, wcslen(MyString), &drawFont,
origin, &format, &drawBrush);
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsFillRectangleMethods/FillRectangle.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsMeasureStringMethods/MeasureString_88string_length_font_origin_boundingBox.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/GraphicsClass/GraphicsMethods/GraphicsDrawStringMethods/DrawString.asp
Hope these information helps,
Kellie.
Vince Morgan
2006-02-16 02:00:19 UTC
Permalink
Post by Dok
I am converting some code from GDI to GDI+ as GDI+ supports alpha.
However I have ran into an issue with DrawSting in GDI+.
In my old code wth GDI I used 'SetBkColor' if I wanted the text to have a
background colour applied to it.
Is there a way to do something like this in GDI+ with DrawSting?
I first though of just doing a clear in the rect that the text is to go in,
but after quick testng I realised this would not work. Because the GDI
Drawtext only draws that background where the text goes ie. if you use
DT_BOTTOM. Where the clear would work on the whole rect which I am not after.
Thanks in advance,
I haven't used GDI+, nor read the docs, so I don't know it. However, in GDI
you would call 'SetBkMode(hDC, mode)' to set the text background to either
OPAQUE or TRANSPARENT.
AFAIK you can still use this in conjunction with GDI+.
--
HTH
Vince Morgan
Remove UNSPAM
***@UNSPAMoptusnet.com.au Adam
Dok
2006-02-16 02:37:28 UTC
Permalink
Post by Vince Morgan
I haven't used GDI+, nor read the docs, so I don't know it. However, in GDI
you would call 'SetBkMode(hDC, mode)' to set the text background to either
OPAQUE or TRANSPARENT.
AFAIK you can still use this in conjunction with GDI+.
SetBkMode does not appear to affect GDI+ DrawString it appears to just
ignore it.
Post by Vince Morgan
Post by Dok
I am converting some code from GDI to GDI+ as GDI+ supports alpha.
However I have ran into an issue with DrawSting in GDI+.
In my old code wth GDI I used 'SetBkColor' if I wanted the text to have a
background colour applied to it.
Is there a way to do something like this in GDI+ with DrawSting?
I first though of just doing a clear in the rect that the text is to go
in,
Post by Dok
but after quick testng I realised this would not work. Because the GDI
Drawtext only draws that background where the text goes ie. if you use
DT_BOTTOM. Where the clear would work on the whole rect which I am not
after.
Post by Dok
Thanks in advance,
I haven't used GDI+, nor read the docs, so I don't know it. However, in GDI
you would call 'SetBkMode(hDC, mode)' to set the text background to either
OPAQUE or TRANSPARENT.
AFAIK you can still use this in conjunction with GDI+.
--
HTH
Vince Morgan
Remove UNSPAM
Loading...