S
H
A
R
E

Thursday, April 28, 2011

CSharp Rounded Blinking Box in Console


Using VS2008 and written with CSharp Language, The rounded box will be blinking, use a simple timer handler you can try It, and this is the code:
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Kotak_Dekorasi k = new Kotak_Dekorasi();
            k.New(1, 1, 78, 5, 200);
           
        re:
           // j.Jalan();
            k.Jalan();
            Console.ReadKey();
            k.Berhenti();
            //j.Berhenti();
            Console.ReadKey();
            goto re;
        }
    }
    class Kotak_Dekorasi
    {
        TimerCallback TmH;
        public Timer tm;
        static char c1 = '*', c2 = ' ';
        static int x1, y1, x2, y2, d;
          public void New(int xx1, int yy1, int xx2, int yy2, int delay)
        {
            x1 = xx1; x2 = xx2; y1 = yy1; y2 = yy2; d = delay;
        }
          public void Jalan()
          {
              TmH = new TimerCallback(kedip);
              tm = new Timer(TmH, 0, 0, 300);

          }
          public void Berhenti()
          {
              tm.Dispose();
          }
        static void kedip(object kondisi) //TImer Handler bro..,
        {
            int i,j;
             Console.ForegroundColor = ConsoleColor.Green;
            //Atas-Bawah
           for (i = x1; i <= x2; i++)
            {
                Console.SetCursorPosition(i, y1);
                lip(i);
                Console.SetCursorPosition(i, y2);
                lip(i + (y2 - y1));
            }
            //Kanan-Kiri
            for (j = y1; j <= y2; j++)
            {
                Console.SetCursorPosition(x2, j) ;
                lip((x2-x1) + j);
                Console.SetCursorPosition(x1, j);
                lip(j);
            }

            char x =c1;c1 = c2; c2 = x;
        }

        static  void lip(int i)
        {
            if (i % 2 == 0)
                Console.Write(c1);
            else Console.Write(c2);
        }

    }
}

0 comments:

Post a Comment