如何绘制类似隧道的效果?

时间:2015-09-29 12:44:42

标签: c# .net winforms gdi+

我知道如何在GDI中绘制内容,但我发现很难确定如何绘制不仅仅是正方形和圆形的实际形状。

我试图绘制一个隧道,只是绘制了一大堆正方形或矩形相互重叠,但我无法弄清楚如何恰当地定位。

我该如何画出这种效果?

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。一旦你理解了如何使用GDI绘制的基础知识,你就应该能够很容易地做到这一点。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Autodraw
{
    public partial class Form1 : Form
    {
        private bool canDraw;

        public class NPanel : Panel
        {
            protected override bool DoubleBuffered
            {
                get
                {
                    return base.DoubleBuffered;
                }

                set
                {
                    base.DoubleBuffered = true;
                }
            }
        }

        public NPanel nPanel = new NPanel();

        public Form1()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            panel = nPanel;
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {
            if (canDraw)
            {
                for (int r = 0; r <= 255; r++)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(r, 0, 0), 1), r, r, r, r);
                }

                for (int r = 0; r <= 255; r++)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, r, 0), 1), r, r, r, r);
                }

                for (int r = 0; r <= 255; r++)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, r), 1), r, r, r, r);
                }
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)Keys.Escape)
            {
                timer.Stop();
                canDraw = false;
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            canDraw = true;
            timer.Start();
        }
    }
}