Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A GDI+ newbie problem

A GDI+ newbie problem

Scheduled Pinned Locked Moved C#
graphicsquestionwinformsdockerhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • C cemlouis

      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

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      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 a Graphics object. Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the line ResizeRedraw = 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; }

      C 1 Reply Last reply
      0
      • C Charlie Williams

        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 a Graphics object. Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the line ResizeRedraw = 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; }

        C Offline
        C Offline
        cemlouis
        wrote on last edited by
        #3

        Hi Charlie, Thank you for your reply, I didn't know the ResizeRedraw property before. Regards, Cem Louis

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups