如何画一个类似隧道的效果
本文关键字:隧道 何画一 | 更新日期: 2023-09-27 18:14:57
我知道如何在GDI中绘制东西,但我发现很难确定如何绘制实际形状,而不仅仅是正方形和圆形。
我试着画一个隧道,只是画了一大堆相互重叠的正方形或矩形,但我不知道如何正确地定位它们。
我怎么画这种效果?
您可以尝试这样做。一旦你了解了如何使用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();
}
}
}