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个矩形了。一目了然。

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

  • 相关阅读:
    1.根据文件路径文件或文件夹图标获取 2.根据后缀获取系统图标
    多线程处理一个问题和多个问题
    C#汉字转换拼音技术详解
    委托小实例
    Winform 线程
    word 转换成 pdf
    几道算法题目
    双色球生成器
    c# Invoke和BeginInvoke
    C# 委托和事件2
  • 原文地址:https://www.cnblogs.com/chucklu/p/3969927.html
Copyright © 2011-2022 走看看