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 = ;
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 使用一点点注意的更多相关文章
- DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记
今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...
- iOS 图形处理 Core Graphics Quartz2D 教程
Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的 绘图.变换.颜色管理.脱屏 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- java工具类之Graphics
利用重写paint()方法绘画出一个坐标轴: package huaxian; import java.awt.Color; import java.awt.FlowLayout; import ja ...
- 利用animation和text-shadow纯CSS实现loading点点点的效果
经常在网上看到loading状态时的点点点的动态效果,自己也用JS写了一个,思路是使用一个计数参数,然后在需要添加点的元素后面利用setInterval一个一个加点,当计数到3时,把点变为一个--写完 ...
- 解决C# WinForm Graphics绘制闪烁问题
不直接使用form的CreateGraphics创建Graphics进行绘制,可以先在Form上面放一个需要大小的PictureBox,再创建一个同大小的Bitmap,将这个Bitmap设置为Pict ...
- [译]Modern Core Graphics with Swift系列
第一篇 想象一下你已经完成了你的app并且运行的很好,但是界面看上去太土,你可以在PS里面画好多不同尺寸的自定义控件,Apple并没有4x的retina屏幕. 或者你已经未雨绸缪,在代码中使用Core ...
- 《3D Math Primer for Graphics and Game Development》读书笔记2
<3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...
- 《3D Math Primer for Graphics and Game Development》读书笔记1
<3D Math Primer for Graphics and Game Development>读书笔记1 本文是<3D Math Primer for Graphics and ...
随机推荐
- DbHelper简单的使用
using System; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApp3 { ...
- 【github】github的使用
一.上传本地代码 1.在github上新建一个repository(命名为英文) 2.打开cmd,进入上传代码所在目录 3.输入如下命令 第一步:git init --建仓第二步:git add * ...
- ST3 C程序自动补全
参考: http://www.cnblogs.com/heleifz/p/3404600.html http://www.cnblogs.com/By-ruoyu/p/4687196.html htt ...
- Hadoop_12_Hadoop 中的RPC框架演示
Hadoop中自己提供了一个RPC的框架.集群中各节点的通讯都使用了那个框架 1.服务端 1.1.业务接口:ClientNamenodeProtocol package cn.bigdata.hdfs ...
- windows下虚拟python环境
Windows虚拟环境 cd %HOMEDRIVE%%HOMEPATH%\Desktop python3 -m venv venv 环境变量修改脚本bat,把脚本放到%HOMEDRIVE%%HOM ...
- PAT Basic 1062 最简分数 (20 分)
一个分数一般写成两个整数相除的形式:/,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 / 和 /,要求你按从小到大的顺序列出它们之间分母为 K 的最简分 ...
- Ubuntu系统---以virtualenv方式安装Tensorflow-CPU
Ubuntu系统---以virtualenv方式安装Tensorflow-CPU 一.安装环境 环境:Ubuntu18.04+CPU+python2.7 本文介绍:如何在ubuntu上以virtual ...
- Web SQL与indexedDB
虽然在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数 ...
- Codeforces 1216E2 枚举位数+二分
两个二分 枚举位数 #include <bits/stdc++.h> #define MOD 1000000007 using namespace std; typedef long lo ...
- React 入门与实战-课时7 虚拟DOM的本质和目的
DOM树的概念: 一个网页呈现的过程: 1.浏览器请求服务器获取页面HTML代码 2.浏览器先在内存中,解析DOM结构,并在浏览器内存中,渲染出一颗DOM树: 3.浏览器把DOM树,呈现到页面上: R ...