我仍然可以';t清除我在OnPaint事件内的控件上绘制的矩形,为什么我可以';y清除它

本文关键字:清除 绘制 我可以 为什么 事件 OnPaint 控件 | 更新日期: 2023-09-27 18:30:10

我所说的清除是指将控件重新绘制或着色回原始控件。这是工作代码:

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);
            }
        }
    }
}

我现在看到,如果没有断点,当程序在两个文本框中键入文本后运行时,我最小化form1,然后调整form1的大小,它确实会清除矩形。

奇怪的是,它似乎只有当我最小化并调整窗体1的大小时才会生效。

在TextChanged事件中,我尝试添加:txtHost.Invalidate();但这无济于事。清除矩形的唯一方法是最小化并重新调整form1的大小。

或者添加这个。Invalidate();成功了。

我仍然可以';t清除我在OnPaint事件内的控件上绘制的矩形,为什么我可以';y清除它

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

但是可以重新绘制所有表单吗?

当然,但这不是很好的表现,因为你正在重新绘制不一定需要重新绘制的区域。Invalidate()应该有一个接受矩形参数的版本。使用它只会使要更新的区域无效。