C#繪製螞蟻線

來源:互聯網
上載者:User

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);
    }
}


 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。