重写OnPaint事件对窗体重绘 实例1

  public class WindowOne:Form
    {
        public WindowOne()
        {
            InitializeCompnent();
        }
        public void InitializeCompnent()
        {
            this.Width = 200;
            this.Height = 200;
        }
        //冲毁window 方法
        private void ReDraw(Graphics g)
        {
            Random ran = new Random();

            //Color ranColor = Color.FromArgb(ran.Next(256), ran.Next(256)
            //    , ran.Next(256), ran.Next(256));

            Color ranColor = Color.FromArgb(100, ran.Next(256)
                , ran.Next(256), ran.Next(256));
            g.Clear(ranColor);
        }
        /// <summary>
        /// 窗体绘制 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            ReDraw(e.Graphics);
        }
        /// <summary>
        /// 窗体大小 重置
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            //强制重新绘制
            this.Invalidate();
        }

    }

显示结果:

      

posted @ 2014-09-07 17:38  天马3798  阅读(718)  评论(0编辑  收藏  举报