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. Spring Boot启动流程分析

    引言 早在15年的时候就开始用spring boot进行开发了,然而一直就只是用用,并没有深入去了解spring boot是以什么原理怎样工作的,说来也惭愧.今天让我们从spring boot启动开始 ...

  2. android适配知识总结

    一.http适配 背景:API升级到28以后,不再支持明文的网络请求,只支持https请求.运行所报错误:java.net.UnknownServiceException: CLEARTEXT com ...

  3. Mysql(七):视图、触发器、事务、存储过程、函数

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  4. linux入门常用指令4.挂载数据盘

    挂载硬盘 #查看当前分区情况 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 dis ...

  5. [CodeForces 160A] Twins

    题目链接:http://codeforces.com/problemset/problem/160/A 注意排序是从大到小排,不要上来就sort导致从小到大排,细节水题. AC代码: #include ...

  6. idea中iml文件的问题

    idea中iml文件的问题 iml文件是idea组织工程的文件, 里面记录了各种记录模块, 文件夹以及依赖的信息, 显示如下: <?xml version="1.0" enc ...

  7. pip安装tesserocr时报错

    在Xubuntu上的python2虚拟环境中, 使用pip安装tesserocr时报错error: command 'x86_64-linux-gnu-gcc' failed with exit st ...

  8. CSS基础学习 20.CSS媒体查询

  9. 2018多校第十场 HDU 6430 (线段树合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430 题意:一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的 ...

  10. MyPlayer

    简单播放器 MyQueue.h #pragma once #include <Windows.h> #include <vector> #include <SDL.h&g ...