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. 使用ABAP批量下载Markdown源文件里的图片到本地

    执行我github里的这个report: 选中一段markdown文档,ctrl C: 然后直接执行report: 执行完毕: 所有文件都下载到本地文件夹: 这个report使用到的工具类:zcl_c ...

  2. weui 注意事项

    个人随笔日记,还请小心采纳为甚 手机移动web开发必须要做的两点: 1.body中加上ontouchstart,即<body ontouchstart>...</body>: ...

  3. HTML5常用的语义化标签

    快速查询 article | aside | nav | section | header | footer 架构预览 nav 定义导航链接的部分 在页脚显示一个站点的导航链接,如首页.服务信息页面. ...

  4. kotlin变量基础

    kotlin变量与输出 这里看一下在Kotlin中的变量是如何定义的? 编译运行: 对比下我们常用的JAVA语言,定义上的区别: 1.Kotlin变量是统一以var开头,而不区分具体类型,而Java在 ...

  5. Python: sqlite3模块

    sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块 SQLite 是一个C语言库,它可以提供一种轻量级的基于磁盘的数据库,这种数据库不需要独立的服务器进程,也允许需要使用一种 ...

  6. mysql基础_数据类型

    1.数字 (1)tinyint(小整数值) 范围:有符号(-128,127) 无符号(0.255) (2)int(大整数值) 范围:有符号  (-2 147 483 648,2 147 483 647 ...

  7. 基于IAP的STM32程序更新技术

    引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...

  8. RecursiveTask和RecursiveAction的使用 以及java 8 并行流和顺序流(转)

    什么是Fork/Join框架        Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 我 ...

  9. .Net利用cwbx.dll call AS400 program得到数据

    calling as400 programs from c# http://forums.asp.net/t/1817332.aspx?calling+as400+programs+from+c+ H ...

  10. 关于JPA动态SQL查询

    1.继承接口 2.下面是简单的操作---自己写过的,下次再整理一下 public Page<IdddsMember> list(MemberSearch search, Pageable ...