ویژوال 30 شارپ

سورس کدهای جالب سی شارپ فقط با یک نگاه

ویژوال 30 شارپ

سورس کدهای جالب سی شارپ فقط با یک نگاه

Analog Clock

private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox1.Width,pictureBox1.Height);
            Graphics g = Graphics.FromImage(b);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            for (int i = 1; i <=4; i++)
            {
                float f1 = (float)Math.Sin(Math.PI / 2 * i) * b.Width / 2 + b.Width / 2;
                float f2 = (float)Math.Cos(Math.PI / 2 * i) * b.Height / 2 + b.Height / 2;
                float f3 = (float)Math.Sin(Math.PI / 2 * i) * 90 + b.Width / 2;
                float f4 = (float)Math.Cos(Math.PI / 2 * i) * 90 + b.Height / 2;
                g.DrawLine(new Pen(Brushes.Black, 2), f3 ,
                   f4, f1, f2);
            }
            System.Drawing.Drawing2D.LinearGradientBrush l = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, Width, Height),
                Color.YellowGreen, Color.Green, System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal);
            g.FillEllipse(l, new Rectangle(0, 0, b.Width - 1, b.Height - 1));
            g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(0, 0, b.Width - 1, b.Height - 1));
            pictureBox1.BackgroundImage = b;

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(b);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            DateTime dt = DateTime.Now;
            double i1 = (dt.Minute * 360) / 60;
            float fm1 = (float)(Math.Sin(Math.PI * i1 / 180) * (b.Width / 2 - 20) + b.Width / 2);
            float fm2 = (float)(-Math.Cos(Math.PI * i1 / 180) * (b.Height / 2 - 20) + b.Height / 2);
            float fm3 = (float)(-Math.Sin(Math.PI * i1 / 180) * (b.Width / 2 - 90) + b.Width / 2);
            float fm4 = (float)(Math.Cos(Math.PI * i1 / 180) * (b.Height / 2 - 90) + b.Height / 2);
            #region Hour
            double i2 =  (dt.Hour * 360) / 12 ;
            float fh1 = (float)(Math.Sin(Math.PI * i2 / 180) * (b.Width / 2 - 50) + b.Width / 2);
            float fh2 = (float)(-Math.Cos(Math.PI * i2 / 180) * (b.Height / 2 - 50) + b.Height / 2);
            float fh3 = (float)(-Math.Sin(Math.PI * i2 / 180) * (b.Width / 2 - 90) + b.Width / 2);
            float fh4 = (float)(Math.Cos(Math.PI * i2 / 180) * (b.Height / 2 - 90) + b.Height / 2);
            g.DrawLine(new Pen(Brushes.Black, 6), b.Width / 2, b.Height / 2, fh1, fh2);
            g.DrawLine(new Pen(Brushes.Black, 6), b.Width / 2, b.Height / 2, fh3, fh4);
            #endregion

            #region Minute
             g.DrawLine(new Pen(Brushes.Blue, 3), b.Width / 2, b.Height / 2, fm1, fm2);
            g.DrawLine(new Pen(Brushes.Blue, 3), b.Width / 2, b.Height / 2, fm3, fm4);
            #endregion

            #region Second

            double i = (dt.Second * 360) / 60;
            float f1 = (float)(Math.Sin(Math.PI * i / 180) * (b.Width / 2 - 15) + b.Width / 2);
            float f2 = (float)(-Math.Cos(Math.PI * i / 180) * (b.Height / 2 - 15) + b.Height / 2);
            float f3 = (float)(-Math.Sin(Math.PI * i / 180) * (b.Width / 2 - 70) + b.Width / 2);
            float f4 = (float)(Math.Cos(Math.PI * i / 180) * (b.Height / 2 - 70) + b.Height / 2);
            g.DrawLine(new Pen(Brushes.Red, 1), b.Width / 2, b.Height / 2, f1, f2);
            g.DrawLine(new Pen(Brushes.Red, 1), b.Width / 2, b.Height / 2, f3, f4);
            #endregion

            g.FillEllipse(Brushes.Black, new Rectangle(b.Width / 2 - 6, b.Height / 2 - 6, 12, 12));
          
            pictureBox1.Image = b;
        }

3D Box

private void Form1_Load(object sender, EventArgs e)
        {
            SolidBrush sb = new SolidBrush(Color.Black);
            Bitmap bi = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bi);
            Rectangle r = new Rectangle(20, 20, 150, 130);
            sb.Color = Color.Red;
            g.FillRectangle(sb, r);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            sb.Color = Color.Black;
            g.DrawRectangle(new Pen(sb), r);
            Rectangle r1 = new Rectangle(40, 40, 150, 130);
            sb.Color = Color.FromArgb(150, 255, 255, 0);
            g.FillRectangle(sb, r1);
            sb.Color = Color.Black;
            g.DrawRectangle(new Pen(sb), r1);
            g.DrawLine(new Pen(sb), 20, 20, 40, 40);
            g.DrawLine(new Pen(sb), 170, 20, 190, 40);
            g.DrawLine(new Pen(sb), 20, 150, 40, 170);
            g.DrawLine(new Pen(sb), 170, 150, 190, 170);
            this.BackgroundImage = bi;

        }

3D Borders With GDI+

private void DrawBorder3D(Graphics g, ref Rectangle rc,
Border3DStyle borderStyle)
{
    ControlPaint.DrawBorder3D(g, rc, borderStyle);
    g.DrawString(borderStyle.ToString(), Font, Brushes.Black,
    rc.Width + 5, rc.Y + (rc.Height - Font.Height) / 2);
    rc.Offset(0, rc.Height + 5);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Rectangle rc = new Rectangle(0, 0, 50, 25);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Adjust);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Bump);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Etched);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Flat);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Raised);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.RaisedInner);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.RaisedOuter);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.Sunken);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.SunkenInner);
    DrawBorder3D(e.Graphics, ref rc, Border3DStyle.SunkenOuter);
}