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 = 20;
        int width = 20;
        int height = 16;
        int w = 4;
        int h = 4;
        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(0, w, 2 * w, h); l.Add(r);
            r = new Rectangle(3 * w, 0, 2 * w, h); l.Add(r);
            r = new Rectangle(2 * w, 3 * w, 2 * 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 = 0; p2.X = width;
            for (int y = 0; y <= height; y = y + h)
            {
                p1.Y = y; p2.Y = y;
                DrawLine(p1, p2, e);
            }
            p1.Y = 0; p2.Y = height;
            for (int x = 0; 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个矩形了。一目了然。

不过,如果考虑用程序实现的话,就复杂了。以后再尝试

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值