程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#繪制螞蟻線

C#繪制螞蟻線

編輯:關於C語言

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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved