[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Drawing multiple blocks on a picturebox

Bob Butler

5/2/2012 12:41:00 AM

I'm working on something where I need to be able to draw multiple colored
blocks with text on them on a picturebox. After toying with FillRect and
the .Line method and other methods I decide to try formatting a second
picturebox with a single block's layout and then use PaintPicture to copy it
to the final position. It seemed like that would simplify the formatting
and eliminate having to keep creating and destroying brushes since I could
use the BackColor and ForeColor properties to let VB do it for me.

I was surprised when it would only allow me to change the BackColor of the
picturebox once. Further changes were accepted without error and the
property value changed but the color on the screen didn't. I've tried every
combination I can think of using Autoredraw, .picture=.image, Refresh, and
nothing works. I assume that I'm missing something blatantly obvious but if
I am I can't see it. I did find that using 'Set Picture2=Nothing' causes it
to recreate the control and work as I had expected but without that all I
get is the first color block repeated. Any suggestions or a dope slap would
be appreciated.

This is VB6 on XP; all you need is a form with 2 picture boxes.


Private Sub Form_Load()
picture1.Move 0, 0, Me.ScaleWidth / 2, Me.ScaleHeight
picture1.AutoRedraw = True
picture1.BackColor = vbWhite
picture1.BorderStyle = vbBSNone

picture2.Move picture1.Width, 0, picture1.Width, picture1.Height / 5
picture2.BorderStyle = vbBSNone
picture2.AutoRedraw = False

MakeBlock 4, vbBlue ' whichever one is first sets the color for all
MakeBlock 3, vbCyan
MakeBlock 2, vbGreen
MakeBlock 1, vbYellow
MakeBlock 0, vbRed
End Sub

Private Sub MakeBlock(ByVal Offset As Long, ByVal TheColor As Long)
' Set picture2 = Nothing ' <-- this line makes it work as expected
picture2.BackColor = TheColor
picture2.Picture = picture2.Image
picture2.Cls
picture2.Refresh
picture2.Picture = picture2.Image
picture1.PaintPicture picture2.Picture, 0, Offset * picture2.Height, , , , ,
, , vbSrcCopy
End Sub


10 Answers

Schmidt

5/2/2012 1:24:00 AM

0

Am 02.05.2012 02:41, schrieb Bob Butler:

You could give this one a try:

Private Sub Form_Load()
Picture1.Move 0, 0, Me.ScaleWidth / 2, Me.ScaleHeight
Picture1.AutoRedraw = True
Picture1.BackColor = vbWhite
Picture1.BorderStyle = vbBSNone

'when using as rendering-backbuffer, ...
Picture2.Visible = False '...it doesn't need to be visible
Picture2.Move Picture1.Width, 0, Picture1.Width, Picture1.Height / 5
Picture2.BorderStyle = vbBSNone
Picture2.AutoRedraw = True 'Autoredraw True = Rendering to PB.Image

MakeBlock 4, vbBlue
MakeBlock 3, vbCyan
MakeBlock 2, vbGreen
MakeBlock 1, vbYellow
MakeBlock 0, vbRed
End Sub

Private Sub MakeBlock(ByVal Offset As Long, ByVal TheColor As Long)
Picture2.BackColor = TheColor

Picture2.CurrentX = 10
Picture2.CurrentY = 10
Picture2.ForeColor = &HFFFFFF Xor TheColor
Picture2.Print "SomeText in Color: " & Hex(Picture2.ForeColor)

'so we use the .Image-Property also as the source for the Blt
Picture1.PaintPicture Picture2.Image, 0, Offset * Picture2.Height
End Sub


Olaf

Larry Serflaten

5/2/2012 1:42:00 AM

0

Bob Butler wrote:
....
> I was surprised when it would only allow me to change the BackColor of the
> picturebox once.
....
> Any suggestions or a dope slap would be appreciated.


Try this for your MakeBlick code:

Private Sub MakeBlock(ByVal Offset As Long, ByVal TheColor As Long)
Picture2.BackColor = TheColor
Picture1.PaintPicture Picture2.Image, 0, Offset * Picture2.Height, , , , , , , vbSrcCopy
End Sub


LFS

Bob Butler

5/2/2012 1:42:00 AM

0


"Schmidt" <sss@online.de> wrote in message
news:jnq2b8$8so$2@dont-email.me...
> Am 02.05.2012 02:41, schrieb Bob Butler:
>
> You could give this one a try:
<cut>
> 'so we use the .Image-Property also as the source for the Blt
> Picture1.PaintPicture Picture2.Image, 0, Offset * Picture2.Height

That's it!!! It is probably the only thing I didn't try changing before.
Using the .Picture property doesn't pickup the color change but using .Image
does. Thanks!

My prayers have been answered <bgd&r>

Larry Serflaten

5/2/2012 1:47:00 AM

0

Bob Butler wrote:

> picture2.Move picture1.Width, 0, picture1.Width, picture1.Height / 5
> picture2.BorderStyle = vbBSNone
> picture2.AutoRedraw = False


If you plan to print on the 2nd picturebox, set its AutoRedraw to True and
again use its Image property in the MakeBlock routine...

LFS

Dee Earley

5/2/2012 10:36:00 AM

0

On 02/05/2012 01:41, Bob Butler wrote:
> I'm working on something where I need to be able to draw multiple
> colored blocks with text on them on a picturebox. After toying with
> FillRect and the .Line method and other methods I decide to try
> formatting a second picturebox with a single block's layout and then use
> PaintPicture to copy it to the final position. It seemed like that would
> simplify the formatting and eliminate having to keep creating and
> destroying brushes since I could use the BackColor and ForeColor
> properties to let VB do it for me.
>
> I was surprised when it would only allow me to change the BackColor of
> the picturebox once. Further changes were accepted without error and the
> property value changed but the color on the screen didn't. I've tried
> every combination I can think of using Autoredraw, .picture=.image,
> Refresh, and nothing works. I assume that I'm missing something
> blatantly obvious but if I am I can't see it. I did find that using 'Set
> Picture2=Nothing' causes it to recreate the control and work as I had
> expected but without that all I get is the first color block repeated.
> Any suggestions or a dope slap would be appreciated.

You've had several answers, but to explain what's happening, the
BackColor ONLY affects the back colour of the control when it has
nothing drawn into it and no picture.
Once drawn in, it's no longer "blank" so the BackColor doesn't come into
play.

If you want the entire background to be one colour, then you need to
draw a block of that colour.

Picture1.Line (0, 0)-(Picture1.ScaleWidth - 1, Picture1.ScaleHeight -
1), vbRed, BF

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Larry Serflaten

5/3/2012 12:42:00 AM

0

Deanna Earley wrote:

> You've had several answers, but to explain what's happening, the
> BackColor ONLY affects the back colour of the control when it has
> nothing drawn into it and no picture.
> Once drawn in, it's no longer "blank" so the BackColor doesn't come into
> play.

It was my understanding that the BackColor property would clear any previous image, as in this example:

Private Sub Form_Load()
Me.Show
Picture1.BackColor = vbWhite
Picture1.Circle (300, 300), 200
Picture1.Print "TEXT"
End Sub

Private Sub Picture1_Click()
Picture1.BackColor = vbYellow
End Sub


When run the picturebox shows the circle and text on a white background. When the picturebox is clicked, the picturebox turns yellow, erasing the previous circle and text....

LFS

Mike Williams

5/3/2012 8:50:00 AM

0

"Larry Serflaten" <serflaten@gmail.com> wrote in message
news:5070275.3.1336005729845.JavaMail.geo-discussion-forums@ynff7...
>> Deanna Earley wrote:
>> You've had several answers, but to explain what's happening,
>> the BackColor ONLY affects the back colour of the control
>> when it has nothing drawn into it and no picture. Once drawn
>> in, it's no longer "blank" so the BackColor doesn't come into
>> play.
>
> It was my understanding that the BackColor property would
> clear any previous image, as in this example [snipped]:
> When run the picturebox shows the circle and text on a white
> background. When the picturebox is clicked, the picturebox
> turns yellow, erasing the previous circle and text....

You are correct, Larry. The BackColor property /always/ takes effect and if
you change it then any previously drawn graphics is cleared, including any
Autoredraw graphics, because the new BackColor is drawn as a solid block on
top of it. This /always/ happens. It doesn't clear the Picture property of
course and so the Picture property remains intact after a BackColor change.
The reason Bob's original code failed to do what he expected it to do is
because of the order in which things happen.

When VB changes the BackColor it clears all previously drawn graphics by
drawing a solid block of BackColor over it. VB /then/ draws the Picture
property on top. In Bob's original code for Picture2 he was first setting
the BackColor to vbBlue and was then assigning Picture2's Image property to
its Picture property. This resulted in a solid blue Picture of the same size
as the PictureBox. The next thing the code did to Picture2 was to change the
BackColor to vbCyan. This change to vbCyan actually /did/ take effect and
any previous drawing was erased by the new vbCyan. However, immediately
after drawing the new cyan BackColor (and before the next line of graphics
code was executed) VB drew the Picture property on top of the newly drawn
vbCyan BackColor, thereby causing the drawn image to become solid blue again
(because at this stage the Picture was blue). The following line which
assigned the Image property to the Picture property did actually work, but
it was effectively assigning a blue block of colour to the picture property
rather than the Cyan that Bob expected. The same thing happened each time
the code changed to a new BackColor, with the output always being Blue.

As Bob discovered, adding the line Set Picture2 = Nothing (effectively Set
Picture2.Picture = Nothing) made the code work okay. This is because
clearing the picture property each time meant that VB did not have a picture
to draw over any newly set BackColor property, and so the new BackColor each
time was /not/ erased by the Picture property. Of course Olaf's code is
better than the original code with the Set picture2 = Nothing addition
because Olaf's code does the task in a much neater way by using the Image
property directly in the PaintPicture line.

Mike


Dee Earley

5/3/2012 9:07:00 AM

0

On 03/05/2012 01:42, Larry Serflaten wrote:
> Deanna Earley wrote:
>
>> You've had several answers, but to explain what's happening, the
>> BackColor ONLY affects the back colour of the control when it has
>> nothing drawn into it and no picture.
>> Once drawn in, it's no longer "blank" so the BackColor doesn't come into
>> play.
>
> It was my understanding that the BackColor property would clear any previous image, as in this example:

Sorry, clearly my memory is failing prematurely :(
I must admit that I stopped using picture boxes for drawing a long time ago.

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Mike Williams

5/3/2012 10:14:00 AM

0

"Deanna Earley" <dee.earley@icode.co.uk> wrote in message
news:jnthqq$hce$1@speranza.aioe.org...
>> On 03/05/2012 01:42, Larry Serflaten wrote:
>> It was my understanding that the BackColor property
>> would clear any previous image, as in this example:
>
> Sorry, clearly my memory is failing prematurely :(
> I must admit that I stopped using picture boxes
> for drawing a long time ago.

Oh right. Gotcha. You were wrong, but you were wrong only because you are
better than the poor ignorant people who use the PictureBoxes that you gave
up using years ago and who of course do not know anything about how to draw
into bitmaps using GDI functions or any other methods. Nice fall ;-)

Mike


Dee Earley

5/3/2012 10:39:00 AM

0

On 03/05/2012 11:14, Mike Williams wrote:
> "Deanna Earley" <dee.earley@icode.co.uk> wrote in message
> news:jnthqq$hce$1@speranza.aioe.org...
>>> On 03/05/2012 01:42, Larry Serflaten wrote:
>>> It was my understanding that the BackColor property
>>> would clear any previous image, as in this example:
>>
>> Sorry, clearly my memory is failing prematurely :(
>> I must admit that I stopped using picture boxes
>> for drawing a long time ago.
>
> Oh right. Gotcha. You were wrong.

Well, I can't be perfect all the time...

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)