我仍然无法清除我在OnPaint事件中的控件上绘制的矩形为什么我可以清除它?

时间:2014-11-11 18:02:04

标签: c# .net winforms

通过清楚我的意思是重新绘制或绘制或着色控件回到它的原始。 这是工作代码:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace FTP_ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            txtHost.TextChanged += anyTextBox_TextChanged;
            txtUploadFile.TextChanged += anyTextBox_TextChanged;
            txtDir.TextChanged += anyTextBox_TextChanged;

            anyTextBox_TextChanged(null, null);

            if ((txtHost.Text == "") || txtUploadFile.Text == "")
            {
                btnUpload.Enabled = false;
            }

            if (txtDir.Text == "")
            {
                checkBox1.Enabled = false;
            }
        }

        private void anyTextBox_TextChanged(object sender, EventArgs e)
        {
            btnUpload.Enabled = txtHost.TextLength > 0 && txtUploadFile.TextLength > 0;
            checkBox1.Enabled = txtDir.TextLength > 0;
            this.Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
                this.txtUploadFile.Text = this.openFileDialog1.FileName;
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if(this.ftpProgress1.IsBusy)
            {
                this.ftpProgress1.CancelAsync();
                this.btnUpload.Text = "Upload";
            }
            else
            {
                FtpSettings f = new FtpSettings();
                f.Host = this.txtHost.Text;
                f.Username = this.txtUsername.Text;
                f.Password = this.txtPassword.Text;
                f.TargetFolder = this.txtDir.Text;
                f.SourceFile = this.txtUploadFile.Text;
                f.Passive = this.chkPassive.Checked;
                try
                {
                    f.Port = Int32.Parse(this.txtPort.Text);
                }
                catch { }
                this.toolStripProgressBar1.Visible = true;
                this.ftpProgress1.RunWorkerAsync(f);
                this.btnUpload.Text = "Cancel";
            }
        }

        private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.toolStripStatusLabel1.Text = e.UserState.ToString();   // the message will be something like: 45 Kb / 102.12 Mb
            this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);      
        }

        private void ftpProgress1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error != null)
                MessageBox.Show(e.Error.ToString(), "FTP error");
            else if(e.Cancelled)
                this.toolStripStatusLabel1.Text = "Upload Cancelled";
            else
                this.toolStripStatusLabel1.Text = "Upload Complete";
            this.btnUpload.Text = "Upload";
            this.toolStripProgressBar1.Visible = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Pen penBorder;
            if (txtHost.TextLength <= 0)
            {
                penBorder = new Pen(Color.Red, 3);
                e.Graphics.DrawRectangle(penBorder, txtHost.Location.X, txtHost.Location.Y, txtHost.Width - 1, txtHost.Height - 1);
            }
            if (txtUploadFile.TextLength <= 0)
            {
                penBorder = new Pen(Color.Red, 3);
                e.Graphics.DrawRectangle(penBorder, txtUploadFile.Location.X, txtUploadFile.Location.Y, txtUploadFile.Width - 1, txtUploadFile.Height - 1);
            }
        }
    }
}

我现在看到,没有断点,如果我在两个textBox中的类型化文本之后运行程序时最小化form1,然后调整form1的大小,它会清除矩形。

奇怪的是,只有当我最小化并调整form1的大小时,它才会生效。

在TextChanged事件中,我试图添加:txtHost.Invalidate();但它没有帮助。 矩形变得清晰的唯一方法是,如果我最小化并重新调整form1。

或者添加this.Invalidate();做了伎俩。

1 个答案:

答案 0 :(得分:1)

只有在需要更新窗口时才会调用

OnPaint()。这是关于Windows如何工作的基本原则。如果您现在需要更新窗口,那么,是的,您需要使窗口无效,以便调用OnPaint()

  

但重绘所有表格是否可以?

当然,但由于您正在重绘不一定需要重绘的区域,因此效果不是很好。 Invalidate()应该有一个接受矩形参数的版本。使用它仅使您想要更新的区域无效。