tags:

views:

149

answers:

1

Hi, I was looking about some GDI tutorial but everything I have found so far works with OnPaint method, which passes Paintarguments to Graphics. I have not found how to start from scratch, I mean how to use Graphics class itself? This is the whole code I have treid that just doesnt work for me:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Pen pen= new Pen(Color.Red, 3);
            Graphics g;
            g = this.CreateGraphics();
    g.DrawEllipse(pen, 150, 150, 100, 100);
        }
    }
}

It just doesnt do anything. I tried it in new form, nothing.

Thank you in advance!

+1  A: 

The code is probably OK, it is drawing a ellipse as you are hoping for. However, after the Load event, there will be a PaintBackground event and a PaintEvent as the form is displayed. The PaintBackground will, by default, erase the contents of the control, effectively removing the ellipse you've just drawn.

Painting is a two stage process:

for each (region in set of regions that need updating)
{
  PaintBackground (region)
  Paint (region)
}

The window manager only redraws the parts of the control that require updating, if the contents of the control haven't changed or no user action has altered the control's visibility then no painting is done.

So, why do you want to draw the ellipse in the Load method? Usually, you only want to draw something when something needs to be drawn, and your form is told when something needs drawing in the PaintBackground and Paint events.

Are you worried about flickering? Or is it a speed issue? Ellipses are quick to draw. Flickering, however, is harder to fix. You need to create a bitmap, draw to the bitmap and blit the bitmap to the control during the Paint event. Also, make the PaintBackground event do nothing - no erasing the control, it's the erasing that causes the flicker.

Skizz

EDIT: An example, I'm using DevStudio 2005 here.

  1. Create a new C# winform application.
  2. In Form1.cs add the following:

    protected override void OnPaintBackground (PaintEventArgs e)
    {
      // do nothing! prevents flicker
    }
    
    
    protected override void OnPaint (PaintEventArgs e)
    {
      e.Graphics.FillRectangle (new SolidBrush (BackColor), e.ClipRectangle);
    
    
      Point
        mouse = PointToClient (MousePosition);
    
    
      e.Graphics.DrawEllipse (new Pen (ForeColor), new Rectangle (mouse.X - 20, mouse.Y - 10, 40, 20));
    }
    
    
    protected override void OnMouseMove (MouseEventArgs e)
    {
      base.OnMouseMove (e);
      Invalidate ();
    }
    
  3. Compile and run.

Skizz
And what if I need to redrawn always some special object using input parameters? My form is following the mouse.
Petr
To follow the mouse, process the mouse move event and invalidate the form, this will cause the control to redraw on mouse move events.
Skizz