您的位置:首页 > 其它

使用GDI+实现光束效果

2007-03-08 12:08 381 查看

使用GDI+实现光束效果

作者 jlgzw

//--------------------------------------------------------------------------------------------------------------
// RandomRound.cs @ 2007 by jl gzw
//--------------------------------------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace RandomRound
{
public class RandomRound : Form
{
//圆的大小
const int cxWidth = 50;
const int cyHeight = 50;
//点的数目
const int iPtNum = 360;
//圆移动的偏移量
int cxMove = 5;
int cyMove = 5;
//圆的坐标
int x, y;
//窗口的中心点
static Point CenterPt;

static void Main()
{
Application.Run(new RandomRound());
}
public RandomRound()
{
this.Text = "RandomRound";
this.BackColor = Color.Black;

CenterPt = new Point(ClientRectangle.Width / 2, ClientSize.Height / 2);

Timer timer = new Timer();
timer.Interval = 100;
timer.Tick += new EventHandler(TimerOnTick);
timer.Start();
}
protected override void OnResize(EventArgs e)
{
x = ClientSize.Width / 2;
y = ClientSize.Height / 2;
}
void TimerOnTick(object obj, EventArgs ea)
{
Random rand = new Random();

Graphics grfx = CreateGraphics();

grfx.Clear(Color.Black);

x += cxMove;
y += cyMove;

if((x + cxWidth >= ClientSize.Width) || (x <= 0))
cxMove = -cxMove;
if ((y + cyHeight >= ClientSize.Height) || (y <= 0))
cyMove = -cyMove;

Color color = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));

PointF[] aptf = new PointF[iPtNum];

for (int i = 0; i < iPtNum; i++)
{
double dAng = i * 2 * Math.PI / (iPtNum - 1);

aptf[i].X =x + (cxWidth - 1) / 2f * (1 + (float)Math.Cos(dAng));
aptf[i].Y =y + (cyHeight - 1) / 2f * (1 + (float)Math.Sin(dAng));

grfx.DrawLine(new Pen(color),CenterPt, aptf[i]);
}

grfx.FillPolygon(new SolidBrush(color), aptf);

grfx.Dispose();
}

}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: