Thread: Grapics question checkerboard

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    Grapics question checkerboard

    I am working on a checkerboard in a winform. I have a fixed size board drawing in the form but want to make the checkerboard rescale itself whenever the window is resized. I have a drawBoard function that I am calling in my Paint event. I think I am just missing something easy but I can only get it to work right when I use specific coordinates.

    Code:
    void Form1_Paint(object sender, PaintEventArgs e)
            {
                drawBoard(e.Graphics, this.ClientRectangle);
            }
    
    private void drawBoard(Graphics g, Rectangle rectangle)
            {
                bool dark = true;
                for (int i = 0; i < 8; i++)
                {
                    dark = !dark;
                    for (int j = 0; j < 8; j++)
                    {
                        dark = !dark;
    
                        Pen redPen = new Pen(Color.Red, 2);
                        SolidBrush brush;
                        if (dark)
                        {
                            brush = new SolidBrush(Color.DarkGray);
                        }
                        else
                        {
                            brush = new SolidBrush(Color.Red);
                        }
    
                        int x = 50 * i + 10;
                        int y = 50 * j + 10;
                        int width = 50;
                        int height = 50;
                         //Draw rectangle to screen.
                        g.DrawRectangle(redPen, x, y, width, height);
                        g.FillRectangle(brush, x, y, width, height);
                        //Invalidate (); // redraw the screen
                    }
                }
            }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What if you create an event handler for the form's Resize event?

    Code:
    void Form1_Resize(object sender, ResizeEventArgs e)  // or whatever it is...
    {
      Invalidate();  // Force the Paint event to trigger
    }
    To illustrate:
    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
    
                using (Pen pen = new Pen(Color.Red))
                    e.Graphics.DrawLine(pen, ClientRectangle.Left, ClientRectangle.Bottom, ClientRectangle.Right, ClientRectangle.Top);
            }
    
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
    
                Invalidate();  // Without this, it doesn't work correctly
            }
        }
    Last edited by itsme86; 04-07-2011 at 03:36 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  3. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM