; gdi+ ahk tutorial 3 written by tic (Tariq Porter) ; Requires Gdip.ahk either in your Lib folder as standard library or using #Include ; ; Tutorial to take make a gui from an existing image on disk ; For the example we will use png as it can handle transparencies. The image will also be halved in size #SingleInstance Force ;#NoEnv ;SetBatchLines -1 ; Uncomment if Gdip.ahk is not in your standard library #Include ../Gdip_All.ahk ; Start gdi+ If !pToken := Gdip_Startup() { MsgBox "Gdiplus failed to start. Please ensure you have gdiplus on your system" ExitApp } OnExit("ExitFunc") ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption ;AHK v1 ;Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs ;Gui, 1: Show, NA Gui1 := GuiCreate("-Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs") Gui1.Show("NA") ; Get a handle to this window we have created in order to update it later hwnd1 := WinExist() ; Get a bitmap from the image pBitmap := Gdip_CreateBitmapFromFile("background.png") ; Check to ensure we actually got a bitmap from the file, in case the file was corrupt or some other error occured If !pBitmap { MsgBox "Could not load 'background.png'" ExitApp } ; Get the width and height of the bitmap we have just created from the file ; This will be the dimensions that the file is Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap) ; Create a gdi bitmap with width and height of what we are going to draw into it. This is the entire drawing area for everything ; We are creating this "canvas" at half the size of the actual image ; We are halving it because we want the image to show in a gui on the screen at half its dimensions hbm := CreateDIBSection(Width//2, Height//2) ; 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) ; We do not need SmoothingMode as we did in previous examples for drawing an image ; Instead we must set InterpolationMode. This specifies how a file will be resized (the quality of the resize) ; Interpolation mode has been set to HighQualityBicubic = 7 Gdip_SetInterpolationMode(G, 7) ; DrawImage will draw the bitmap we took from the file into the graphics of the bitmap we created ; We are wanting to draw the entire image, but at half its size ; Coordinates are therefore taken from (0,0) of the source bitmap and also into the destination bitmap ; The source height and width are specified, and also the destination width and height (half the original) ; Gdip_DrawImage(pGraphics, pBitmap, dx, dy, dw, dh, sx, sy, sw, sh, Matrix) ; d is for destination and s is for source. We will not talk about the matrix yet (this is for changing colours when drawing) Gdip_DrawImage(G, pBitmap, 0, 0, Width//2, Height//2, 0, 0, Width, Height) ; Update the specified window we have created (hwnd1) with a handle to our bitmap (hdc), specifying the x,y,w,h we want it positioned on our screen ; So this will position our gui at (0,0) with the Width and Height specified earlier (half of the original image) UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width//2, Height//2) ; 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) ; The bitmap we made from the image may be deleted Gdip_DisposeImage(pBitmap) Return ;####################################################################### ExitFunc(ExitReason, ExitCode) { global ; gdi+ may now be shutdown on exiting the program Gdip_Shutdown(pToken) }