PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. ; gdi+ ahk tutorial 3 written by tic (Tariq Porter)
  2. ; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
  3. ;
  4. ; Tutorial to take make a gui from an existing image on disk
  5. ; For the example we will use png as it can handle transparencies. The image will also be halved in size
  6. #SingleInstance, Force
  7. #NoEnv
  8. SetBatchLines, -1
  9. ; Uncomment if Gdip.ahk is not in your standard library
  10. ;#Include, Gdip.ahk
  11. ; Start gdi+
  12. If !pToken := Gdip_Startup()
  13. {
  14. MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  15. ExitApp
  16. }
  17. OnExit, Exit
  18. ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
  19. Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
  20. ; Show the window
  21. ScreenCenter := A_ScreenWidth/2-300
  22. ScreenBottom := A_ScreenHeight - 164
  23. Gui, 1: Show, x%ScreenCenter% y30
  24. ; Get a handle to this window we have created in order to update it later
  25. hwnd1 := WinExist()
  26. ; If the image we want to work with does not exist on disk, then download it...
  27. If !FileExist("zizorz2 bg.png")
  28. UrlDownloadToFile, http://www.autohotkey.net/~tic/background.png, background.png
  29. ; Get a bitmap from the image
  30. pBitmap := Gdip_CreateBitmapFromFile("zizorz2 bg.png")
  31. ; Check to ensure we actually got a bitmap from the file, in case the file was corrupt or some other error occured
  32. If !pBitmap
  33. {
  34. MsgBox, 48, File loading error!, Could not load the image specified
  35. ExitApp
  36. }
  37. ; Get the width and height of the bitmap we have just created from the file
  38. ; This will be the dimensions that the file is
  39. Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
  40. ; 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
  41. ; We are creating this "canvas" at half the size of the actual image
  42. ; We are halving it because we want the image to show in a gui on the screen at half its dimensions
  43. hbm := CreateDIBSection(Width, Height)
  44. ; Get a device context compatible with the screen
  45. hdc := CreateCompatibleDC()
  46. ; Select the bitmap into the device context
  47. obm := SelectObject(hdc, hbm)
  48. ; Get a pointer to the graphics of the bitmap, for use with drawing functions
  49. G := Gdip_GraphicsFromHDC(hdc)
  50. ; We do not need SmoothingMode as we did in previous examples for drawing an image
  51. ; Instead we must set InterpolationMode. This specifies how a file will be resized (the quality of the resize)
  52. ; Interpolation mode has been set to HighQualityBicubic = 7
  53. Gdip_SetInterpolationMode(G, 7)
  54. ; DrawImage will draw the bitmap we took from the file into the graphics of the bitmap we created
  55. ; We are wanting to draw the entire image, but at half its size
  56. ; Coordinates are therefore taken from (0,0) of the source bitmap and also into the destination bitmap
  57. ; The source height and width are specified, and also the destination width and height (half the original)
  58. ; Gdip_DrawImage(pGraphics, pBitmap, dx, dy, dw, dh, sx, sy, sw, sh, Matrix)
  59. ; d is for destination and s is for source. We will not talk about the matrix yet (this is for changing colours when drawing)
  60. Gdip_DrawImage(G, pBitmap, 0, 0, Width, Height, 0, 0, Width, Height)
  61. ; 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
  62. ; So this will position our gui at (0,0) with the Width and Height specified earlier (half of the original image)
  63. UpdateLayeredWindow(hwnd1, hdc, ScreenCenter, ScreenBottom, Width, Height)
  64. ; Select the object back into the hdc
  65. SelectObject(hdc, obm)
  66. ; Now the bitmap may be deleted
  67. DeleteObject(hbm)
  68. ; Also the device context related to the bitmap may be deleted
  69. DeleteDC(hdc)
  70. ; The graphics may now be deleted
  71. Gdip_DeleteGraphics(G)
  72. ; The bitmap we made from the image may be deleted
  73. Gdip_DisposeImage(pBitmap)
  74. Return
  75. ;#######################################################################
  76. Exit:
  77. ; gdi+ may now be shutdown on exiting the program
  78. Gdip_Shutdown(pToken)
  79. ExitApp
  80. Return