C#绘制蚂蚁线

这篇博客介绍如何在C#中实现动态蚂蚁线效果。通过调用gdi32.dll库的LineDDA方法,结合GraphicsPath对象,创建了一个在按钮点击事件中运行的定时器。定时器在每个间隔内更新像素颜色,从而产生移动的点,形成蚂蚁线的视觉效果。代码示例详细展示了如何设置路径、调用绘图函数以及改变颜色来达到目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

delegate void LINEDDAPROC(int X, int Y, IntPtr lpData);
[DllImport("gdi32.dll")]
static extern int LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd,
    LINEDDAPROC lpLineFunc, IntPtr lpData);
 
private const byte PT_CLOSEFIGURE = 1;
private const byte PT_LINETO = 2;
private const byte PT_BEZIERTO = 4;
private const byte PT_MOVETO = 6;
 
[DllImport("gdi32.dll")]
static extern int SetPixel(IntPtr hdc, int X, int Y, int crColor);
 
GraphicsPath graphicsPath = new GraphicsPath();
private int counter = 0;
private IntPtr graphicsHandle = IntPtr.Zero;
//设计Zswang 2007-04-30 wjhu111#21cn.com 尊重作者,转贴请注明出处
 
private void button1_Click(object sender, EventArgs e)
{
    graphicsPath.ClearMarkers();
    graphicsPath.AddRectangle(new Rectangle(10, 10, 100, 100));
    timer1.Interval = 100;
    timer1.Enabled = true;
}
 
private void MovingDots(int X, int Y, IntPtr lpData)
{
    counter = (counter + 1) % 15;
    Color vColor;
    if (counter < 5)
        vColor = Color.White;
    else if (counter < 12)
        vColor = Color.Red;
    else vColor = Color.Blue;
    SetPixel(graphicsHandle, X, Y, vColor.R | vColor.G << 8 | vColor.B << 16);
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    graphicsHandle = Graphics.FromHwnd(Handle).GetHdc();
    for (int i = 0; i < graphicsPath.PathPoints.Length; i++)
    {
        if (graphicsPath.PathTypes[i] == (byte)(PT_CLOSEFIGURE | PT_LINETO))
        {
            for (int j = i; j >= 0; j--)
            {
                if (graphicsPath.PathTypes[j] == PT_MOVETO)
                {
                    LineDDA(
                        (int)graphicsPath.PathPoints[i].X,
                        (int)graphicsPath.PathPoints[i].Y,
                        (int)graphicsPath.PathPoints[j].X,
                        (int)graphicsPath.PathPoints[j].Y,
                        MovingDots, IntPtr.Zero);
                    break;
                }
            }
            continue;
        }
        if (i == graphicsPath.PathPoints.Length - 1)
            LineDDA(
                (int)graphicsPath.PathPoints[i].X,
                (int)graphicsPath.PathPoints[i].Y,
                (int)graphicsPath.PathPoints[0].X,
                (int)graphicsPath.PathPoints[0].Y,
                MovingDots, IntPtr.Zero);
        else
            LineDDA(
                (int)graphicsPath.PathPoints[i].X,
                (int)graphicsPath.PathPoints[i].Y,
                (int)graphicsPath.PathPoints[i + 1].X,
                (int)graphicsPath.PathPoints[i + 1].Y,
                MovingDots, IntPtr.Zero);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值