有动态效果,需要timer来实现,就是随着tick,改变液体高度(正如楼上所讲你可以画实心矩形),然后重画。

解决方案 »

  1.   

    方法一:Gdi+,Timer控件或线程。
    在窗体中画出矩形,通过Timer来触发事件不停的重绘窗体。方法和前面几位兄弟所说一样(最简单的)方法二:Label控件一个就可
    设置label的背景色,用h值来改变label的高。
      

  2.   

    ControlPaint.DrawReverseLine
    反复画,反复擦除
      

  3.   

    这是我写的一段已成功运行的代码供参考:
    (默认温度计能够计量100个单位,由于我把用来表示温度计的矩形设置成了300个像素的长度,所以3个像素表示一个单位。没有设置输入检查。)
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Thermometer
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components; static bool flag=false;
    static int moveCounter;
    static int position;
    static int counter;
    //private Point frameTopLeft=new Point(20,10);
    //private Size frameSize=new Size(300,30);
    private Rectangle frameRectangle=new Rectangle(20,10,30,310);
    private System.Windows.Forms.TextBox txtInput;
    //private Size dropSize=new Size(28,5);

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); SetStyle ( ControlStyles.DoubleBuffer , true ) ;
    SetStyle ( ControlStyles.AllPaintingInWmPaint , true ); counter=0;
    position=310;
    moveCounter=0; //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } protected override void OnPaint(PaintEventArgs e)
    {
    int top=newPosition();
    Graphics dc=e.Graphics;
    Pen bluePen=new Pen(Color.Blue,2);
    Pen redPen=new Pen(Color.Red,2);
    Pen yellowPen=new Pen(Color.Yellow,1);
    Brush stuff=Brushes.Brown;
    Font drawFont = new Font("Arial", 6); base.OnPaint (e); moveCounter=(Abstract(position-top));
    if(counter<moveCounter)
    {
    dc.DrawRectangle(bluePen,frameRectangle);
    for(int i=0;i<=100;i++)
    {
    if(i%5==0||i==0)
    {
    dc.DrawString((100-i).ToString(),drawFont,stuff,20-15,10+3*i-3);//字体上调3个像素。
    }
    dc.DrawLine(yellowPen,20-5,10+3*i,18,10+3*i);
    }
    dc.DrawRectangle(redPen,22,position-sign(position-top),26,3);
    dc.FillRectangle(stuff,22,position-sign(position-top),26,3);
    }
    else
    {
    dc.DrawRectangle(bluePen,frameRectangle);
    for(int i=0;i<=100;i++)
    {
    if(i%5==0||i==0)
    {
    dc.DrawString((100-i).ToString(),drawFont,stuff,20-15,10+3*i-3);//字体上调3个像素。
    }
    dc.DrawLine(yellowPen,20-5,10+3*i,18,10+3*i);
    }
    dc.DrawRectangle(redPen,22,position-sign(position-top),26,3);
    dc.FillRectangle(stuff,22,position-sign(position-top),26,3);
    position=top;
    flag=true;
    timer1.Stop();
    }
    counter+=1;

    } private int sign(int c)
    {
    if(c<0)
    return 0-counter;
    else
    return counter;
    }

    private int Abstract(int v)
    {
    if(v<0)
    return 0-v;
    return v;
    }

    private int newPosition()
    {

    int temp=Int32.Parse(txtInput.Text);
    int newpst=310-3*temp;
    return newpst; }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.txtInput = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.SuspendLayout();
    // 
    // txtInput
    // 
    this.txtInput.Location = new System.Drawing.Point(328, 176);
    this.txtInput.Name = "txtInput";
    this.txtInput.TabIndex = 0;
    this.txtInput.Text = "0";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(344, 264);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // timer1
    // 
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(544, 485);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.txtInput);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    try
    {
    while(flag==false)
    {
    Application.DoEvents();
    }
    }
    catch(Exception e)
    {
    MessageBox.Show(e.Message);
    }
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    this.Invalidate();
    } private void button1_Click(object sender, System.EventArgs e)
    {
    counter=0;
    moveCounter=0;
    flag=false;
    timer1.Interval=20;
    timer1.Start();

    }
    }
    }