Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:csharp:gui_windows.forms_es_grafika

< CSharp

Windows.Forms és grafika

Vonal rajzolása

using System;
using System.Drawing;
using System.Windows.Forms;
 
public class Form1 : Form
{
        Button gomb = new Button();
 
        public Form1()
        {
                gomb.Text = "Kattints ide";
                gomb.Location = new Point(112, 50);
                gomb.Click += new EventHandler(Button_Click);
                Controls.Add(gomb);
 
                this.Paint += new PaintEventHandler(Form1_Paint);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Gomb lenyomva");
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
                Graphics g = this.CreateGraphics();
                Pen toll = new Pen(Color.Red, 5);  // Ecset színe és vastagsága
                g.DrawLine(toll, 20, 20, 200, 210);
        }
}
g.DrawLine(toll, x1, y1, x2, y2);

Az x1 és y1 a kezdőpont koordinátái. Az x2 és a y2 a végpont koordinátái.

Körök, ellipszisek rajzolása

using System;
using System.Drawing;
using System.Windows.Forms;
 
public class Form1 : Form
{
        Button gomb = new Button();
 
        public Form1()
        {
                gomb.Text = "Kattints ide";
                gomb.Location = new Point(112, 50);
                gomb.Click += new EventHandler(Button_Click);
                Controls.Add(gomb);
 
                Paint += new PaintEventHandler(Rajzolas);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Gomb lenyomva");
        }
 
        private void Rajzolas(object sender, PaintEventArgs e)
        {
                Graphics g = this.CreateGraphics();
                Pen toll = new Pen(Color.Red, 5);  // Toll színe és vastagsága
                Rectangle rajzTerulet = new Rectangle(300, 50, 50,80);	
                g.DrawEllipse(toll, rajzTerulet);
        }
}

Az ellipszist a köré írható téglalap határozza meg.

A téglalap megadásnál a Rectangle konstruktor a következő paraméterekkel rendelkezik:

Rectangle(x, y, szélesség, magasság);

A x,y a téglalap bal felső sarka.

Ha a rajzTerulet objektumot felakarjuk újra használni, a méreteit és a helyét újraállíthatjuk:

rajzTerulet.Location = new Point(50, 50);
rajzTerulet.Size = new Size(50, 50);

Téglalap rajzolása

using System;
using System.Drawing;
using System.Windows.Forms;
 
public class Form1 : Form
{
        Button gomb = new Button();
 
        public Form1()
        {
                gomb.Text = "Kattints ide";
                gomb.Location = new Point(112, 50);
                gomb.Click += new EventHandler(Button_Click);
                Controls.Add(gomb);
 
                this.Paint += new PaintEventHandler(Form1_Paint);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Gomb lenyomva");
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
                Graphics g = this.CreateGraphics();
                Pen toll = new Pen(Color.Red, 5);  // Toll színe és vastagsága
 
                Rectangle rajzTerulet = new Rectangle(20, 20, 250, 200);
                g.DrawRectangle(toll, rajzTerulet);
        }
}

Kitöltés

Ha Draw helyett Fill előtagú utasításokat használunk, akkor az adott alakzat kitöltődik. A kitöltött alakzathoz viszont nem toll, hanem ecset kell.

using System;
using System.Drawing;
using System.Windows.Forms;
 
public class Form1 : Form
{
        public Form1()
        {
		Width = 800;
		Height = 600;
 
                Paint += new PaintEventHandler(Rajzol);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Rajzol(object sender, PaintEventArgs e)
        {
                Graphics g = this.CreateGraphics();
		SolidBrush ecset = new SolidBrush(Color.Blue);
 
		Rectangle rajzTerulet = new Rectangle(300, 50, 50,80);			
		g.FillEllipse(ecset, rajzTerulet);				
        }
}

Sokszög és 2D rajz

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
        Button gomb = new Button();
 
        public Form1()
        {
                gomb.Text = "Kattints ide";
                gomb.Location = new Point(112, 50);
                gomb.Click += new EventHandler(Button_Click);
                Controls.Add(gomb);
 
                this.Paint += new PaintEventHandler(Form1_Paint);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Gomb lenyomva");
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
                Graphics g = this.CreateGraphics();
 
		Pen toll1 = new Pen(Color.Tomato,3);
 
                //Szagatott vonalas görbe rajzolása
                toll1.DashStyle = DashStyle.Dot;
                g.DrawPie(toll1,90,80,140,40,120,100);
 
 
		Pen toll2 = new Pen(Color.Maroon,4);
		//Sokszög rajzolása
                g.DrawPolygon(toll2,new Point[] {
                        new Point(30,140),
                        new Point(270,250),
                        new Point(110,240),
                        new Point(200,170),
                        new Point(70,350),
                        new Point(50,200)
                });
 
        }
}

