/Data/img/Test Background.ahk
http://github.com/Appifyer/Zizorz · AutoHotKey · 105 lines · 40 code · 25 blank · 40 comment · 0 complexity · 06262d67d258cf4762da3b9c7f6bdbf6 MD5 · raw file
- ; 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.ahk
- ; Start gdi+
- If !pToken := Gdip_Startup()
- {
- MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
- ExitApp
- }
- OnExit, Exit
- ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
- Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
- ; Show the window
- ScreenCenter := A_ScreenWidth/2-300
- ScreenBottom := A_ScreenHeight - 164
- Gui, 1: Show, x%ScreenCenter% y30
- ; Get a handle to this window we have created in order to update it later
- hwnd1 := WinExist()
- ; If the image we want to work with does not exist on disk, then download it...
- If !FileExist("zizorz2 bg.png")
- UrlDownloadToFile, http://www.autohotkey.net/~tic/background.png, background.png
- ; Get a bitmap from the image
- pBitmap := Gdip_CreateBitmapFromFile("zizorz2 bg.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, 48, File loading error!, Could not load the image specified
- 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, Height)
- ; 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, Height, 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, ScreenCenter, ScreenBottom, Width, Height)
- ; 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
- ;#######################################################################
- Exit:
- ; gdi+ may now be shutdown on exiting the program
- Gdip_Shutdown(pToken)
- ExitApp
- Return