4 Replies - 375 Views - Last Post: 24 October 2019 - 03:41 PM Rate Topic: -----

#1 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Why does the Form_Paint event run continuously and only draw once ?

Posted 22 October 2019 - 07:11 PM

I have the following code, I put a label in Form_Paint to track the number jump, I see the label jumping continuously but e.Graphics.DrawString(...) only looks once, does anyone know why ?

public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            //progressBar1.Visible = false;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;

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

        }
        int iNum = 0;        
        private void btnDrawText_Click(object sender, EventArgs e) //PaintEventArgs e
        {            
            progressBar1.Value = 0;
            progressBar1.Visible = true;
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;            
                       
        }
        private void ShadowedTextPaint(PaintEventArgs e, int num)
        {
            using (Font font1 = new Font("Times New Roman", 250, FontStyle.Bold, GraphicsUnit.Pixel))
            {
                PointF pointF1 = new PointF(310, 270);
                e.Graphics.DrawString(num.ToString(), font1, Brushes.LightGreen, pointF1);                
                lblNum.Text = num.ToString();
            }            
            
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            ShadowedTextPaint(e, iNum);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {            
            if (progressBar1.Value < 100)
            {                
                Random rd = new Random();
                iNum = rd.Next(0, 999);                
                progressBar1.Value++;
            }
            else
            {                
                this.timer1.Enabled = false;
                progressBar1.Visible = false;                
            }
            
        }
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Why does the Form_Paint event run continuously and only draw once ?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,220
  • Joined: 05-May 12

Re: Why does the Form_Paint event run continuously and only draw once ?

Posted 22 October 2019 - 07:56 PM

Windows minimizes the areas that needs to be repainted. When the WM_PAINT event is sent, only the invalidated area will be repainted. Since you are not invalidating the area that you affecting using DrawString(), it is clipped from the repaint region. The label on the other hand, will mark its client area as invalid, and so it will get repainted as some point when the WM_PAINT gets sent to the static text control backing the label.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,220
  • Joined: 05-May 12

Re: Why does the Form_Paint event run continuously and only draw once ?

Posted 22 October 2019 - 08:07 PM

More reading about how Windows paints and draws: Painting and Drawing

Even thought .NET Core 3.0 has given WinForms an reprieve from end-of-life, I still highly recommend moving away from WinForms and going towards WPF or Xamarin. To do WinForms well, you need to know the Win32 API well. That means you'll need crack open your copy of Petzold's Programming Windows 5th Edition which will teach you how and why Windows User and GDI works the way it does, along with its quirks. Then, since Progamming Windows is pretty dated, you need to supplement your learning by reading Richter's various books about how Windows NT works to learn more about how the Windows Kernel interacts with the Windows Shell, User, and GDI. Then next you'll need to immerse yourself in MSDN to learn everything else that has been update since Petzold's and Richter's books.

WPF and Xamarin abstracts all of that away from you. It provides a much more consistent and easy to understand model of how rendering works.
Was This Post Helpful? 0
  • +
  • -

#4 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Why does the Form_Paint event run continuously and only draw once ?

Posted 23 October 2019 - 09:11 PM

Hi Skydiver !
The problem you have told me to switch to another programming language, this is something I have already thought about. I have learned the language of xamarin to write applications for mobile phones because xamarin language writes an application that can be used for both Android and iphone phones, but the reality is different, the first is to install the language. This programming language has to be configured strongly using VS2015 or higher, documents for this language are very rare, I have logged into xamarin forums to ask sample code about application writing examples for phones not available, ask Xamarin Code Library also has no sample code to learn and learn. I need a language with lots of documents and examples so I will choose winform to perform my program for the time being, while the WPF language I have not learned, I will learn the WPF language in the future. Do you have WPF, Xamarin language code libraries and documents ? especially the Xamarin language I am very interested in.

This post has been edited by dongtrien: 23 October 2019 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,220
  • Joined: 05-May 12

Re: Why does the Form_Paint event run continuously and only draw once ?

Posted 24 October 2019 - 03:41 PM

I lean towards WPF since I don't really need mobile app development, or UWP development. I found the MSDN WPF documents excellent for getting started in terms of understanding what does what, but I found that Josh Smith's WPF books and blog posts are excellent way to learn the how and why you should do things a particular way. My little experience with transitioning over into Xamarin was pretty smooth because the concepts are the same.

Personally, the hardest transition is going from WinForms to WPF. They are completely different animals. It's almost equivalent to going from console programming to going to event driven programming (be it Windows, Mac, XWindows, etc.); or going from GUI event driven programming to stateless client-server web programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1