GDI+学习笔记(待续)
来源:互联网 发布:js随机生成英文名字 编辑:程序博客网 时间:2024/05/01 11:01
一:在应用程序中获取一个Graphics对象
选择哪一种绘图表面?
Windows应用程序中,一个窗体就是一个绘图表面,每个窗体都有一个与之相关联的Graohics对象。
如何获得一个与窗口相关联的Graohics对象。
法一:使用窗体paint事件的PaintEventArgs属性来获取Graohics对象。
private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs e)
{ Graphics g = e.Graphics; }
法二:重载OnPaint方法
protected override void OnPaint(PaintEventArgs e)
{ Graphics g = e.Graphics; }
法三:通过使用CreateGraphics方法
Graphics. g=this.CreateGraphics();
g.Clear(this.BackColor);
g.Dispose();
二:创建钢笔和画笔
Pen redPen = new Pen(Color.Red,3); //红色,宽度为3个像素
Pen blackPen = Pens.Black; //黑色1个像素
SolidBrush greenBrush = new SolidBrush(Color.Green); //绿色实心画笔
三:绘制图形
四:第一个GDI+应用程序
protected override void OnPaint(PaintEventArgs e)
{
// Obtain the Graphics object
Graphics g = e.Graphics;
// Set the composit quality and smooting mode
// of the surface
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
// Create a rectangle from point (20, 20) to (100, 100)
Rectangle rect = new Rectangle(20, 20, 100, 100);
// Create two Pen objects with Red and Green color
Pen redPen = new Pen(Color.Red, 3);
Pen blackPen = Pens.Black;
// Create a SolidBrush objects
SolidBrush greenBrush = new SolidBrush(Color.Green);
// Draw shapes and lines
g.DrawRectangle(redPen, rect);
g.FillEllipse(greenBrush, rect);
g.DrawLine(blackPen, 0, 250, this.Width, 250);
g.FillEllipse(Brushes.Blue, 70, 220, 30, 30);
g.FillEllipse(Brushes.SkyBlue, 100, 210, 40, 40);
g.FillEllipse(Brushes.Green, 140, 200, 50, 50);
g.FillEllipse(Brushes.Yellow, 190, 190, 60, 60);
g.FillEllipse(Brushes.Violet, 250, 180, 70, 70);
g.FillEllipse(Brushes.Red, 320, 170, 80, 80);
// Dispose objects
greenBrush.Dispose();
// blackPen.Dispose();
redPen.Dispose();
g.Dispose();
}
五.Color结构体
六:Point和PointF结构
private void menuItem2_Click(object sender, System.EventArgs e)
{
// Create a new Point
Point pt = new Point(50, 50);
// Create a new point using Point.Empty
Point newPoint = Point.Empty;
// Set X and Y properties of Point
newPoint.X = 100;
newPoint.Y = 200;
// Create a Graphics object from the
// current form's handle
Graphics g = Graphics.FromHwnd(this.Handle);
// Create a new pen with blue color
// and with = 4
Pen pn = new Pen(Color.Blue, 4);
// Draw a line from point pt to
// new point
g.DrawLine(pn, pt, newPoint);
// Dispose pen and graphics objects
pn.Dispose();
g.Dispose();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
// Create a new PointF
PointF pt = new PointF(50.0F, 50.0F);
// Create a new point using PointF.Empty
PointF newPoint = PointF.Empty;
// Set X and Y properties of PointF
newPoint.X = 100.0F;
newPoint.Y = 200.0F;
// Create a Graphics object from the
// current form's handle
Graphics g = Graphics.FromHwnd(this.Handle);
// Create a new pen with blue color
// and with = 4
Pen pn = new Pen(Color.Blue, 4);
// Draw a line from point pt to
// new point
g.DrawLine(pn, pt, newPoint);
// Dispose pen and graphics objects
pn.Dispose();
g.Dispose();
}
七.Rectangle和RectangleF结构体
private void RectCreate_Click(object sender, System.EventArgs e)
{
// Create a Graphics object
Graphics g = this.CreateGraphics();
int x = 40;
int y = 40;
int height = 120;
int width = 120;
// Create a Point
Point pt = new Point(80, 80);
// Create a Size
Size sz = new Size(100, 100);
// Create a Rectangle from Point and Size
Rectangle rect1 = new Rectangle(pt, sz);
// Create a Rectangel from integers
Rectangle rect2 =
new Rectangle(x, y, width, height);
// Create a Rectangle from direct integers
Rectangle rect3 =
new Rectangle(10, 10, 180, 180);
// Create pens and brushes
Pen redPen = new Pen(Color.Red, 2);
SolidBrush greenBrush =
new SolidBrush(Color.Blue);
SolidBrush blueBrush =
new SolidBrush(Color.Green);
// Draw and fill rectangles
g.DrawRectangle(redPen, rect3);
g.FillRectangle(blueBrush, rect2);
g.FillRectangle(greenBrush, rect1);
// Dispose
redPen.Dispose();
blueBrush.Dispose();
greenBrush.Dispose();
g.Dispose();
}
private void RectFCreate_Click(object sender, System.EventArgs e)
{
// Create a Graphics object
Graphics g = this.CreateGraphics();
float x = 40.0f;
float y = 40.0f;
float height = 120.0f;
float width = 120.0f;
// Create a Point
PointF pt = new PointF(80.0f, 80.0f);
// Create a Size
SizeF sz = new SizeF(100.0f, 100.0f);
// Create a Rectangle from Point and Size
RectangleF rect1 = new RectangleF(pt, sz);
// Create a Rectangel from integers
RectangleF rect2 =
new RectangleF(x, y, width, height);
// Create a Rectangle from direct integers
RectangleF rect3 =
new RectangleF(10.0f, 10.0f, 180.0f, 180.0f);
// Create pens and brushes
Pen redPen = new Pen(Color.Red, 2);
SolidBrush greenBrush =
new SolidBrush(Color.Blue);
SolidBrush blueBrush =
new SolidBrush(Color.Green);
// Draw and fill rectangles
g.DrawRectangle(redPen, rect3.X, rect3.Y,
rect3.Width, rect3.Height);
g.FillRectangle(blueBrush, rect2);
g.FillRectangle(greenBrush, rect1);
// Dispose
redPen.Dispose();
blueBrush.Dispose();
greenBrush.Dispose();
g.Dispose();
}
- GDI+学习笔记(待续)
- SQL学习笔记(待续)
- vim学习笔记(待续...)
- EJB学习笔记 (待续)
- Gdi+学习笔记(一)
- Gdi+学习笔记(二)
- GDI+学习笔记(一)
- Ajax学习笔记总结(未完待续)
- jasperreport+ireport学习笔记(待续)
- struts2 学习笔记(未完待续)
- Oracle学习笔记(未完待续)
- Linux Glibc(学习笔记五)待续。。。。
- dreamweaver学习笔记(未完待续)
- css学习笔记(未完待续)
- 51单片机汇编学习笔记(待续)
- 【Matlab学习笔记】保存图片(待续)
- angular2学习笔记(未完待续)
- 进程学习笔记(未完待续)
- air的应用程序描述文件application.xml(中文说明)
- 网页换皮肤的实现
- 如何将excel文件导入到mssql中(dephi)
- JSP与SQL SERVER的留言本.
- 用.NET建立Office Add-in
- GDI+学习笔记(待续)
- Graphics类
- 使用画笔和钢笔
- 颜色、字体和文本
- 在我什么都没有的时候
- I'm not ivy!
- 矩形和区域
- 我的心情
- Eclipse开发调试RMI指南