Színátmenet

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
        Button gomb = new Button();
 
        public Form1()
        {
                gomb.Text = "Kattints ide";
                gomb.Location = new Point(112, 50);
                gomb.Click += new EventHandler(Button_Click);
                Controls.Add(gomb);
 
                this.Paint += new PaintEventHandler(Form1_Paint);
        }
 
        static public void Main()
        {
                Application.Run(new Form1());
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Gomb lenyomva");
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
 
                Graphics g = this.CreateGraphics();
 
                // ellipszis téglalap és színátmenetes ecset 
                Rectangle rajzTerulet = new Rectangle( 5, 35, 30, 100 );
                LinearGradientBrush linearisSzinatmenetEcset =
                        new LinearGradientBrush ( rajzTerulet, Color.Blue,
                                                  Color.Yellow, LinearGradientMode.ForwardDiagonal );
 
                // ellipszis rajzolása kitöltve kék-sárga színátmenettel
                g.FillEllipse( linearisSzinatmenetEcset, 5, 30, 60, 100 );
 
        }
}

Antialias

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
class Program : Form
{
	Program()
	{
		Width=800;
		Height=600;
		Paint += new PaintEventHandler(Rajzol);
	}
	static void Main()
	{
		Application.Run(new Program());
	}
	private void Rajzol(object sender, PaintEventArgs e)
	{
		Graphics g = e.Graphics;
		g.SmoothingMode = SmoothingMode.AntiAlias;
		Pen toll = new Pen(Color.Blue, 5);
		g.DrawEllipse(toll, 100, 100, 120, 80);
		g.SmoothingMode = SmoothingMode.Default;
		g.DrawEllipse(toll, 250, 100, 120, 80);
	}
}

Szöveg rajzolása

Szöveg normál módon, vízszintesen

using System;
using System.Drawing;
using System.Windows.Forms;
 
class Program : Form
{
	Program()
	{
 
		Width = 800;
		Height = 600;
		Paint += new PaintEventHandler(Rajzol);
	}
	private void Rajzol(object sender, PaintEventArgs e)
	{
		Graphics g = CreateGraphics();
		SolidBrush ecset = new SolidBrush(Color.Black);
		g.DrawString("Valami", new Font("Arial", 180), ecset, 50, 100);
	}
	public static void Main()
	{
		Application.Run(new Program());
	}
}

Szöveg függőlegesen

using System;
using System.Drawing;
using System.Windows.Forms;
 
class Program : Form
{
	Program()
	{
 
		Width = 800;
		Height = 600;
		Paint += new PaintEventHandler(Rajzol);
	}
	private void Rajzol(object sender, PaintEventArgs e)
	{
		Graphics g = CreateGraphics();
		SolidBrush ecset = new SolidBrush(Color.Black);
		StringFormat forma = new StringFormat();
		forma.FormatFlags = StringFormatFlags.DirectionVertical;
		g.DrawString("Valami", new Font("Arial", 120), ecset, 50, 100, forma);
	}
	public static void Main()
	{
		Application.Run(new Program());
	}
}

Vegyük észre, hogy egy plusz osztályt kellet betenni. Ez a StringFormat. Annak beállítottuk a FormatFlags tulajdonságát. A tulajdonság lehetséges értékei a StringFormatFlags felsorolt típusból van.

Rajzolás alternatív módon

Eddig mindig úgy dolgoztunk ha rajzolni akartunk, hogy az ablak Paint eseményéhez rendeltünk egy rajzoló metódust. Úgy is rajzolhatunk egy ablakra, ha a már meglévő OnPaint metódusát felülírjuk:

using System;
using System.Drawing;
using System.Windows.Forms;
 
class Program : Form
{
	Program()
	{
 
		Width = 800;
		Height = 600;
	}
	protected override void OnPaint(PaintEventArgs e)
	{
		Graphics g = CreateGraphics();		
		Pen toll = new Pen(Color.Blue, 5);
		g.DrawRectangle(toll, 50, 50, 100, 100);
	}
	public static void Main()
	{
		Application.Run(new Program());
	}
}
oktatas/programozas/csharp/gui_windows.forms_es_grafika.txt · Utolsó módosítás: 2019/08/21 22:27 szerkesztette: admin