GUI 剖析之 3)我想写画图程序

本文介绍了使用GDI和GDI+ API进行基本图形绘制的方法,包括线条、圆、矩形等,并展示了如何设置画笔颜色、宽度及样式来丰富图形表现。

首先介绍GDI里面的API

绘制线条:

HPEN  hPen;//分配画笔句柄

hPen=CreatePen(PS_SOLID,10,RGB(0,100,10));//创建画笔

SelectObject(hdc,hPen);//将画笔选入设备

MoveToEx(hdc, 0, 0,NULL);

LineTo(hdc, 10, 10);// MoveToExLineTo要配套使用,一个起点,一个终点

绘制圆

HPEN hPen;

hPen=CreatePen(PS_SOLID,10,RGB(0,0,255));

SelectObject(hdc,hPen);

Ellipse (hdc, 0, 0, 20, 30) ;

绘制矩形:

HPEN hPen;

hPen=CreatePen(PS_SOLID,10,RGB(100,0,0));

SelectObject(hdc,hPen);

Rectangle (hdc, 0, 0, 30, 30) ;

GDI+里面的API:

绘制线条:

Graphics  graphics(hdc);

Pen      pen(Color(255, 0, 0, 255));

graphics.DrawLine(&pen, 0, 0, 10, 10);

绘制矩形:

Graphics    graphics(hdc);

Pen blackPen(Color(255, 0, 0, 0), 5);

graphics.DrawRectangle(&blackPen, 0, 0, 10, 10);

绘制具有线帽的线条:

Graphics    graphics(hdc);

Pen pen(Color(255, 0, 0, 255), 8);

pen.SetStartCap(LineCapArrowAnchor);

pen.SetEndCap(LineCapRoundAnchor);

graphics.DrawLine(&pen,0, 0, 10, 10);

绘制自定义虚线:

Graphics    graphics(hdc);

REAL dashValues[4] = {5, 2, 15, 4};

Pen blackPen(Color(255, 0, 0, 0), 5);

blackPen.SetDashPattern(dashValues, 4);

graphics.DrawLine(&blackPen,Point(2,2), Point(10, 10));

绘制用纹理填充的线条:

Graphics    graphics(hdc);

static Image *gimg=NULL;

if (gimg==NULL)

{

       gimg=Image::FromFile(_T("my.jpg"));//这里的"my.jpg"是图片资源的名称,在调、//试时你应该将图片资源放在工程目录下,在外部执行EXE文件时应该放在exe

//文件一起,这是默认的,你也可以在编译器中设置路径。

}

TextureBrush  tBrush(gimg);

Pen           texturedPen(&tBrush, 30);

graphics.DrawImage(gimg, 0, 0, gimg->GetWidth()-600, gimg->GetHeight());

graphics.DrawEllipse(&texturedPen,0,0,12, 12);

用纯色填充形状:

Graphics graphics(hdc);

SolidBrush solidBrush(Color(255, 255, 0, 0));

graphics.FillEllipse(&solidBrush, 0, 0, 12, 12);

用阴影图案填充形状:

Graphics graphics(hdc);

HatchBrush hBrush(HatchStyleHorizontal, Color(255, 255, 0, 0),

Color(255, 128, 255, 255));

graphics.FillEllipse(&hBrush, 0, 0, 12, 12);

用渐变色填充形状:

Graphics graphics(hdc);

LinearGradientBrush linGrBrush(

Point(0,0),

Point(12, 12),

Color(255, 255, 0, 0),   

Color(255, 0, 0, 255));  

Pen pen(&linGrBrush);

graphics.FillEllipse(&linGrBrush,0, 0, 12, 12);

用阴影图案纹理填充形状:

Graphics graphics(hdc);

static Image * gimg=NULL;

if (gimg==NULL)

{

       gimg=Image::FromFile(_T("2011011434435349.jpg"));

}

TextureBrush tBrush(gimg);

tBrush.SetTransform(&Matrix(75.0/640.0, 0.0f, 0.0f,75.0/480.0, 0.0f, 0.0f));

graphics.FillEllipse(&tBrush,Rect( 0, 0, 20, 30));

在形状中平铺图像:

Graphics graphics(hdc);

static Image * gimg=NULL;

if (gimg==NULL)

{

       gimg=Image::FromFile(_T("my.jpg"));

}

TextureBrush tBrush(gimg);

Pen blackPen(Color(255, 0, 0, 255));

graphics.FillEllipse(&tBrush, Rect(0, 0, 23, 23));

graphics.DrawRectangle(&blackPen, Rect( 40, 40, 100, 100));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值