WinForm画网格并填充颜色
因为研究CodeCombat上的最后一题,自己尝试分解题目,然后想到需要画网格,还有最优化的方法
源代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawGrid
{
public partial class Form1 : Form
{
int multiple = ;
int width = ;
int height = ;
int w = ;
int h = ;
Pen bluePen = new Pen(Color.Black); public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine("Form长:{0},宽:{1}", this.Width, this.Height);
this.Multiple();
DrawGrid(width, height, w, h, e); List<Rectangle> l = new List<Rectangle>();
Rectangle r;
r = new Rectangle(, w, * w, h); l.Add(r);
r = new Rectangle( * w, , * w, h); l.Add(r);
r = new Rectangle( * w, * w, * w, h); l.Add(r);
foreach (Rectangle r1 in l)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Black), r1);
}
} /// <summary>
/// 将比例放大
/// </summary>
private void Multiple()
{
width = width * multiple;
height = height * multiple;
w = w * multiple;
h = h * multiple;
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("X={0},Y={1}", e.X, e.Y);
} /// <summary>
/// 画网格
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="w"></param>
/// <param name="h"></param>
private void DrawGrid(int width, int height, int w, int h, PaintEventArgs e)
{
Point p1 = new Point();
Point p2 = new Point(); p1.X = ; p2.X = width;
for (int y = ; y <= height; y = y + h)
{
p1.Y = y; p2.Y = y;
DrawLine(p1, p2, e);
}
p1.Y = ; p2.Y = height;
for (int x = ; x <= width; x = x + w)
{
p1.X = x; p2.X = x;
DrawLine(p1, p2, e);
}
} /// <summary>
/// 画直线
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
private void DrawLine(Point p1, Point p2, PaintEventArgs e)
{
e.Graphics.DrawLine(bluePen, p1, p2);
}
}
}

自己的测试图片如上图,是一个5*4的网格;
有三个地方已经被填充了。
剩下空白的地方,需可以用矩形填充。
考虑使用最少的矩形填充,应该就是4个矩形了。一目了然。
不过,如果考虑用程序实现的话,就复杂了。以后再尝试
WinForm画网格并填充颜色的更多相关文章
- Winform中设置ZedGraph的颜色填充使用Fill
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- HTML5填充颜色的fillStyle测试
效果:http://hovertree.com/texiao/html5/canvas/1/ 代码: <html> <head> <meta http-equiv=&qu ...
- winform设置button的边框颜色,或取消边框颜色,不显示边框
// winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现. 一.设置按钮本身属性 buttonBubufx.FlatStyle = Fl ...
- excel如何设置输入数字后单元格自动填充颜色
在使用excel的过程中,有时需要在输入数字时,突出显示这些单元格,突出显示可以用有填充颜色的单元格来表示.为了实现这样的效果,需要借助excel的条件格式. 工具/原料 电脑 Excel 2010 ...
- Android 绘制一个Loading动画__向图片中缓慢填充颜色,从而形成动画效果
需求:制作一个加载动画,向一个不规则图片图形中从从下到上依次填充颜色,形成动画效果. 效果如下: 代码如下: LoadingAnimatorView.java package cn.yw.li ...
- VBA对指定单元格填充颜色并且赋值
使用VBA对指定的单元格赋值并填充颜色 ====================================================== 代码区域 ==================== ...
- 怎样将Excel包含某字符的单元格填充颜色
在处理数据的时候,xmyanke想将Excel中包含某字符的单元格填充蓝色,比较容易看清,弄了好一阵子都没完成,最后试用条件格式处理了一下,终于实现了. 比如要将A1到A12区间包含数字1的单元格填充 ...
- Excel的单元格设置下拉选项并填充颜色
如何在Excel的单元格中加入下拉选项 方法/步骤 第一步:打开excel文档,选中需加入下拉选项的单元格. 第二步:点击菜单中的“数据”->“数据有效性”->“数据 ...
- C#中画三角形和填充三角形的简单实现
C#中画三角形和填充三角形的简单实现: private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graph ...
随机推荐
- ext3日志模式
ext3日志模式 http://blog.sina.com.cn/s/blog_5d4ab4b40100dosx.html ext3支持多种日志模式 ext3 是ext2文件系统的高一级版本,完全兼容 ...
- js-jquery-SweetAlert【二】配置方法
一.配置 Argument Default value 含义 Description title null (required) 模态对话框的标题.它可以在参数对象的title参数中设置,也可以在 ...
- requesMapping注解
java类 package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; impor ...
- cookie和session的使用
http://www.cnblogs.com/linguoguo/p/5106618.html 1:在控制器中添加session和cookie @RequestMapping("/getin ...
- ev3_basic——HITCON CTF 2018
[MISC] EN-US This challenge provides a jpg file and a pklg file. The jpg is shown a part of string o ...
- C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法
对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还 ...
- Redis的设计与实现——字典
参考博客 绝大多数语言中的字典底层实现基本上都是哈希表.哈希表中用 “负载因子” 来衡量哈希表的 空/满 程度.为了让负载因子在一定的合理范围之内,提高查询的性能,一般的做法是让哈希表扩容,然后reh ...
- 【安装vsftpd】安装vsftpd工具步骤
1 安装vsftpd组件 [root@bogon ~]# yum -y install vsftpd 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. 2 添 ...
- JQ 给textarea赋值
<textarea id='t1'></textarea> 下面是 jq赋值的三种方式 $("#t1").text("AAA"); $( ...
- C++中的访问权限
C++中类的成员的权限: private:只能由该类的成员函数,友元函数访问,不能被该类的对象访问. protected:除了private外还能被子类的函数访问,同样不能被该类的对象访问. publ ...