VB2005 pro Graphics

G

Galen Somerville

Going from VB6 (RAD) to VB2005 (SAD)

The documentation always shows graphics drawing in a paint event like

Private Sub Pictcolor_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles PictColor.Paint

End Sub

But my VB6 program draws to the picturebox PictColor from many different
subs.
Can I draw to PictColor directly from subs as follows:


Here I am drawing pixels

Dim h As Graphics, g As Graphics, p As New Pen(Color.Black)
'Draws the color squares
g = Me.PictColor.CreateGraphics
h = Me.picBW.CreateGraphics
For intNumber0 = 0 To 255
For intNumber1 = 0 To 255
p.Color = Color.FromArgb(intNumber0, 255 - intNumber1, 255 -
intNumber0)
g.DrawEllipse(p, intNumber1, intNumber0, 1, 1)
p.Color = Color.FromArgb(intNumber0, intNumber0, intNumber0)
h.DrawEllipse(p, intNumber1, intNumber0, 1, 1)
Next
Next

Here I am drawing lines

Dim Incr, Index As Short, g As Graphics, p As New Pen(Color.Black), c As
Color
g = Me.PictColor.CreateGraphics
c = Color.FromArgb(mlColors(0))
p.Color = c
Incr = 200 / 5
For Index = 0 To 5 Step 1
g.DrawLine(p, X1, Y1, X2, Y2)
X1 = X1 + Incr
If Index = 4 Then X1 = X1 - 1
X2 = X1
Next Index

Or do I have to make a cajillion different Paint events for PictColor ??

If I can do it outside of Paint events, can I declare the CreateGraphics
once after Form load ??


GalenS
 
C

Chris

Galen said:
Going from VB6 (RAD) to VB2005 (SAD)

The documentation always shows graphics drawing in a paint event like

Private Sub Pictcolor_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles PictColor.Paint

End Sub

But my VB6 program draws to the picturebox PictColor from many different
subs.
Can I draw to PictColor directly from subs as follows:


Here I am drawing pixels

Dim h As Graphics, g As Graphics, p As New Pen(Color.Black)
'Draws the color squares
g = Me.PictColor.CreateGraphics
h = Me.picBW.CreateGraphics
For intNumber0 = 0 To 255
For intNumber1 = 0 To 255
p.Color = Color.FromArgb(intNumber0, 255 - intNumber1, 255 -
intNumber0)
g.DrawEllipse(p, intNumber1, intNumber0, 1, 1)
p.Color = Color.FromArgb(intNumber0, intNumber0, intNumber0)
h.DrawEllipse(p, intNumber1, intNumber0, 1, 1)
Next
Next

Here I am drawing lines

Dim Incr, Index As Short, g As Graphics, p As New Pen(Color.Black), c As
Color
g = Me.PictColor.CreateGraphics
c = Color.FromArgb(mlColors(0))
p.Color = c
Incr = 200 / 5
For Index = 0 To 5 Step 1
g.DrawLine(p, X1, Y1, X2, Y2)
X1 = X1 + Incr
If Index = 4 Then X1 = X1 - 1
X2 = X1
Next Index

Or do I have to make a cajillion different Paint events for PictColor ??

If I can do it outside of Paint events, can I declare the CreateGraphics
once after Form load ??


GalenS

The reason to do it in the paint method is that event gets called each
time the object needs to refresh. So if a window goes across your form
and it now needs to be repainted. How will you know when to repaint if
you don't capture the PictureBox.Paint event?

You can call a sub from inside the paint event and pass in the graphics
object.

Hope this helps
Chris
 
G

Galen Somerville

Chris said:
The reason to do it in the paint method is that event gets called each
time the object needs to refresh. So if a window goes across your form
and it now needs to be repainted. How will you know when to repaint if
you don't capture the PictureBox.Paint event?

You can call a sub from inside the paint event and pass in the graphics
object.

Hope this helps
Chris

ouch. The current program displays Heart sounds in real time. That is it
sweeps across the screen like an oscilloscope with the new data replacing
the old data (with a 5 to 10 pixel gap)

They can freeze the screen to look at the current display. No other program
is allowed to usurp the screen.

Say they want to place a Marker (small vertical line) with the Latency in
milliseconds. The Mouse down, Mouse up and Mouse move displays the Marker
and latency and moves it across the waveform until they are happy with
placement.

Now they can add other Markers (up to six).

This was easy in VB6 but I just can't get it into my mind as to
accomplishing this in VB2005.

GalenS (if I were the other Galen, I would know)
 
K

Ken Halter

