A GDI+ problem

C

Cem Louis

Hi,

I have a problem in GDI+, I put a code snippet (draws a rectangle with a
text inside) in the onpaint event handler of my simple form but when I
resize the form, the handler redraws my rectangle? Is there a way to
determine if there is a rectangle or some other thing on the form's drawing
area to not to redraw the rectangle? How can I solve this problem? (my code
is below...)

Thanks in advance...
Cem Louis

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{

// Obtain the Graphics object
Graphics g = this.CreateGraphics();

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
g.Dispose();
}

}
}
 
B

Bob Powell [MVP]

The problem here is that the background of the form is being painted,
followed by the graphics you've defined so it flickers.

Set the styles in the constructor to do double-buffering and when you paint,
use the graphics handed to you in the paint event arguments, not the one you
create with CreateGraphics. See code after my signature...


--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

---------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//enable double buffering
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{

// DO NOT!! Obtain the Graphics object like this
// Graphics g = this.CreateGraphics();

Graphics g = e.Graphics;

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
// g.Dispose(); Don't dispose of the graphics handed in to the
PaintEventArgs
}

}
}


Cem Louis said:
Hi,

I have a problem in GDI+, I put a code snippet (draws a rectangle with a
text inside) in the onpaint event handler of my simple form but when I
resize the form, the handler redraws my rectangle? Is there a way to
determine if there is a rectangle or some other thing on the form's drawing
area to not to redraw the rectangle? How can I solve this problem? (my code
is below...)

Thanks in advance...
Cem Louis

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{

// Obtain the Graphics object
Graphics g = this.CreateGraphics();

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
g.Dispose();
}

}
}
 
C

Cem Louis

Hi Bob,

Thank you for your reply but it didn't work as I want... Anyway I found a
property for Windows forms which is "ResizeRedraw" I put it in my form
constructor. Here is my new code below:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.ResizeRedraw = true; // The property which activates when the form
is resized

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{
/*
// Create a new Point object
Point pt = new Point(50, 50);
// Create a new point using Point.Empty
Point newPoint = Point.Empty;
// Set X and Y properties of Point
newPoint.X = 100;
newPoint.Y = 200;
// Create a Graphics object from the
// current form's handle
Graphics g = Graphics.FromHwnd(this.Handle);
// Create a new pen with color blue
// and width = 4
Pen pn = new Pen(Color.Blue, 4);
// Draw a line from point pt to
// new point
g.DrawLine(pn, pt, newPoint);
// Dispose of Pen and Graphics objects
pn.Dispose();
g.Dispose();
*/

// Obtain the Graphics object
Graphics g = this.CreateGraphics();

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
g.Dispose();

}

}
}

Bob Powell said:
The problem here is that the background of the form is being painted,
followed by the graphics you've defined so it flickers.

Set the styles in the constructor to do double-buffering and when you paint,
use the graphics handed to you in the paint event arguments, not the one you
create with CreateGraphics. See code after my signature...


--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

---------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//enable double buffering
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{

// DO NOT!! Obtain the Graphics object like this
// Graphics g = this.CreateGraphics();

Graphics g = e.Graphics;

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
// g.Dispose(); Don't dispose of the graphics handed in to the
PaintEventArgs
}

}
}


Cem Louis said:
Hi,

I have a problem in GDI+, I put a code snippet (draws a rectangle with a
text inside) in the onpaint event handler of my simple form but when I
resize the form, the handler redraws my rectangle? Is there a way to
determine if there is a rectangle or some other thing on the form's drawing
area to not to redraw the rectangle? How can I solve this problem? (my code
is below...)

Thanks in advance...
Cem Louis

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI_Test01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void form1_Paint(object sender, PaintEventArgs e)
{

// Obtain the Graphics object
Graphics g = this.CreateGraphics();

int rect_width = 110; int rect_height = 20;
Rectangle rect = new
Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;


// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect,
strFormat1);

verdanaFont.Dispose();
g.Dispose();
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top