Graphics 使用一点点注意

Form_Load 事件下绘制的结果会被 paint 刷新掉.也就等于没有绘制一样.

Graphics g = this.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.Blue, 2), new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
g.DrawRectangle(new Pen(Color.Red, 2), new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
g.DrawRectangle(new Pen(Color.Yellow, 2), new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
g.DrawRectangle(new Pen(Brushes.Green, 2), new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));

  这段代码可以放在很多事件里执行.除了LOAD.

此外,今天使用的背景控件是 tableLayoutPanel1 但它没有触发 Enter 事件.最终用 tableLayoutPanel1_MouseEnter 事件解决.

全部代码如下 

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleCustorApplication
{
    public class Form1 : Form
    {
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 273);
            this.tableLayoutPanel1.TabIndex = 0;
            this.tableLayoutPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel1_Paint);
            this.tableLayoutPanel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.tableLayoutPanel1_MouseMove);
            this.tableLayoutPanel1.MouseEnter += new System.EventHandler(this.tableLayoutPanel1_MouseEnter);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            if (new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p))
                this.Cursor = Cursors.Cross;
            else if (new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p))
                this.Cursor = Cursors.Hand;
            else if (new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p))
                this.Cursor = Cursors.VSplit;
            else if (new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p))
                this.Cursor = Cursors.UpArrow;
            else
                if (new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p))
                    this.Cursor = Cursors.Default;
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(Brushes.Yellow, new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.FillRectangle(Brushes.Green, new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.FillRectangle(Brushes.Red, new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.FillRectangle(Brushes.Blue, new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));

        }

        private void tableLayoutPanel1_MouseEnter(object sender, EventArgs e)
        {
            Graphics g = tableLayoutPanel1.CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.Blue, 2), new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.DrawRectangle(new Pen(Color.Red, 2), new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.DrawRectangle(new Pen(Color.Yellow, 2), new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2));
            g.DrawRectangle(new Pen(Brushes.Green, 2), new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2));

        }
    }
}
原文地址:https://www.cnblogs.com/z5337/p/3315136.html