《GDI+系列教程》第6章 —— 饼图与线段的效果

本章,我们会使用饼图与线段,加上前5章学到的知识(例如纹理画刷),总结性的在屏幕上画一个娃娃出来。

本章结束以后,GDI+ 中常用绘画工具你就已经都有所了解并有一定掌握了,剩下的就是创意与练习。

《GDI+系列教程》第6章 —— 饼图与线段的效果


1.使用定式直接创建出画布

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip_All.ahk

; 初始化 gdi+ 需要的一切
gosub, init

2.矩形画脸

; Create a hatched brush with background and foreground colours specified (HatchStyleBackwardDiagonal = 3)
; Draw into the graphics of the bitmap from coordinates (100,80) a filled rectangle with 200 width and 250 height using the brush created
; Delete the brush created to save memory as we don't need the same brush anymore
; 矩形画脸
pBrush := Gdip_BrushCreateHatch(0xff553323, 0xffc09e8e, 31)
Gdip_FillRectangle(G, pBrush, (400-200)//2, (400-250)//2, 200, 250)
Gdip_DeleteBrush(pBrush)

3.饼图画鼻子

; Create a hatched brush with background and foreground colours specified (HatchStyleBackwardDiagonal = 3)
; Draw an ellipse into the graphics with the brush we just created. 40 degree sweep angle starting at 250 degrees (0 degrees from right horizontal)
; Delete the brush created to save memory as we don't need the same brush anymore
; 饼图画鼻子
; Gdip_FillPie(画布, 画刷, 椭圆x, 椭圆y, 椭圆宽, 椭圆高, 起始角度, 幅度)
; 终点角度 = 起始角度 + 幅度
; 注意 Gdip_FillPie() 是从一个椭圆中切出饼来的,如果 饼图宽 = 饼图高,也就是正圆时,那么切出来的自然就是扇形。
pBrush := Gdip_BrushCreateHatch(0xff000000, 0xff4d3538, 31)
Gdip_FillPie(G, pBrush, (400-80)//2, (400-80)//2, 80, 80, 250, 40)
Gdip_DeleteBrush(pBrush)

4.圆形画眼睛

; Create a white brush
; Create a black brush
; Loop to draw 2 ellipses filling them with white then black in the centre of each
; Delete both brushes
; 圆形画眼睛
pBrushWhite := Gdip_BrushCreateSolid(0xffffffff)
pBrushBlack := Gdip_BrushCreateSolid(0xff000000)
Loop, 2
{
	x := (A_Index = 1) ? 120 : 220, y := 100
	Gdip_FillEllipse(G, pBrushWhite, x, y, 60, 60)
	x += 15, y+=15
	Gdip_FillEllipse(G, pBrushBlack, x, y, 30, 30)
}
Gdip_DeleteBrush(pBrushWhite), Gdip_DeleteBrush(pBrushBlack)

5.饼图画嘴

; Create a hatched brush with background and foreground colours specified (HatchStyle30Percent = 10)
; Again draw another ellipse into the graphics with the specified brush
; Delete the brush
; 饼图画嘴
pBrush := Gdip_BrushCreateHatch(0xfff22231, 0xffa10f19, 10)
Gdip_FillPie(G, pBrush, 150, 200, 100, 80, 0, 180)
Gdip_DeleteBrush(pBrush)

6.线段画胡子

; Create a 3 pixel wide slightly transparent black pen
; Create some coordinates for the lines in format x1,y1,x2,y2 then loop and draw all the lines
; Delete the pen
; 线段画胡子
pPen := Gdip_CreatePen(0xbb000000, 3)
Lines = 180,200,130,220|180,190,130,195|220,200,270,220|220,190,270,195
Loop, Parse, Lines, |
{
	StringSplit, Pos, A_LoopField, `,
	; 画线段 Gdip_DrawLine(画布, 画笔, x1, y1, x2, y2)
	Gdip_DrawLine(G, pPen, Pos1, Pos2, Pos3, Pos4)
}
Gdip_DeletePen(pPen)

7.显示出来

; Update the specified window
; 显示出来
UpdateLayeredWindow(hwnd1, hdc, A_ScreenWidth//2-200, A_ScreenHeight//2-250, A_ScreenWidth, A_ScreenHeight)
Return

8.收工善后

ExitGdip:
	; Select the object back into the hdc
	SelectObject(hdc, obm)

	; Now the bitmap may be deleted
	DeleteObject(hbm)

	; Also the device context related to the bitmap may be deleted
	DeleteDC(hdc)

	; The graphics may now be deleted
	Gdip_DeleteGraphics(G)

	; ...and gdi+ may now be shutdown
	Gdip_Shutdown(pToken)
	ExitApp
Return

0.定式

init:
	; 以下都是初始化定式
	; Start gdi+
	If !pToken := Gdip_Startup()
	{
		MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
		ExitApp
	}
	OnExit, ExitGdip
	; Create a layered window (+E0x80000) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
	Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
	; Show the window
	Gui, 1: Show, NA
	; Get a handle to this window we have created in order to update it later
	hwnd1 := WinExist()
	; Create a gdi bitmap with width and height of the work area
	hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
	; Get a device context compatible with the screen
	hdc := CreateCompatibleDC()
	; Select the bitmap into the device context
	obm := SelectObject(hdc, hbm)
	; Get a pointer to the graphics of the bitmap, for use with drawing functions
	G := Gdip_GraphicsFromHDC(hdc)
	; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
	Gdip_SetSmoothingMode(G, 4)
Return

本章习题:画棋盘或任何其它你自己想到的可以使用到前6章知识的图案。

《GDI+系列教程》第6章 —— 饼图与线段的效果


全部代码与库文件下载地址:

https://ahk.lanzoux.com/b01nypnuh

密码:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
教程

《GDI+系列教程》第5章 —— 纹理画刷的效果

2021-1-28 12:22:03

教程

《GDI+系列教程》第7章 —— 将画布内容保存为文件

2021-1-28 13:10:50

2 条回复 A文章作者 M管理员
  1. 墨流

    蕉作业

  2. 未名游客给您打赏了¥2
个人中心
购物车
优惠劵
有新私信 私信列表
搜索