我用一个winform做了一个进度条,可是在弹出窗体的时候变成了一堆白色.之后想到用repaint,可是找不到,后来就用了Invalidate(); 但是还是没有效果.问题如何解决.具体情况如下:
     form1访问连接数据库时候修改一个bool值并弹出带进度条的窗体,之后再访问数据库完毕后另bool起变化从而关闭进度条所在的窗口.就是在这个过程中弹出窗口变成了一陀白色的矩行.

解决方案 »

  1.   

    用户控件代码:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;namespace SmoothProgressBar
    {
    /// <summary>
    /// UserControl1 的摘要说明。
    /// </summary>
    public class UserControl1 : System.Windows.Forms.UserControl
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public UserControl1()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// 
    int min = 0; // Minimum value for progress range
    int max = 100; // Maximum value for progress range
    int val = 0; // Current progress
    Color BarColor = Color.Blue; // Color of progress meter protected override void OnResize(EventArgs e)
    {
    // Invalidate the control to get a repaint.
    this.Invalidate();
    } protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    SolidBrush brush = new SolidBrush(BarColor);
    float percent = (float)(val - min) / (float)(max - min);
    Rectangle rect = this.ClientRectangle; // Calculate area for drawing the progress.
    rect.Width = (int)((float)rect.Width * percent); // Draw the progress meter.
    g.FillRectangle(brush, rect); // Draw a three-dimensional border around the control.
    Draw3DBorder(g); // Clean up.
    brush.Dispose();
    g.Dispose();
    } public int Minimum
    {
    get
    {
    return min;
    } set
    {
    // Prevent a negative value.
    if (value < 0)
    {
    min = 0;
    } // Make sure that the minimum value is never set higher than the maximum value.
    if (value > max)
    {
    min = value;
    min = value;
    } // Ensure value is still in range
    if (val < min)
    {
    val = min;
    } // Invalidate the control to get a repaint.
    this.Invalidate();
    }
    } public int Maximum
    {
    get
    {
    return max;
    } set
    {
    // Make sure that the maximum value is never set lower than the minimum value.
    if (value < min)
    {
    min = value;
    } max = value; // Make sure that value is still in range.
    if (val > max)
    {
    val = max;
    } // Invalidate the control to get a repaint.
    this.Invalidate();
    }
    } public int Value
    {
    get
    {
    return val;
    } set
    {
    int oldValue = val; // Make sure that the value does not stray outside the valid range.
    if (value < min)
    {
    val = min;
    }
    else if (value > max)
    {
    val = max;
    }
    else
    {
    val = value;
    } // Invalidate only the changed area.
    float percent; Rectangle newValueRect = this.ClientRectangle;
    Rectangle oldValueRect = this.ClientRectangle; // Use a new value to calculate the rectangle for progress.
    percent = (float)(val - min) / (float)(max - min);
    newValueRect.Width = (int)((float)newValueRect.Width * percent); // Use an old value to calculate the rectangle for progress.
    percent = (float)(oldValue - min) / (float)(max - min);
    oldValueRect.Width = (int)((float)oldValueRect.Width * percent); Rectangle updateRect = new Rectangle(); // Find only the part of the screen that must be updated.
    if (newValueRect.Width > oldValueRect.Width)
    {
    updateRect.X = oldValueRect.Size.Width;
    updateRect.Width = newValueRect.Width - oldValueRect.Width;
    }
    else
    {
    updateRect.X = newValueRect.Size.Width;
    updateRect.Width = oldValueRect.Width - newValueRect.Width;
    } updateRect.Height = this.Height; // Invalidate the intersection region only.
    this.Invalidate(updateRect);
    }
    } public Color ProgressBarColor
    {
    get
    {
    return BarColor;
    } set
    {
    BarColor = value; // Invalidate the control to get a repaint.
    this.Invalidate();
    }
    } private void Draw3DBorder(Graphics g)
    {
    int PenWidth = (int)Pens.White.Width; g.DrawLine(Pens.DarkGray,
    new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
    new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
    g.DrawLine(Pens.DarkGray,
    new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
    new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
    new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
    new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
    new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
    new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if( components != null )
    components.Dispose();
    }
    base.Dispose( disposing );
    } #region 组件设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // UserControl1
    // 
    this.BackColor = System.Drawing.Color.IndianRed;
    this.Name = "UserControl1";
    this.Size = new System.Drawing.Size(150, 40); }
    #endregion
    }
    }
      

  2.   

    控件使用:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using SmoothProgressBar;
    namespace WindowsApplication2
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Button button1;
    private SmoothProgressBar.UserControl1 smoothProgressBar1;
    private SmoothProgressBar.UserControl1 smoothProgressBar2;
    private System.ComponentModel.IContainer components; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.smoothProgressBar1 = new SmoothProgressBar.UserControl1();
    this.button1 = new System.Windows.Forms.Button();
    this.smoothProgressBar2 = new SmoothProgressBar.UserControl1();
    this.SuspendLayout();
    // 
    // timer1
    // 
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // smoothProgressBar1
    // 
    this.smoothProgressBar1.Location = new System.Drawing.Point(40, 72);
    this.smoothProgressBar1.Maximum = 100;
    this.smoothProgressBar1.Minimum = 0;
    this.smoothProgressBar1.Name = "smoothProgressBar1";
    this.smoothProgressBar1.ProgressBarColor = System.Drawing.Color.Blue;
    this.smoothProgressBar1.Size = new System.Drawing.Size(368, 24);
    this.smoothProgressBar1.TabIndex = 0;
    this.smoothProgressBar1.Value = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(144, 200);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // smoothProgressBar2
    // 
    this.smoothProgressBar2.Location = new System.Drawing.Point(48, 136);
    this.smoothProgressBar2.Maximum = 100;
    this.smoothProgressBar2.Minimum = 0;
    this.smoothProgressBar2.Name = "smoothProgressBar2";
    this.smoothProgressBar2.ProgressBarColor = System.Drawing.Color.Blue;
    this.smoothProgressBar2.Size = new System.Drawing.Size(368, 24);
    this.smoothProgressBar2.TabIndex = 2;
    this.smoothProgressBar2.Value = 0;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(496, 301);
    this.Controls.Add(this.smoothProgressBar2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.smoothProgressBar1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    if (this.smoothProgressBar1.Value > 0)
    {
    this.smoothProgressBar1.Value--;
    this.smoothProgressBar2.Value++;
    }
    else
    {
    this.timer1.Enabled = false;
    }
    } private void button1_Click(object sender, System.EventArgs e)
    {
    this.smoothProgressBar1.Value = 100;
    this.smoothProgressBar2.Value = 0; this.timer1.Interval = 1;
    this.timer1.Enabled = true; } private void Form1_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  3.   

    引用DLL的问题就不用说了吧,把代码复制过去就可以用了
      

  4.   

    zdq801104兄,我的那个进度条不时以各用于load的进度条,只使一格连接访问数据库的一种等待进度条,所以你说的那个似乎不怎么适合我,我要解决的问题是为什么我已经在弹出框中使用了Invalidate(); 但还是变成空白一片的问题。