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 = ;
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(, );
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = ;
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(, );
this.tableLayoutPanel1.TabIndex = ;
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(, );
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(, , ClientRectangle.Width / , ClientRectangle.Height / ).Contains(p))
this.Cursor = Cursors.Cross;
else if (new Rectangle(, ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / ).Contains(p))
this.Cursor = Cursors.Hand;
else if (new Rectangle(ClientRectangle.Width / , , ClientRectangle.Width / , ClientRectangle.Height / ).Contains(p))
this.Cursor = Cursors.VSplit;
else if (new Rectangle(ClientRectangle.Width / , ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / ).Contains(p))
this.Cursor = Cursors.UpArrow;
else
if (new Rectangle(, , ClientRectangle.Width / , ClientRectangle.Height / ).Contains(p))
this.Cursor = Cursors.Default;
} private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Yellow, new Rectangle(, , ClientRectangle.Width / , ClientRectangle.Height / ));
g.FillRectangle(Brushes.Green, new Rectangle(, ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / ));
g.FillRectangle(Brushes.Red, new Rectangle(ClientRectangle.Width / , , ClientRectangle.Width / , ClientRectangle.Height / ));
g.FillRectangle(Brushes.Blue, new Rectangle(ClientRectangle.Width / , ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / )); } private void tableLayoutPanel1_MouseEnter(object sender, EventArgs e)
{
Graphics g = tableLayoutPanel1.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.Blue, ), new Rectangle(, , ClientRectangle.Width / , ClientRectangle.Height / ));
g.DrawRectangle(new Pen(Color.Red, ), new Rectangle(, ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / ));
g.DrawRectangle(new Pen(Color.Yellow, ), new Rectangle(ClientRectangle.Width / , , ClientRectangle.Width / , ClientRectangle.Height / ));
g.DrawRectangle(new Pen(Brushes.Green, ), new Rectangle(ClientRectangle.Width / , ClientRectangle.Height / , ClientRectangle.Width / , ClientRectangle.Height / )); }
}
}

Graphics 使用一点点注意的更多相关文章

  1. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

  2. iOS 图形处理 Core Graphics Quartz2D 教程

    Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的 绘图.变换.颜色管理.脱屏 ...

  3. Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译

    本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  4. java工具类之Graphics

    利用重写paint()方法绘画出一个坐标轴: package huaxian; import java.awt.Color; import java.awt.FlowLayout; import ja ...

  5. 利用animation和text-shadow纯CSS实现loading点点点的效果

    经常在网上看到loading状态时的点点点的动态效果,自己也用JS写了一个,思路是使用一个计数参数,然后在需要添加点的元素后面利用setInterval一个一个加点,当计数到3时,把点变为一个--写完 ...

  6. 解决C# WinForm Graphics绘制闪烁问题

    不直接使用form的CreateGraphics创建Graphics进行绘制,可以先在Form上面放一个需要大小的PictureBox,再创建一个同大小的Bitmap,将这个Bitmap设置为Pict ...

  7. [译]Modern Core Graphics with Swift系列

    第一篇 想象一下你已经完成了你的app并且运行的很好,但是界面看上去太土,你可以在PS里面画好多不同尺寸的自定义控件,Apple并没有4x的retina屏幕. 或者你已经未雨绸缪,在代码中使用Core ...

  8. 《3D Math Primer for Graphics and Game Development》读书笔记2

    <3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...

  9. 《3D Math Primer for Graphics and Game Development》读书笔记1

    <3D Math Primer for Graphics and Game Development>读书笔记1 本文是<3D Math Primer for Graphics and ...

随机推荐

  1. SQL使用 dateadd添加使天数加x

    ,Receivedate)), ) --第一个参数 表示增加什么(day ) --第二个参数表示增加多少( int ) --第三个参数表示那个字段  (Receivedate 字段属性) conver ...

  2. CentOS7 解决不能切换中英文输入法的问题

    1. 运行 im-chooser(如果没有要先安装) $ im-chooser 2. 在打开的窗口选择 iBus,然后 Logout 再 Login, 输入法即可切换.

  3. Springboot静态资源映射 “/” 引发的血案

    因为少写一个 / 浪费已个下午的时间,

  4. Java中wait()与notify()理解

    通常,多线程之间需要协调工作.例如,浏览器的一个显示图片的线程displayThread想要执行显示图片的任务,必须等待下载线程 downloadThread将该图片下载完毕.如果图片还没有下载完,d ...

  5. golang打包和部署到centos7

    一.环境说明:VS code 二.编译: set GOOS=linux set GOARCH=amd64 go build -o "packageName"   三.发布 上传到服 ...

  6. Spark任务调度初识

    前置知识 spark任务模型 job:action的调用,触发了DAG的提交和整个job的执行. stage:stage是由是否shuffle来划分,如果发生shuffle,则分为2个stage. t ...

  7. IDEA配置和插件

    1.相关配置 设置字体和大小 2.插件 maven helper 解决maven包冲突的问题 打开pom文件,并可以切换tab,简单使用,如下图 RestfulToolkit RestfulToolk ...

  8. .net core Areas区域

    //使用MVC app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:ex ...

  9. 013_STM32程序移植之_DS18B20

    013_STM32程序移植之_DS18B20 1. 测试环境:STM32C8T6 2. 测试模块:DS18B20模块 3. 测试接口: 1. DS18B20模块接口: DS18B20引脚        ...

  10. ajax+批量下载文件

    用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个php文件夹上传下载的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助.此控件PC全平台支持包括mac,li ...