PC Review


Reply
Thread Tools Rate Thread

Background color of rectangle

 
 
DazedAndConfused
Guest
Posts: n/a
 
      9th Aug 2005
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.GreenYellow)
Dim fillBrush As New SolidBrush(myColor)

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?


 
Reply With Quote
 
 
 
 
hoppy
Guest
Posts: n/a
 
      9th Aug 2005
"DazedAndConfused" <(E-Mail Removed)> wrote in
news:jt1Ke.6329$(E-Mail Removed):

> 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.GreenYellow)
> Dim fillBrush As New SolidBrush(myColor)
>
> 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 measureStringBitmap As New Bitmap(CInt(sender.ClientSize.Width), _
CInt(sender.ClientSize.Height))
Dim measureStringGraphics As Graphics
measureStringGraphics = Graphics.FromImage(measureStringBitmap)

' need to set the string format to get line-wraps
Dim textStringFormat As StringFormat = _
CType(StringFormat.GenericDefault.Clone(), StringFormat)
Dim textSize As New SizeFtextSize = measureString

Graphics.MeasureString(sourceText, _
sender.Font, New SizeF(sender.ClientSize.Width, _
sender.ClientSize.Height), textStringFormat, Nothing, Nothing)

measureStringGraphics.Dispose()

'textSize now contins the width and height that the text takes up when
'draw with the given font and stringformat.
 
Reply With Quote
 
DazedAndConfused
Guest
Posts: n/a
 
      9th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:Xns96AD8F1F01769blah@194.117.143.53...
> "DazedAndConfused" <(E-Mail Removed)> wrote in
> news:jt1Ke.6329$(E-Mail Removed):
>
>> 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.GreenYellow)
>> Dim fillBrush As New SolidBrush(myColor)
>>
>> 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 measureStringBitmap As New Bitmap(CInt(sender.ClientSize.Width), _
> CInt(sender.ClientSize.Height))
> Dim measureStringGraphics As Graphics
> measureStringGraphics = Graphics.FromImage(measureStringBitmap)
>
> ' need to set the string format to get line-wraps
> Dim textStringFormat As StringFormat = _
> CType(StringFormat.GenericDefault.Clone(), StringFormat)
> Dim textSize As New SizeFtextSize = measureString
>
> Graphics.MeasureString(sourceText, _
> sender.Font, New SizeF(sender.ClientSize.Width, _
> sender.ClientSize.Height), textStringFormat, Nothing, Nothing)
>
> measureStringGraphics.Dispose()
>
> 'textSize now contins the width and height that the text takes up when
> 'draw with the given font and stringformat.



 
Reply With Quote
 
hoppy
Guest
Posts: n/a
 
      9th Aug 2005
"DazedAndConfused" <(E-Mail Removed)> wrote in
news:vq2Ke.4$%(E-Mail Removed):

