A GDI+ newbie problem
-
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 description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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 /// /// The main entry point for the application. /// [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.Align
-
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 description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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 /// /// The main entry point for the application. /// [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.Align
A couple of things. First, the problem is not that the rectangle is being drawn again when the form is resized, it's that the old one is not being erased. To solve this use the
Graphics
object passed into the paint event handler (e.Graphics) instead of creating aGraphics
object. Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the lineResizeRedraw = true;
to your form's constructor. You can see for yourself the difference this makes. One last thing, since you're doing this painting in the form itself, you can override the OnPaint method rather than handling the paint event. Charlie if(!curlies){ return; } -
A couple of things. First, the problem is not that the rectangle is being drawn again when the form is resized, it's that the old one is not being erased. To solve this use the
Graphics
object passed into the paint event handler (e.Graphics) instead of creating aGraphics
object. Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the lineResizeRedraw = true;
to your form's constructor. You can see for yourself the difference this makes. One last thing, since you're doing this painting in the form itself, you can override the OnPaint method rather than handling the paint event. Charlie if(!curlies){ return; }