www.xbdev.net
xbdev - software development
Friday March 29, 2024
home | contact | Support | Programming.. With C# and .Net...
>>
     
 

Programming..

With C# and .Net...

 


Drawing to the screen.

 

 

We need to update our screen when its covered or uncovered.... usually through a message being sent to our app, informinug us that our canvas areas is now dirty and we need to update it.

 

DownloadSourceCode

 

The demo isn't much...but the code is really simple to follow I think.  It clears the background to blue, then renders some simple graphics objects on our canvas... by canvas I mean our client rect.  Simple red square and a line from corner to corner, as you can see by the screen shot on the right.

 

 

There are two main parts to the code which you must not forget... first is to add a paint handling funciton - which will be called by windows whenever our window need painting.  For example if it gets covered by another window and then uncovered.  The next thing we need to do, is to tell windows which function will be handling our paint udateing....so we do this in our constructor using "this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);".

 

code: screen.cs

using System;

using System.Drawing;

using System.Windows.Forms;

 

class SimpleWindow : System.Windows.Forms.Form

{

 

      private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)

      {

            Rectangle r = this.ClientRectangle;

 

            System.Drawing.Graphics g = e.Graphics;

            g.FillRectangle(System.Drawing.Brushes.Blue, r);

 

            // Draw a line from top left to bottom right

            g.DrawLine(System.Drawing.Pens.Green, 0, 0, r.Right, r.Bottom);

 

            // Draw a small red square for colour :)

            g.FillRectangle(System.Drawing.Brushes.Red, 30, 30, 30, 30);

      }// End of OnPaint(..)

 

      // Window constructor

      SimpleWindow()

      {

            //** Important ** Important ** Important ** Important ** Important **

            // Don't forget to inform our window, which function will be handling

            // the repainting.

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);

      }

 

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}// End of SimpleWindow Class

 

 

The "PaintEventArgs e" member function that we are passed contains additional information, such as a rectangle to the area that needs repainting etc (e.ClipRectangle) as well as the Graphics context (e.Graphics).

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.