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 ...
随机推荐
- Java安全停止线程方法
1. 早期Java提供java.lang.Thread类型包含了一些列的方法 start(), stop(), stop(Throwable) and suspend(), destroy() and ...
- 简单的flask对象
简单的flask对象 # coding:utf-8 # 导入Flask类 from flask import Flask #Flask类接收一个参数__name__ app = Flask(__nam ...
- Ubuntu安装libssl-dev失败(依靠aptitude管理降级软件)并记录dpkg展示安装软件列表
Ubuntu 12.04LTS下直接安装 libssl-dev 失败 提示错误: $ sudo apt-get install libssl-dev Reading package lists... ...
- Python 列出 windows 安装的软件
Python 列出 windows 安装的软件 参考链接:https://stackoverflow.com/questions/802499/how-can-i-enumerate-list-all ...
- 编辑器 --- Visual Studio Code 英文界面转换成中文(简体)
打开编辑器 同时按下Ctrl+Shift+P打开命令面板: 之后输入"config"筛选可用命令表,最后选择配置语言命令进行选择或安装插件
- VUE:Select2
<template> <div> <ul class="skill"> <li v-for='item of list' v-on:cli ...
- PHP mysqli_fetch_field() 函数
返回结果集中下一字段(列),然后输出每个字段名称.表格和最大长度: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect ...
- Python 爬取喜马拉雅音频
一.分析音频下载相关链接地址 1. 分析专辑音频列表页面 在 PC端用 Chrome 浏览器中打开 喜马拉雅 网站,打开 Chrome开发者工具,随意打开一个音频专辑页面,Chrome开发者工具中 ...
- 如何使用PHP排序key为字母+数字的数组
你还在为如何使用PHP排序字母+数字的数组而烦恼吗? 今天有个小伙伴在群里问: 如何将一个key为字母+数字的数组按升序排序呢? 举个例子: $test = [ 'n1' => 22423, ' ...
- 使用Camtasia 9 录制屏幕软件
Camtasia 9 录制屏幕软件,并且有丰富的专业剪辑功能.