> 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" <(E-Mail Removed)> wrote in message
> news:Xns96AD8F1F01769blah@194.117.143.53...
>> "DazedAndConfused" <(E-Mail Removed)> wrote in
>> news:jt1Ke.6329$(E-Mail Removed):
>>
>>> 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.GreenYellow)
>>> Dim fillBrush As New SolidBrush(myColor)
>>>
>>> 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 measureStringBitmap As New Bitmap(CInt(sender.ClientSize.Width),
>> _
>> CInt(sender.ClientSize.Height))
>> Dim measureStringGraphics As Graphics
>> measureStringGraphics = Graphics.FromImage(measureStringBitmap)
>>
>> ' need to set the string format to get line-wraps
>> Dim textStringFormat As StringFormat = _
>> CType(StringFormat.GenericDefault.Clone(), StringFormat)
>> Dim textSize As New SizeFtextSize = measureString
>>
>> Graphics.MeasureString(sourceText, _
>> sender.Font, New SizeF(sender.ClientSize.Width, _
>> sender.ClientSize.Height), textStringFormat, Nothing, Nothing)
>>
>> measureStringGraphics.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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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 measureStringBitmap As New _
Bitmap(CInt(Label1.ClientSize.Width), _
CInt(Label1.ClientSize.Height))
Dim measureStringGraphics As Graphics
measureStringGraphics = _
Graphics.FromImage(measureStringBitmap)
'setting stringformat will ensure the lines wrap.
Dim textStringFormat As StringFormat _
= CType(StringFormat.GenericDefault.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 = _
measureStringGraphics.MeasureString(sourceText, TextBox1.Font, _
New SizeF(Label1.ClientSize.Width, Label1.ClientSize.Height), _
textStringFormat, charsFitted, linesUsed)
Debug.WriteLine("Chars fitted into label: " & charsFitted & _
" of " & sourceText.Length)
Debug.WriteLine("Lines fitted into label: " & linesUsed)
measureStringGraphics.Dispose()
measureStringBitmap.Dispose()
'create a bitmap of the text, with backcolor set
Dim stringBitmap As New Bitmap(CInt(textSize.Width), _
CInt(textSize.Height))
'create a graphics to draw on
Dim g As Graphics = Graphics.FromImage(stringBitmap)
'fill the back of the bitmap with a color
g.Clear(System.Drawing.Color.Orange)
'draw the string
Dim b As New SolidBrush(Color.Black)
Dim recF As New RectangleF(0.0F, 0.0F, _
textSize.Width, textSize.Height)
g.DrawString(sourceText, TextBox1.Font, _
b, recF, textStringFormat)
b.Dispose()
textStringFormat.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

 
Reply With Quote
 
DazedAndConfused
Guest
Posts: n/a
 
      9th Aug 2005
I am using the PrintDocument Control (and PrintPreview) and using
Graphics.DrawString 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.DrawRectangle(reportPen, 10, gridStartY, x, gridEndY -
gridStartY)

but if I do:

e.Graphics.FillRectangle(fillBrush, 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.FillRectangle 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.DrawString.

I was just wondering if there was a way to use
e.Graphics.FillRectangle(fillBrush, 10, gridStartY, e.PageBounds.Width - 15,
gridEndY - gridStartY) that would not cover up the text on the report if it
was generated first.


"hoppy" <(E-Mail Removed)> wrote in message
news:Xns96ADAC4E04AE0blah@194.117.143.38...
> "DazedAndConfused" <(E-Mail Removed)> wrote in
> news:vq2Ke.4$%(E-Mail Removed):
>
>> 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" <(E-Mail Removed)> wrote in message
>> news:Xns96AD8F1F01769blah@194.117.143.53...
>>> "DazedAndConfused" <(E-Mail Removed)> wrote in
>>> news:jt1Ke.6329$(E-Mail Removed):
>>>
>>>> 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.GreenYellow)
>>>> Dim fillBrush As New SolidBrush(myColor)
>>>>
>>>> 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 measureStringBitmap As New Bitmap(CInt(sender.ClientSize.Width),
>>> _
>>> CInt(sender.ClientSize.Height))
>>> Dim measureStringGraphics As Graphics
>>> measureStringGraphics = Graphics.FromImage(measureStringBitmap)
>>>
>>> ' need to set the string format to get line-wraps
>>> Dim textStringFormat As StringFormat = _
>>> CType(StringFormat.GenericDefault.Clone(), StringFormat)
>>> Dim textSize As New SizeFtextSize = measureString
>>>
>>> Graphics.MeasureString(sourceText, _
>>> sender.Font, New SizeF(sender.ClientSize.Width, _
>>> sender.ClientSize.Height), textStringFormat, Nothing, Nothing)
>>>
>>> measureStringGraphics.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(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) 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 measureStringBitmap As New _
> Bitmap(CInt(Label1.ClientSize.Width), _
> CInt(Label1.ClientSize.Height))
> Dim measureStringGraphics As Graphics
> measureStringGraphics = _
> Graphics.FromImage(measureStringBitmap)
> 'setting stringformat will ensure the lines wrap.
> Dim textStringFormat As StringFormat _
> = CType(StringFormat.GenericDefault.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 = _
> measureStringGraphics.MeasureString(sourceText, TextBox1.Font, _
> New SizeF(Label1.ClientSize.Width, Label1.ClientSize.Height), _
> textStringFormat, charsFitted, linesUsed)
> Debug.WriteLine("Chars fitted into label: " & charsFitted & _
> " of " & sourceText.Length)
> Debug.WriteLine("Lines fitted into label: " & linesUsed)
> measureStringGraphics.Dispose()
> measureStringBitmap.Dispose()
> 'create a bitmap of the text, with backcolor set
> Dim stringBitmap As New Bitmap(CInt(textSize.Width), _
> CInt(textSize.Height))
> 'create a graphics to draw on
> Dim g As Graphics = Graphics.FromImage(stringBitmap)
> 'fill the back of the bitmap with a color
> g.Clear(System.Drawing.Color.Orange)
> 'draw the string
> Dim b As New SolidBrush(Color.Black)
> Dim recF As New RectangleF(0.0F, 0.0F, _
> textSize.Width, textSize.Height)
> g.DrawString(sourceText, TextBox1.Font, _
> b, recF, textStringFormat)
> b.Dispose()
> textStringFormat.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
>



 
Reply With Quote
 

Guest
Posts: n/a
 
      10th Aug 2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text box control with background color and round edge rectangle in Windows Mobile Bright Microsoft Dot NET Framework 0 6th Jun 2008 05:36 PM
How make background dark blue with yellow text within rectangle? =?Utf-8?B?c21hZ3M=?= Microsoft Access Getting Started 1 27th Jan 2006 03:35 PM
? Selection Rectangle Color Alec S. Windows XP Customization 4 9th Jun 2004 04:35 PM
help with Form1 program - drawing rectangle and background colouring on mousemove Colin McGuire Microsoft VB .NET 2 6th Apr 2004 08:30 AM
Color a rectangle box background based on a date Mark Microsoft Access Forms 1 30th Aug 2003 11:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:49 AM.