Galen Somerville said:
This was easy in VB6 but I just can't get it into my mind as to
accomplishing this in VB2005.

I haven't even attempted drawing anything in .Net but..... can't you create
a sub that does the drawing and call that from "anywhere" plus the paint
event? Sounds like "the VB6 way" I know said:
GalenS (if I were the other Galen, I would know)

Maybe you *are* the other Galen and no one told you? <g>
 
G

Galen Somerville

Ken Halter said:
I haven't even attempted drawing anything in .Net but..... can't you
create a sub that does the drawing and call that from "anywhere" plus the


Maybe you *are* the other Galen and no one told you? <g>
Last things first. In nosing around the net groups I saw a Galen and I think
he (she ?) was an MVP

In "the VB6 way" all of my drawing is in subs. Each sub ends by storing the
points and colors of what it just did.

Then, in those cases where I pop a listbox on top of the drawing, I take the
stored info and repaint (actually redraw) what I messed up.

That's why my question included, "can I do this drawing outside of the
picturebox paint event" and gave examples of what I proposed to do. No
answer yet.

I can't run the VB2005 version until I clear 102 errors and a whole slew of
warnings.

Galen S
 
C

CMM

First, I have oodles of experience drawing in VB.Classic and quite a bit in
..NET....NET drawing is great... though the x2/y2 pixel offsetting is
confusing sometimes (to me).

Let me guess.... you have AutoRedraw on your VB.Classic picture box set? If
not, what's the problem?
I don't really see it.... What's the problem with doing your drawing inside
the paint event?
Sub ... Paint(...)
DrawLines(g)
DrawSomethingElse(g)
End Sub

If you wanted to update the canvas (based on some changes in your numbers or
whatever that you'd maintain on the module class level) you'd simply call
..Refresh on the form or control in question.

Drawing in .NET is extremely extraordinarily fast (at least compared to
VB.Classic). Even if your Paint event is called a 30 times in a second you
would never really notice. If flashing becomes a problem turn on
DoubleBuffering.

Am I misunderstanding your intent somehow? If so, explain further and I'll
try to help.
 
J

James Parsly

I've also been reluctant to jump into vb.net graphics, precisely because its
behavior is substantially different from vb classic.

However, getting a similar behavior to the vb classic autoredraw shouldn't
be
all that hard. What you need to do is to do explicitly in vb.net what vb
classic
was doing for you behind the scenes. With autoredraw turned on, drawing
operations
in vb classic were actually being written to an off-screen picture. The
on-screen image
would automatically be repaired as needed from the off-screen picture.

So what you need to do, I think, is to create a global bitmap and do all
your drawing operations
to it. That way you can do your drawing from your subroutines just like you
are used to.
In the paint event, the only thing you have to do is copy the bitmap onto
the screen.
The "refresh" that you did in vb classic is replaced with something that
forces a paint event
(invalidate?)
 
G

Galen Somerville

The reply by James Parsly sounds like what I need.

I draw to the picture box different things at different times and I coudn't
see how I could accomplish that if I put it all in a Paint event. I would
have case or if statements galore trying to separate it all out.

For instance the sweep is drawing a line from the last pixel position to the
next pixel position with X being incremented by 1. Whereas the vertical
lines (and text) are xored so the mouse movement draws it a second time to
erase it then draws the vertical line at the next position. Etc.

Comments?

GalenS
 
L

Larry Lard

James said:
I've also been reluctant to jump into vb.net graphics, precisely because its
behavior is substantially different from vb classic.

However, getting a similar behavior to the vb classic autoredraw shouldn't
be
all that hard. What you need to do is to do explicitly in vb.net what vb
classic
was doing for you behind the scenes. With autoredraw turned on, drawing
operations
in vb classic were actually being written to an off-screen picture. The
on-screen image
would automatically be repaired as needed from the off-screen picture.

So what you need to do, I think, is to create a global bitmap and do all
your drawing operations
to it. That way you can do your drawing from your subroutines just like you
are used to.
In the paint event, the only thing you have to do is copy the bitmap onto
the screen.

This is exactly what I was going to suggest, after reading the original
post.
The "refresh" that you did in vb classic is replaced with something that
forces a paint event
(invalidate?)

Yes, and we can even be clever and only invalidate that part of the
picture that has just changed - check the various overloads of
Control.Invalidate.
 
G

Galen Somerville

Larry Lard said:
This is exactly what I was going to suggest, after reading the original
post.


Yes, and we can even be clever and only invalidate that part of the
picture that has just changed - check the various overloads of
Control.Invalidate.
Thanks, will look into that. Things are looking up and I'm almost ready to
fly.

GalenS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top