using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Pen p = new Pen(Color.Black);
g.DrawLine(p, 0, 0, 100, 100);
p.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
illustrates the use of multiple Pens
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example21_2.cs illustrates the use of multiple Pens
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Example21_2 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
public Example21_2()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(400, 400);
this.Name = "Example21_2";
this.Text = "Example21_2";
this.Paint += new System.Windows.Forms.
PaintEventHandler(this.Example21_2_Paint);
}
static void Main()
{
Application.Run(new Example21_2());
}
private void Example21_2_Paint(
object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
// draw two lines with one pen
Pen p = new Pen(Color.Black, 10);
g.DrawLine(p, 25, 25, 375, 375);
g.DrawLine(p, 25, 375, 375, 25);
// draw four lines with another pen
Pen p2 = new Pen(Color.Gray, 7);
p2.EndCap = LineCap.Round;
p2.StartCap = LineCap.ArrowAnchor;
g.DrawLine(p2, 25, 35, 25, 365);
g.DrawLine(p2, 35, 375, 365, 375);
g.DrawLine(p2, 375, 365, 375, 35);
g.DrawLine(p2, 365, 25, 35, 25);
}
}
Pen Dash Styles
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GDI_Basics
{
/// <summary>
/// Summary description for PenDashStyles.
/// </summary>
public class PenDashStyles : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public PenDashStyles()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// PenDashStyles
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(500, 398);
this.Name = "PenDashStyles";
this.Text = "PenDashStyles";
this.Resize += new System.EventHandler(this.PenDashStyles_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenDashStyles_Paint);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new PenDashStyles());
}
private void PenDashStyles_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 10);
int y = 20;
foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle)))
{
myPen.DashStyle = dash;
e.Graphics.DrawLine(myPen, 20, y, 100, y);
e.Graphics.DrawString(dash.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
y += 30;
}
y += 10;
myPen.StartCap = LineCap.Round;
myPen.EndCap = LineCap.Round;
foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle)))
{
myPen.DashStyle = dash;
e.Graphics.DrawLine(myPen, 20, y, 100, y);
e.Graphics.DrawString(dash.ToString() + " (with round caps)", new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
y += 30;
}
}
private void PenDashStyles_Resize(object sender, System.EventArgs e)
{
this.Invalidate();
}
}
}
Pen width and color
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitializeComponent();
CenterToScreen();
SetStyle(ControlStyles.ResizeRedraw, true);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Pens...";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
}
static void Main()
{
Application.Run(new MainForm());
}
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen bluePen = new Pen(Color.Blue, 20);
Pen pen2 = Pens.Firebrick;
// Render some shapes with the pens.
g.DrawEllipse(bluePen, 10, 10, 100, 100);
g.DrawLine(pen2, 10, 130, 110, 130);
g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Pen alignment: inset
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 3);
p.Alignment = PenAlignment.Inset;
g.DrawRectangle(p, 3, 3, 80, 70);
p.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Pen alignment: center
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 3);
p.Alignment = PenAlignment.Center;
g.DrawRectangle(p, 3, 3, 80, 70);
p.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
make red and blue pens
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class TestGDI1 : System.Windows.Forms.Form{
//in order to paint something OnPaint method needs to be overridden
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
//OnPaint method is a member of Form class
//The following call sends pe to an event listener Graphics
base.OnPaint(pe);
//initialize Graphics
System.Drawing.Graphics g=pe.Graphics;
//designate the area of the form where the drawing must take place
//ClientRectangle is a member of Windows.Forms.Control class
System.Drawing.Rectangle client_area=this.ClientRectangle;
//point11 is at the top left corner of the client_area
System.Drawing.Point point11=new System.Drawing.Point(client_area.X,client_area.Y);
//point12 is at the bottom right corner of the client area
System.Drawing.Point point12=new System.Drawing.Point(client_area.Width,client_area.Height);
//create a Brush object of white color
//SolidBrush means that the color does not change from point to point
System.Drawing.Brush background=new System.Drawing.SolidBrush(System.Drawing.Color.White);
//color client_area with solid white brush
g.FillRectangle(background,client_area);
//make red and blue pens
System.Drawing.Pen p=new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Pen p1=new System.Drawing.Pen(System.Drawing.Color.Blue);
//create points and rectangles
System.Drawing.SizeF size=new System.Drawing.SizeF();
size.Height=160;
size. Width=180;
System.Drawing.PointF point=new System.Drawing.PointF();
point.X=8;
point.Y=40;
System.Drawing.Point point1=new System.Drawing.Point();
point1.X=300;
point1.Y=300;
System.Drawing.Point point2=new System.Drawing.Point();
point2.X=0;
point2.Y=0;
System.Drawing.RectangleF rec =new System.Drawing.RectangleF(point,size);
//draw an ellipse inscribed in the invisible rectangle rec
//to change the size or shape of the ellipse change an invisible rectangle in which it is inscribed
//to change the color of the ellipse, change the color of the pen p which is used to draw it
g.DrawEllipse(p,rec);
//draw a line between a pair of points point1 and point2 with pen p1
g.DrawLine(p1,point1,point2);
}
public static void Main() {
System.Windows.Forms.Application.Run(new TestGDI1());//display form
}
}