Results 1 to 6 of 6

Thread: Redraw images when lost focus

  1. #1

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330

    Redraw images when lost focus

    He everyone!

    I'm using this function to draw images on my pictureBox
    VB Code:
    1. Dim gr As Graphics = pctBox.CreateGraphics
    2. 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:
    1. Public Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    2.         Dim iLoop, iX, iY As Integer
    3.  
    4.         For iLoop = 0 To 29
    5.             iX = Coordinate2Pixel(pioPion(iLoop).GetCoordinateX())
    6.             iY = Coordinate2Pixel(pioPion(iLoop).GetCoordinateY())
    7.  
    8.             Dim gr As Graphics = pctBox.CreateGraphics
    9.             gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
    10.         Next
    11. 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

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Redraw images when lost focus

    Originally posted by Bloged
    He everyone!

    I'm using this function to draw images on my pictureBox
    VB Code:
    1. Dim gr As Graphics = pctBox.CreateGraphics
    2. 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:
    1. Public Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    2.         Dim iLoop, iX, iY As Integer
    3.  
    4.         For iLoop = 0 To 29
    5.             iX = Coordinate2Pixel(pioPion(iLoop).GetCoordinateX())
    6.             iY = Coordinate2Pixel(pioPion(iLoop).GetCoordinateY())
    7.  
    8.             Dim gr As Graphics = pctBox.CreateGraphics
    9.             gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
    10.         Next
    11. 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.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    Thanks MrPolite

    Will try it as soon as I'm home!


    Edit:
    Tried it here with an simple test... But it won't work it looks like it redraws them but then 'removes' them directly after... (Same thing happend when using frmMain_Activated).

    Any ideas


    Grtz,

    Bloged
    Last edited by Bloged; Mar 17th, 2004 at 06:10 AM.

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm post the code you have in the paint event. You're not clearing or refreshing the form or anything ?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    Sorry for the delay but here is the code I'm using:

    VB Code:
    1. 'If map is (redraw) draw the pions back on it
    2. Private Sub pctMap_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pctMap.Paint
    3.     'If game is started
    4.     If booStarted = True Then
    5.         Map.DrawAllPions(pioPion, pctMap)
    6.     End If
    7. End Sub

    and the DrawAllPions function:
    VB Code:
    1. '(Re)draws all the pions on the map
    2. Public Shared Function DrawAllPions(ByRef pioPion() As Pion, ByVal pctBox As PictureBox)
    3.     Dim iLoop, iX, iY As Integer
    4.  
    5.     For iLoop = 0 To 29
    6.         iX = Coordinate2Pixel(pioPion(iLoop).GetCoordinateX())
    7.         iY = Coordinate2Pixel(pioPion(iLoop).GetCoordinateY())
    8.  
    9.         Dim gr As Graphics = pctBox.CreateGraphics
    10.         gr.DrawImage(New Bitmap("c:\pion.gif"), iX, iY)
    11.     Next
    12. End Function

    Grtz,

    Bloged

  6. #6

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    *bump*

    Anyone?

    Grtz,

    Bloged

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width