Doing your own drawing PDF Print E-mail

The Paint event of a control is called everytimes the control needs to be redrawn. We can override the OnPaint method of the form class to do our own painting. The simplest such program is probably similar to:

  1.  
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. class OnPaintOverride : Form {
  6. public static void Main() {
  7. Application.Run( new OnPaintOverride() );
  8. }
  9. protected override void OnPaint( PaintEventArgs pea ) {
  10. pea.Graphics.DrawString( "Hello World", new Font( "Arial", 12 ), new SolidBrush( Color.Black ), 1, 1 );
  11. }
  12. }
 

We need to add an extra namespace System.Drawing to access the classes related to painting a control.

The OnPaint method expect a PaintEventArgs argument, and with this argument we can access its Graphics property. The Graphics object referenced by the argument has a DrawString method which we use to write "Hello World". The other arguments to DrawString are the font and brush objects needed to draw the string and its position.




Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
< Prev   Next >
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design