《GDI+系列教程》的作者:空
在《GDI+系列教程》前4章里,我不厌其烦的向大家解释了几乎每一句代码的作用、由来、概念等等,并且总结了相应的使用定式。这是因为刚入门,很多东西需要有一个熟悉的过程。
从本章开始,入门算是结束了,咱得开始提高了。所以我们将直接使用之前总结好的定式,并且任何旧知识,都尽量简言带过,只重点讲述新内容。
本章,我们会在屏幕上画一堆圆,演示纹理画刷的效果。
1.使用定式直接创建出画布并准备画圆
#SingleInstance, Force #NoEnv SetBatchLines, -1 ; Uncomment if Gdip.ahk is not in your standard library #Include, Gdip_All.ahk ; 初始化 gdi+ 需要的一切 gosub, init ; Set a timer to draw a new ellipse every 2000ms ; 使用纹理画刷画圆 SetTimer, DrawCircle, 2000 Return
2.随机设置纹理样式等参数
DrawCircle: ; Get a random colour for the background and foreground of hatch style used to fill the ellipse, ; as well as random brush style, x and y coordinates and width/height ; 随机生成圆的参数 ; 这里将透明度固定为 0xff 即不透明,否则随机生成的数范围太大,又常常夹带透明度,造成看起来都是白色的。 Random, RandBackColour, 0xff000000, 0xffffffff Random, RandForeColour, 0xff000000, 0xffffffff ; 纹理样式 Random, RandBrush, 0, 53 ; 圆的高宽 设置大一点,才看得清里面的纹理 Random, RandElipseWidth, 200, 300 Random, RandElipseHeight, 200, 300 ; 圆的坐标 Random, RandElipsexPos, %WALeft%, % WAWidth-RandElipseWidth Random, RandElipseyPos, %WATop%, % WAHeight-RandElipseHeight
3.使用随机纹理样式等参数画圆
; Create the random brush ; 创建纹理画刷 又叫影线画刷 Gdip_BrushCreateHatch(背景色, 前景色, 样式) pBrush := Gdip_BrushCreateHatch(RandBackColour, RandForeColour, RandBrush) ; Fill the graphics of the bitmap with an ellipse using the brush created ; 填充圆 Gdip_FillEllipse(G, pBrush, RandElipsexPos, RandElipseyPos, RandElipseWidth, RandElipseHeight) ; Update the specified window ; 显示出来 UpdateLayeredWindow(hwnd1, hdc, WALeft, WATop, WAWidth, WAHeight) ; Delete the brush as it is no longer needed and wastes memory ; 刷子用完了,要立刻释放资源。在本例中,实际效果来讲,放 Exitgdip 标签里也一样,但放这里主要是提醒大家,即用即释放,就像随手关门一样! Gdip_DeleteBrush(pBrush) Return
4.收工善后
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 ; Get the dimensions of the primary monitor ; 将作画范围设置为整个屏幕 SysGet, MonitorPrimary, MonitorPrimary SysGet, WA, MonitorWorkArea, %MonitorPrimary% WAWidth := WARight-WALeft WAHeight := WABottom-WATop ; 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(WAWidth, WAHeight) ; 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
本章习题:画一个棋盘出来。(主要是这个纹理)
全部代码与库文件下载地址:
https://ahk.lanzoux.com/b01nypnuh
密码:1234
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)