Redraw images when lost focus
He everyone!
I'm using this function to draw images on my pictureBox
VB Code:
Dim gr As Graphics = pctBox.CreateGraphics
gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
As you know when you minimize your application all those things you drew on it are gone so I searched for the GotFocus or something and found/created this:
VB Code:
Public Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Dim iLoop, iX, iY As Integer
For iLoop = 0 To 29
iX = Coordinate2Pixel(pioPion(iLoop).GetCoordinateX())
iY = Coordinate2Pixel(pioPion(iLoop).GetCoordinateY())
Dim gr As Graphics = pctBox.CreateGraphics
gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
Next
End Sub
To draw them back whenever my app lost focus... Now does this work when I popup an messagebox... but not when I minimize & maximize my application anyone with good ideas?!
Thnx in advance,
Bloged
Re: Redraw images when lost focus
Quote:
Originally posted by Bloged
He everyone!
I'm using this function to draw images on my pictureBox
VB Code:
Dim gr As Graphics = pctBox.CreateGraphics
gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
As you know when you minimize your application all those things you drew on it are gone so I searched for the GotFocus or something and found/created this:
VB Code:
Public Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Dim iLoop, iX, iY As Integer
For iLoop = 0 To 29
iX = Coordinate2Pixel(pioPion(iLoop).GetCoordinateX())
iY = Coordinate2Pixel(pioPion(iLoop).GetCoordinateY())
Dim gr As Graphics = pctBox.CreateGraphics
gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
Next
End Sub
To draw them back whenever my app lost focus... Now does this work when I popup an messagebox... but not when I minimize & maximize my application anyone with good ideas?!
Thnx in advance,
Bloged
two answers: you can make the image to stay forever if you draw it on the picturebox's image.
instead of
Dim gr As Graphics = pctBox.CreateGraphics
you could use
Dim gr As Graphics = Graphics.FromImage(pctBox.Image)
This way the actual image contained within the picturebox will be modified. You will only have to draw the grahpics ONCE and wont need to call your code again.
other way: if for some other reason you need to use the method that you are using right now (Grahpics.CreateGrahpics), you should put your code in the Paint event of your picturebox. The paint even is fired everytime the control is to be redrawn. Put this code in the Paint event of pctBox:
e.Graphics.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
notice the e.Graphics.... in the paint event a graphics object is provided already so you dont need to make your own graphics object.