Layerd GUI(GDI+) add buttons?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Layerd GUI(GDI+) add buttons?

09 Jul 2015, 11:23

How to add control to layered GUI?
I want to make gui using GDI+ Librery and then add Pictures(Gui Pictures for buttons).
This is code, another words how to add controls like Pictures to it:

Code: Select all

; 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 +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()

; If the image we want to work with does not exist on disk, then download it...
If !FileExist("background.png")
UrlDownloadToFile, http://www.autohotkey.net/~tic/background.png, background.png

; 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, 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//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

;#######################################################################

Exit:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp
Return
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Layerd GUI(GDI+) add buttons?

09 Jul 2015, 21:15

A layered window gets its entire visual representation from a bitmap. Even if you add a control, you will only see the bitmap. You've used Gdip_DrawImage to draw the GDI+ bitmap (pBitmap / background.png) onto the GDI bitmap (hbm) via the GDI+ graphics object (G); you can use the same functions to load and draw another bitmap, such as a button graphic.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Layerd GUI(GDI+) add buttons?

10 Jul 2015, 02:02

lexikos, thank you another words I have to push buttons into the GUI the same way I sent background.png through GDI+ to it?
can you help me, the thing is that I need background with shadows on edges(that will need transparency) and good aliasing, I do not need that stuff on buttons that will be lay on it, so may be no need to push them this way throug GDI+ may be just create another GUI with transparent background that will be lay above GDI UI and put Pics on this for buttons without GDI+? So I'll have 2 GUIs one with GDI+ Background and one for simple Pics?
What will be the easiest and most right way to accomplish this task?
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Layerd GUI(GDI+) add buttons?

10 Jul 2015, 02:55

Whatever works for you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 307 guests