zoukankan      html  css  js  c++  java
  • 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个矩形了。一目了然。

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

  • 相关阅读:
    鼠标滑过图片显示放大镜效果
    如何点击iframe跳转以及允许点击全屏展示
    百度echarts饼图百分比的计算规则---最大余额法
    移动端点击出现蓝色背景框&pc端覆盖chrome浏览器input本身的背景颜色
    未解决 --- gorde-map移动端 样式为圆角移动过程中不生效
    Vue -- 数据更新echarts表格不更新问题
    Vue -- table多表头,在表头中添加按钮
    responsive --- a:hover伪类在ios移动端浏览器内无效的解决方法
    编辑器 --- Visual Studio Code mac window 常用快捷键
    mysql 知识整理
  • 原文地址:https://www.cnblogs.com/chucklu/p/3969927.html
Copyright © 2011-2022 走看看