《GDI+系列教程》的作者:空
本章,我们会使用饼图与线段,加上前5章学到的知识(例如纹理画刷),总结性的在屏幕上画一个娃娃出来。
本章结束以后,GDI+ 中常用绘画工具你就已经都有所了解并有一定掌握了,剩下的就是创意与练习。
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章知识的图案。
全部代码与库文件下载地址:
https://ahk.lanzoux.com/b01nypnuh
密码:1234
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)