ویژوال 30 شارپ

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

ویژوال 30 شارپ

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

Transparent Circle Painting

bool b = false;
        int x1, y1 = 0;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (b == true)
            {
                SolidBrush sb = new SolidBrush(Color.White);
                Bitmap bi = new Bitmap(Width, Height);
                Graphics g = Graphics.FromImage(bi);
                int xx = e.X - x1;
                int yy = e.Y - y1;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawEllipse(new Pen(sb), new Rectangle(x1, y1, xx, yy));
                sb.Color = Color.FromArgb(150, 255, 0, 0);
                g.FillEllipse(sb, new Rectangle(x1, y1, xx, yy));
                pictureBox1.Image = bi;

            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            b = false;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            b = true;
            x1 = e.X;
            y1 = e.Y;
        }

Texture Brush

bool cl = false;
        int x1, y1 = 0;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            cl = true;
            x1 = e.X;
            y1 = e.Y;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            cl = false;
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (cl == true)
            {
                Bitmap b = new Bitmap(5, 5);
                Rectangle r1 = new Rectangle(0, 0, 5, 5);
                Graphics g = Graphics.FromImage(b);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                LinearGradientBrush lgb = new LinearGradientBrush(r1,
                    Color.Yellow, Color.Green , LinearGradientMode.ForwardDiagonal);
                g.FillRectangle(lgb, r1);
               
                Rectangle r = new Rectangle(x1, y1, (e.X - x1) + 1, (e.Y - y1) + 1);
                Bitmap b1 = new Bitmap(Width, Height);
                Graphics g1 = Graphics.FromImage(b1);
                g1.SmoothingMode = SmoothingMode.AntiAlias;
                TextureBrush tb = new TextureBrush(b);
                g1.FillRectangle(tb, r);
                g1.DrawRectangle(new Pen(Brushes.Black), r);
                this.BackgroundImage = b1;

            }
        }

String Lines Count

using System.Text.RegularExpressions;

public static long LineCount2(string source, bool isFileName)
{
    if (source != null)
    {
        string text = source;
        long numOfLines = 0;
        if (isFileName)
        {
            using (FileStream FS = new FileStream(source, FileMode.Open,
            FileAccess.Read, FileShare.Read))
            {
                using (StreamReader SR = new StreamReader(FS))
                {
                    while (text != null)
                    {
                        text = SR.ReadLine();
                        if (text != null)
                        {
                            ++numOfLines;
                        }
                    }
                }
            }
            return (numOfLines);
        }
        else
        {
            Regex RE = new Regex("\n", RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(text);
            return (theMatches.Count + 1);
        }
    }
    else
    {
        // Handle a null source here.
        return (0);
    }
}

Startup Dir

MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);


MessageBox.Show(System.IO.Directory.GetCurrentDirectory());

MessageBox.Show(Application.StartupPath);

MessageBox.Show(System.IO.Path.GetDirectoryName(Application.ExecutablePath));

Star Form

int l = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            l-=1;
            System.Drawing.Drawing2D.GraphicsPath g = new System.Drawing.Drawing2D.GraphicsPath();
            for (int i = 0; i <= 360; i+=20)
            {
                g.AddLine((float)Math.Cos(Math.PI * (i+l) / 180) * this.Width / 2 + this.Width / 2
                    , (float)Math.Sin(Math.PI * (i+l) / 180) * this.Height / 2 + this.Height / 2,
                    (float)Math.Cos(Math.PI * (i +l) / 180) * this.Width / 4 + this.Width / 2,
                    (float)Math.Sin(Math.PI * (i+l) / 180) * this.Height / 4 + this.Height / 2);
            }
            this.Region = new Region(g);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 300;
            this.Height = 300;
        }