Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Proper use of Refresh() and Invalidate()

Proper use of Refresh() and Invalidate()

Scheduled Pinned Locked Moved C#
graphicsquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mutty
    wrote on last edited by
    #1

    I'm drawing a few shapes on an img field on a windows form by overriding OnPaint, and using a double buffer as many people have detailed here. However, my shapes disappear after they are drawn. If I put a Refresh() at the end of my OnPaint override, it works (but obviously bogs down)If I set a timer it flickers at the rate of the timer. What am I doing wrong?

    M 1 Reply Last reply
    0
    • M Mutty

      I'm drawing a few shapes on an img field on a windows form by overriding OnPaint, and using a double buffer as many people have detailed here. However, my shapes disappear after they are drawn. If I put a Refresh() at the end of my OnPaint override, it works (but obviously bogs down)If I set a timer it flickers at the rate of the timer. What am I doing wrong?

      M Offline
      M Offline
      mikker_123
      wrote on last edited by
      #2

      Uf, that happend a lot when I first used drawing. What I learned is that drawing directly in OnPaint event isn't good solution in most cases, better draw it in Form_Load for example. What you must know is that in this case you need refresh you drawing in Form's Activated event... because if you hide ur Form (which happends a lot when in debug mode) and than show it again your precious work will be gone... If you follow my suggestion and drop function calling in Page_Load then you'll have code like this: Form_Load() { DrawStuff(); } Form_Activated() { DrawStuff(); } In case you stick with OnPaint() I guess simple this.Invalidate(true); in Form_Activated should do the trick. I guess someone will eventualy give you right answer but if you get stuck, drop a code - that will help us to help you. cheers

      M 1 Reply Last reply
      0
      • M mikker_123

        Uf, that happend a lot when I first used drawing. What I learned is that drawing directly in OnPaint event isn't good solution in most cases, better draw it in Form_Load for example. What you must know is that in this case you need refresh you drawing in Form's Activated event... because if you hide ur Form (which happends a lot when in debug mode) and than show it again your precious work will be gone... If you follow my suggestion and drop function calling in Page_Load then you'll have code like this: Form_Load() { DrawStuff(); } Form_Activated() { DrawStuff(); } In case you stick with OnPaint() I guess simple this.Invalidate(true); in Form_Activated should do the trick. I guess someone will eventualy give you right answer but if you get stuck, drop a code - that will help us to help you. cheers

        M Offline
        M Offline
        Mutty
        wrote on last edited by
        #3

        Thank you for the response I am still a bit confused, however. One of the shapes I'm drawing I'd like to follow the mouse as it moves over the img box - So I would have to refresh() each time the mousemove event triggers, correct? If I put the refresh() in the mousemove event it seems to work fine as long as the mouse is moving, but as soon as the mouse stops, the shape disappears. This is really frustrating! Thank you for any help - mutty

        M 1 Reply Last reply
        0
        • M Mutty

          Thank you for the response I am still a bit confused, however. One of the shapes I'm drawing I'd like to follow the mouse as it moves over the img box - So I would have to refresh() each time the mousemove event triggers, correct? If I put the refresh() in the mousemove event it seems to work fine as long as the mouse is moving, but as soon as the mouse stops, the shape disappears. This is really frustrating! Thank you for any help - mutty

          M Offline
          M Offline
          mikker_123
          wrote on last edited by
          #4

          This is working for me... even if Refresh in MouseHover is commented... but maybe you should try uncommenting it. private void pictureBox1_MouseHover(object sender, System.EventArgs e) { //Refresh(); } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Refresh(); } private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { using (Graphics g = this.CreateGraphics()) { PaintEventArgs pa = new PaintEventArgs(g, this.ClientRectangle); g.SmoothingMode = SmoothingMode.AntiAlias; //Draw anything pa.Graphics.DrawLine(new Pen(Color.FromArgb(255, 0, 0, 0)), new PointF(MousePosition.X-10, MousePosition.Y+10), new PointF(MousePosition.X-20, MousePosition.Y+20)); } } In any case, if you can't get it to work, drop me .cs on pele@zig-zag.net and I'll look into it. Cheers

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups