473,782 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Background color of rectangle

I have a rectangle around text that I want to fill in with color. I do not
know the height of the rectangle until I actually go through and draw out
the text. Is there a way of filling in the rectangle that will allow the
text to show through?

I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)

but that isn't exactly the effect I wanted.

Is the only way to do this is to actually calculate what the height of the
rectangle first, fill in the color then draw the text lines?
Nov 21 '05 #1
5 4708
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:jt******** *********@twist er.nyroc.rr.com :
I have a rectangle around text that I want to fill in with color. I do
not know the height of the rectangle until I actually go through and
draw out the text. Is there a way of filling in the rectangle that
will allow the text to show through?

I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)

but that isn't exactly the effect I wanted.

Is the only way to do this is to actually calculate what the height of
the rectangle first, fill in the color then draw the text lines?


Use measurestring to get the wodth and height without actually drawing
the string.

Dim sourceText As String = sender.Text
Dim measureStringBi tmap As New Bitmap(CInt(sen der.ClientSize. Width), _
CInt(sender.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = Graphics.FromIm age(measureStri ngBitmap)

' need to set the string format to get line-wraps
Dim textStringForma t As StringFormat = _
CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString

Graphics.Measur eString(sourceT ext, _
sender.Font, New SizeF(sender.Cl ientSize.Width, _
sender.ClientSi ze.Height), textStringForma t, Nothing, Nothing)

measureStringGr aphics.Dispose( )

'textSize now contins the width and height that the text takes up when
'draw with the given font and stringformat.
Nov 21 '05 #2
Sorry I wasn't clear enough, the rectangle is around a number of text lines.
The number of text lines can be any where between 8 and 45 lines. I do not
know how many lines up front. I am trying to avoid going through and
calculating the lines, then create the rectangle and fill, then go back and
actually create the text lines.

I was just wondering if I could fill a rectangle around all the text with
color without covering up the text.

"hoppy" <no***@invalid. null> wrote in message
news:Xn******** **********@194. 117.143.53...
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:jt******** *********@twist er.nyroc.rr.com :
I have a rectangle around text that I want to fill in with color. I do
not know the height of the rectangle until I actually go through and
draw out the text. Is there a way of filling in the rectangle that
will allow the text to show through?

I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)

but that isn't exactly the effect I wanted.

Is the only way to do this is to actually calculate what the height of
the rectangle first, fill in the color then draw the text lines?


Use measurestring to get the wodth and height without actually drawing
the string.

Dim sourceText As String = sender.Text
Dim measureStringBi tmap As New Bitmap(CInt(sen der.ClientSize. Width), _
CInt(sender.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = Graphics.FromIm age(measureStri ngBitmap)

' need to set the string format to get line-wraps
Dim textStringForma t As StringFormat = _
CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString

Graphics.Measur eString(sourceT ext, _
sender.Font, New SizeF(sender.Cl ientSize.Width, _
sender.ClientSi ze.Height), textStringForma t, Nothing, Nothing)

measureStringGr aphics.Dispose( )

'textSize now contins the width and height that the text takes up when
'draw with the given font and stringformat.

Nov 21 '05 #3
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:vq******** ***@twister.nyr oc.rr.com:
Sorry I wasn't clear enough, the rectangle is around a number of text
lines. The number of text lines can be any where between 8 and 45
lines. I do not know how many lines up front. I am trying to avoid
going through and calculating the lines, then create the rectangle and
fill, then go back and actually create the text lines.

I was just wondering if I could fill a rectangle around all the text
with color without covering up the text.

"hoppy" <no***@invalid. null> wrote in message
news:Xn******** **********@194. 117.143.53...
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:jt******** *********@twist er.nyroc.rr.com :
I have a rectangle around text that I want to fill in with color. I
do not know the height of the rectangle until I actually go through
and draw out the text. Is there a way of filling in the rectangle
that will allow the text to show through?

I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)

but that isn't exactly the effect I wanted.

Is the only way to do this is to actually calculate what the height
of the rectangle first, fill in the color then draw the text lines?


Use measurestring to get the wodth and height without actually
drawing the string.

Dim sourceText As String = sender.Text
Dim measureStringBi tmap As New Bitmap(CInt(sen der.ClientSize. Width),
_
CInt(sender.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = Graphics.FromIm age(measureStri ngBitmap)

' need to set the string format to get line-wraps
Dim textStringForma t As StringFormat = _
CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString

Graphics.Measur eString(sourceT ext, _
sender.Font, New SizeF(sender.Cl ientSize.Width, _
sender.ClientSi ze.Height), textStringForma t, Nothing, Nothing)

measureStringGr aphics.Dispose( )

'textSize now contins the width and height that the text takes up
when 'draw with the given font and stringformat.



Not sure what you mean..

This will make a bitmap from a string, set the backcolor to orange, and
wrap the text to the number of lines required to fit the text. (so long
as label1 is bit enough to hold all the text).

With a form with these controls:
textbox1 for entering the text
label1 the size of this is the size of the bit of papaer, or control or
form or whatever it is that you want the text to be scaled for (so it's
width is the right size),
button1
and a picturebox to show the resultant bitmap:

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click

'fill label1 with text from textbox1
Dim sourceText As String = TextBox1.Text
'it will draw into label1, we pass the width and height.
'The height isn't really important here. If the text
'doesn't fit then it gets clipped. If the text fits
'easily then the size returned is just the size of the text.
Dim measureStringBi tmap As New _
Bitmap(CInt(Lab el1.ClientSize. Width), _
CInt(Label1.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = _
Graphics.FromIm age(measureStri ngBitmap)
'setting stringformat will ensure the lines wrap.
Dim textStringForma t As StringFormat _
= CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
'we can get the number of text lines that are drawn
'and the number of characters that fit into the final size by
'passing a couple of variables byref
Dim charsFitted As Integer
Dim linesUsed As Integer
Dim textSize As New SizeF
'draw the string from textbox1
'into an area the size of label1,
'using the font from textbox1
'using a stringformat to wrap the lines
'get the size of rectangle required to fit
'this text.
textSize = _
measureStringGr aphics.MeasureS tring(sourceTex t, TextBox1.Font, _
New SizeF(Label1.Cl ientSize.Width, Label1.ClientSi ze.Height), _
textStringForma t, charsFitted, linesUsed)
Debug.WriteLine ("Chars fitted into label: " & charsFitted & _
" of " & sourceText.Leng th)
Debug.WriteLine ("Lines fitted into label: " & linesUsed)
measureStringGr aphics.Dispose( )
measureStringBi tmap.Dispose()
'create a bitmap of the text, with backcolor set
Dim stringBitmap As New Bitmap(CInt(tex tSize.Width), _
CInt(textSize.H eight))
'create a graphics to draw on
Dim g As Graphics = Graphics.FromIm age(stringBitma p)
'fill the back of the bitmap with a color
g.Clear(System. Drawing.Color.O range)
'draw the string
Dim b As New SolidBrush(Colo r.Black)
Dim recF As New RectangleF(0.0F , 0.0F, _
textSize.Width, textSize.Height )
g.DrawString(so urceText, TextBox1.Font, _
b, recF, textStringForma t)
b.Dispose()
textStringForma t.Dispose()
g.Dispose()
'now you can draw the bitmap where you want the string.
'I'll just show it in a picturebox..
Me.PictureBox1. Image = stringBitmap

End Sub

Nov 21 '05 #4
I am using the PrintDocument Control (and PrintPreview) and using
Graphics.DrawSt ring to put out the text. The text uses 2 different fonts
depending on what text is generated.

I store the starting position of Y (I already know where x begins and ends).
When all the text is generated I get the finnal position of Y and draw a
rectangle around the text. (actualy I draw two rectangles and a few vertical
line to give kind of a grid effect, but completely fill the outer most
rectangle)

what I wanted to do is fill the rectangle with color before I did:
e.Graphics.Draw Rectangle(repor tPen, 10, gridStartY, x, gridEndY -
gridStartY)

but if I do:

e.Graphics.Fill Rectangle(fillB rush, 10, gridStartY, x, gridEndY -
gridStartY)

It covers up the text already written and I was wondering if there was a way
to use e.Graphics.Fill Rectangle that would allow the text to show as if it
was ontop of the FillRectangle.

I was able to change my logic so that I could get the start position and
calculate the height of the rectangle and fill it before I start generating
the text using Graphics.DrawSt ring.

I was just wondering if there was a way to use
e.Graphics.Fill Rectangle(fillB rush, 10, gridStartY, e.PageBounds.Wi dth - 15,
gridEndY - gridStartY) that would not cover up the text on the report if it
was generated first.
"hoppy" <no***@invalid. null> wrote in message
news:Xn******** **********@194. 117.143.38...
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:vq******** ***@twister.nyr oc.rr.com:
Sorry I wasn't clear enough, the rectangle is around a number of text
lines. The number of text lines can be any where between 8 and 45
lines. I do not know how many lines up front. I am trying to avoid
going through and calculating the lines, then create the rectangle and
fill, then go back and actually create the text lines.

I was just wondering if I could fill a rectangle around all the text
with color without covering up the text.

"hoppy" <no***@invalid. null> wrote in message
news:Xn******** **********@194. 117.143.53...
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in
news:jt******** *********@twist er.nyroc.rr.com :

I have a rectangle around text that I want to fill in with color. I
do not know the height of the rectangle until I actually go through
and draw out the text. Is there a way of filling in the rectangle
that will allow the text to show through?

I did try:
myColor = Color.FromArgb( 127, Color.GreenYell ow)
Dim fillBrush As New SolidBrush(myCo lor)

but that isn't exactly the effect I wanted.

Is the only way to do this is to actually calculate what the height
of the rectangle first, fill in the color then draw the text lines?


Use measurestring to get the wodth and height without actually
drawing the string.

Dim sourceText As String = sender.Text
Dim measureStringBi tmap As New Bitmap(CInt(sen der.ClientSize. Width),
_
CInt(sender.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = Graphics.FromIm age(measureStri ngBitmap)

' need to set the string format to get line-wraps
Dim textStringForma t As StringFormat = _
CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString

Graphics.Measur eString(sourceT ext, _
sender.Font, New SizeF(sender.Cl ientSize.Width, _
sender.ClientSi ze.Height), textStringForma t, Nothing, Nothing)

measureStringGr aphics.Dispose( )

'textSize now contins the width and height that the text takes up
when 'draw with the given font and stringformat.



Not sure what you mean..

This will make a bitmap from a string, set the backcolor to orange, and
wrap the text to the number of lines required to fit the text. (so long
as label1 is bit enough to hold all the text).

With a form with these controls:
textbox1 for entering the text
label1 the size of this is the size of the bit of papaer, or control or
form or whatever it is that you want the text to be scaled for (so it's
width is the right size),
button1
and a picturebox to show the resultant bitmap:

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click

'fill label1 with text from textbox1
Dim sourceText As String = TextBox1.Text
'it will draw into label1, we pass the width and height.
'The height isn't really important here. If the text
'doesn't fit then it gets clipped. If the text fits
'easily then the size returned is just the size of the text.
Dim measureStringBi tmap As New _
Bitmap(CInt(Lab el1.ClientSize. Width), _
CInt(Label1.Cli entSize.Height) )
Dim measureStringGr aphics As Graphics
measureStringGr aphics = _
Graphics.FromIm age(measureStri ngBitmap)
'setting stringformat will ensure the lines wrap.
Dim textStringForma t As StringFormat _
= CType(StringFor mat.GenericDefa ult.Clone(), StringFormat)
'we can get the number of text lines that are drawn
'and the number of characters that fit into the final size by
'passing a couple of variables byref
Dim charsFitted As Integer
Dim linesUsed As Integer
Dim textSize As New SizeF
'draw the string from textbox1
'into an area the size of label1,
'using the font from textbox1
'using a stringformat to wrap the lines
'get the size of rectangle required to fit
'this text.
textSize = _
measureStringGr aphics.MeasureS tring(sourceTex t, TextBox1.Font, _
New SizeF(Label1.Cl ientSize.Width, Label1.ClientSi ze.Height), _
textStringForma t, charsFitted, linesUsed)
Debug.WriteLine ("Chars fitted into label: " & charsFitted & _
" of " & sourceText.Leng th)
Debug.WriteLine ("Lines fitted into label: " & linesUsed)
measureStringGr aphics.Dispose( )
measureStringBi tmap.Dispose()
'create a bitmap of the text, with backcolor set
Dim stringBitmap As New Bitmap(CInt(tex tSize.Width), _
CInt(textSize.H eight))
'create a graphics to draw on
Dim g As Graphics = Graphics.FromIm age(stringBitma p)
'fill the back of the bitmap with a color
g.Clear(System. Drawing.Color.O range)
'draw the string
Dim b As New SolidBrush(Colo r.Black)
Dim recF As New RectangleF(0.0F , 0.0F, _
textSize.Width, textSize.Height )
g.DrawString(so urceText, TextBox1.Font, _
b, recF, textStringForma t)
b.Dispose()
textStringForma t.Dispose()
g.Dispose()
'now you can draw the bitmap where you want the string.
'I'll just show it in a picturebox..
Me.PictureBox1. Image = stringBitmap

End Sub

Nov 21 '05 #5

you have to fill first, then draw your text.

i've done the same type of thing before that you're doing. first i fill the
client area, next draw any borders needed, then i drawstring. fillrectangle
is doing exactly what it should...it's just that in this order, it fills in
over the text you've already output...just change your order of operations.

hth,

me
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4699
by: Smith Duggan | last post by:
I am attempting to change the background properties of a rectangle in the code, and have been stuck now for a day and a half. This is my first access app. Please be gentle with my steaming pile of code: If I include the following in a subroutine, the rectangle properties will change just fine for a rectangle with the name of "Pos4Box": Me!Pos4Box.BackColor = 8421504 I have nine rectangles Pos1Box - Pos9Box and have a variable...
1
4121
by: Robert W. | last post by:
In my Winforms app I'm trying to get an image's background to appear transparent on a form that has a gradient background. So I added a PictureBox and then attempted to add a custom paint command for the PictureBox. But it's not working. Here's the code I've written: public void InitializeLinearGradients() { this.Paint += new PaintEventHandler(PaintClient); this.SizeChanged += new EventHandler(SizeClient); pictureLogo.Paint += new...
2
8799
by: Nathan | last post by:
How doy you resize the background image to fill a form when the form is resized?
4
2673
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a rectangle drawn on the form or not. It works perfectly as shown below. But it won't work if I change the values of scaleFactor, rotateFactor, translateFactorX, translateFactorY in the program. I would like to 'correct' the values of e.X and e.Y in...
8
9279
by: Matt | last post by:
I've been searching around for a simple way to display a background image in my MDI form's client area. I want to be able to load the image from file on start up and have it resize as the MDI form is resized. It's the resizing part that I am having trouble with. I've seen a few old posts that point in the right direction but nothing simple. I wondered if they has been any new ideas I have missed.
4
1975
by: John Smith | last post by:
Hi All; I am using the HeaderAndDataAlignColumn class from Ken Tucker. I have customized it for a wider row. I reposition the DrawString method so the text is centered vertically in the row. My problem now is that on ReadOnly cells the shaded background is not at the top of the cell. I adjust the rectangle Height so that the shading does not drop into the next row.
0
1403
by: Bharathi Kumar | last post by:
Hi, Iam working in a windows application using .Net framework 2.0, VS.NET 2005 and CR X. Scenario : When we create a report using CR by default they will be assigned some background colors.
8
5370
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In this code there is a panel panel1, that I populate with a lable in the foreground. Then when I click on "button1" a backgroundworker thread in async mode is started. When the backgoundworker thread completes the thread returns a panel to populate...
9
8029
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have a MDI Container Form, with one label control on the Background of this container form. When I now open a child form, this form is behind the label ... and this looks ugly ... :-)) When I set the label with "SendToBack", it completely disapears. I also tried to set the child form like "BringToFront" but this also does not help.
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.