Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

[SOLVED] Gui opacity


  • Please log in to reply
16 replies to this topic
Roninz
  • Guests
  • Last active:
  • Joined: --
Hey guys, i got 2 questions..
1. Is there a way i can change opacity of a gui?
2. How to do it when mouse hovers over an area?
2nd one is jst for effects. If anyone knows either, plz let me know.

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
There is nothing specific to an AHk Gui to change opacity/tranparency.

However, there is a method to change the transparency of windows, but AHk an external.
<!-- m -->http://www.autohotke...ands/WinSet.htm<!-- m -->
WinSet, Transparent, N, WinTitle

Roninz
  • Guests
  • Last active:
  • Joined: --
Ok, but how do i trigger it when cursor is hovering over an image?

Ronins
  • Members
  • 120 posts
  • Last active: Apr 18 2016 10:54 PM
  • Joined: 11 Apr 2012
ok, i found this code. It makes GUI transparent, and is internal AHK code.

WinSet, Transparent, [0-255], [wintitle]

But, it makes whole GUI transparent. Is it possible i can make only GUI background transparent, and the controls within visible?

Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
Try this

#Persistent
#SingleInstance, Force

; Your controls
Gui, Add, Button, , AAAAAAAA
Gui, Add, Button, , BBBBBBBB
Gui, Add, Button, , CCCCCCCC

; Removes the Border and Task bar icon
Gui, +ToolWindow -Caption 
; can be any colour but it's good to use a color that is NOT present in ur GUI
Gui, Color, 000111
Gui, Show, W400 H300, Test

WinSet, Transcolor, 000111, Test
Return

Ahk_L (Unicode 32-bit)


Ronins
  • Members
  • 120 posts
  • Last active: Apr 18 2016 10:54 PM
  • Joined: 11 Apr 2012
WHOA!!!! Exactly what i needed!!! Super Thanx man!!!

EDIT: how do i move it with respect to other window? I mean, i've placed it over some other GUI. And in this case, there is no wintitle for transparent GUI, so how can i move it using winmove and other GUI? I plan to use this as GUI, 2

Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
Okay, since this window has no "Title", you can get its ID like so:

#Persistent
#SingleInstance, Force

Gui, Add, Button, , AAAAAAAA
Gui, Add, Button, , BBBBBBBB
Gui, Add, Button, , CCCCCCCC

Gui, +ToolWindow -Caption [color=#FF0000]+LastFound[/color]
[color=#FF0000]GuiID:=WinExist()[/color]
Gui, Color, 000111
Gui, Show, W400 H300, Test

WinSet, Transcolor, 000111, Test
[color=#FF0000]MsgBox, Press OK to move the Window which has this ID: %GuiID%
WinMove, ahk_id %GuiID%, , 0, 0[/color]
Return

Ahk_L (Unicode 32-bit)


Ronins
  • Members
  • 120 posts
  • Last active: Apr 18 2016 10:54 PM
  • Joined: 11 Apr 2012
ok, here is the link

<!-- m -->https://ahknet.autoh...om/paste/9xaoi1<!-- m -->

i am using example 7(?) of GDIP standard library, and rest, u know... M jst scratching the hell outta my head to make it work

I've commented where i've added stuffs, and it needs gdip.dll (which is gdip.ahk actually)

Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
Okay here is a working example that moves the transparent window when the Main window is moved:

#Persistent
#SingleInstance, Force
SetBatchLines, -1
SetWinDelay, -1

[color=#00BF00]; Main window/GUI[/color]
Gui 1: Add, Text, , Move this window to move the Transparent background window
Gui 1: +LastFound
GuiID_1:=WinExist()
Gui 1: Show
[color=#FF0000]OnMessage(0x03,"WM_Move")[/color]

[color=#00BF00]; Transparent Window[/color]
Gui 2: Add, Button, , AAAAAAAA
Gui 2: +ToolWindow -Caption +LastFound
GuiID_2:=WinExist()
Gui 2: Color, 000111
Gui 2: Show, NA
WinSet, Transcolor, 000111, ahk_id %GuiID_2%
Return

[color=#FF0000]WM_Move() {
	Global
	WinGetPos, X, Y, W, , ahk_id %GuiID_1%
	WinMove, ahk_id %GuiID_2%, , % X+W, % Y ; X and Y is the new position of the main window. Adjust the + to position the transparent window properly to the main window
}[/color]

Is this what you need?

Ahk_L (Unicode 32-bit)


Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
Okay this is a working example in the code you provided. When you move the Black Semi-transparent window, the other window will Move:

; gdi+ ahk tutorial 8 written by tic (Tariq Porter)
; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
;
; Tutorial to write text onto a gui
 
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
SetWinDelay, -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
 
Width := 300, Height := 200

Gui, 1: +E0x80000 +LastFound -Caption
; Show the window
; =============== Stuff i added=======================

Gui, 1: Show
WinGet, abc, PID
MsgBox, %abc%
 
Gui, 2: +Owner +AlwaysOnTop
Gui, 2: Add, text,, asdecae
Gui, 2: Show, , [color=#FF0000]TestWindow[/color]

; ========================= ENDS HERE WHAT I ADDED=========================
 
; Get a handle to this window we have created in order to update it later
hwnd1 := WinExist() ; [color=#FF0000]hwnd1 is the Gui ID for the transparent window[/color]
 
; 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
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)
 
; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
Gdip_SetSmoothingMode(G, 4)
 
; Create a partially transparent, black brush (ARGB = Transparency, red, green, blue) to draw a rounded rectangle with
pBrush := Gdip_BrushCreateSolid(0xaa000000)
 
; Fill the graphics of the bitmap with a rounded rectangle using the brush created
; Filling the entire graphics - from coordinates (0, 0) the entire width and height
; The last parameter (20) is the radius of the circles used for the rounded corners
Gdip_FillRoundedRectangle(G, pBrush, 0, 0, Width, Height, 20)
 
; Delete the brush as it is no longer needed and wastes memory
Gdip_DeleteBrush(pBrush)
 
; We can specify the font to use. Here we use Arial as most systems should have this installed
Font = Arial
; Next we can check that the user actually has the font that we wish them to use
; If they do not then we can do something about it. I choose to give a wraning and exit!
If !hFamily := Gdip_FontFamilyCreate(Font)
{
   MsgBox, 48, Font error!, The font you have specified does not exist on the system
   ExitApp
}
; Delete font family as we now know the font does exist
Gdip_DeleteFontFamily(hFamily)
 
; There are a lot of things to cover with the function Gdip_TextToGraphics
 
; The 1st parameter is the graphics we wish to use (our canvas)
 
; The 2nd parameter is the text we wish to write. It can include new lines `n
 
; The 3rd parameter, the options are where all the action takes place...
; You can write literal x and y coordinates such as x20 y50 which would place the text at that position in pixels
; or you can include the last 2 parameters (Width and Height of the Graphics we will use) and then you can use x10p
; which will place the text at 10% of the width and y30p which is 30% of the height
; The same percentage marker may be used for width and height also, so w80p makes the bounding box of the rectangle the text
; will be written to 80% of the width of the graphics. If either is missed (as I have missed height) then the height of the bounding
; box will be made to be the height of the graphics, so 100%
 
; Any of the following words may be used also: Regular,Bold,Italic,BoldItalic,Underline,Strikeout to perform their associated action
 
; To justify the text any of the following may be used: Near,Left,Centre,Center,Far,Right with different spelling of words for convenience
 
; The rendering hint (the quality of the antialiasing of the text) can be specified with r, whose values may be:
; SystemDefault = 0
; SingleBitPerPixelGridFit = 1
; SingleBitPerPixel = 2
; AntiAliasGridFit = 3
; AntiAlias = 4
 
; The size can simply be specified with s
 
; The colour and opacity can be specified for the text also by specifying the ARGB as demonstrated with other functions such as the brush
; So cffff0000 would make a fully opaque red brush, so it is: cARGB (the literal letter c, follwed by the ARGB)
 
; The 4th parameter is the name of the font you wish to use
 
; As mentioned previously, you don not need to specify the last 2 parameters, the width and height, unless
; you are planning on using the p option with the x,y,w,h to use the percentage
Options = x10p y30p w80p Centre cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(G, "Tutorial 8`n`nThank you for trying this example", Options, Font, 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
; With some simple maths we can place the gui in the centre of our primary monitor horizontally and vertically at the specified heigth and width
UpdateLayeredWindow(hwnd1, hdc, (A_ScreenWidth-Width)//2, (A_ScreenHeight-Height)//2, Width, Height)
 
; By placing this OnMessage here. The function WM_LBUTTONDOWN will be called every time the user left clicks on the gui
OnMessage(0x201, "WM_LBUTTONDOWN")
[color=#FF0000]OnMessage(0x03,"WM_Move")[/color] 
 
; 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)
Return
 
;#######################################################################
 
; This function is called every time the user clicks on the gui
; The PostMessage will act on the last found window (this being the gui that launched the subroutine, hence the last parameter not being needed)

[color=#FF0000]WM_Move(wParam, lParam, msg, hwnd) {
	Global
	If (hwnd = hwnd1)
	{
		WinGetPos, X, Y, W, , ahk_id %hwnd1%
		WinMove, TestWindow, , % X+10, % Y+10
	}
}[/color]

WM_LBUTTONDOWN()
{
   PostMessage, 0xA1, 2
}
 
;#######################################################################
 
GuiClose:
Exit:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp
Return

Ahk_L (Unicode 32-bit)


Ronins
  • Members
  • 120 posts
  • Last active: Apr 18 2016 10:54 PM
  • Joined: 11 Apr 2012
okkkk!!! thanks to your code, i figured out that with -caption, wintitle property is not destroyed. Real thanx man. A BIG THANKS. BTW, how do i mark this thread as solved now??

Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
To mark post as Solved: Go back to your starting post at the top. Click Edit and add "[SOLVED]" at the start of the Subject/Title

And I'm glad I could help :) Let me know if I can help again

Ahk_L (Unicode 32-bit)


Roninz
  • Guests
  • Last active:
  • Joined: --
I almost forgot to ask. How can i trigger change in opacity when my pointer is over the base gui? Only way i can think of is mousegetpos.
Also, do u know how do i make this effect programatically such that when i launch a gui, or in this case, the upper gui, editbox will get focus automatically?

Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
This will call a function when the mouse is on the Black Semi-Transparent window

; gdi+ ahk tutorial 8 written by tic (Tariq Porter)
; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
;
; Tutorial to write text onto a gui
 
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
SetWinDelay, -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
 
Width := 300, Height := 200

Gui, 1: +E0x80000 +LastFound -Caption
; Show the window
; =============== Stuff i added=======================

Gui, 1: Show
WinGet, abc, PID
MsgBox, %abc%
 
Gui, 2: +Owner +AlwaysOnTop
Gui, 2: Add, text,, asdecae
Gui, 2: Show, , TestWindow

; ========================= ENDS HERE WHAT I ADDED=========================
 
; Get a handle to this window we have created in order to update it later
hwnd1 := WinExist() ; hwnd1 is the Gui ID for the transparent window
 
; 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
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)
 
; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
Gdip_SetSmoothingMode(G, 4)
 
; Create a partially transparent, black brush (ARGB = Transparency, red, green, blue) to draw a rounded rectangle with
pBrush := Gdip_BrushCreateSolid(0xaa000000)
 
; Fill the graphics of the bitmap with a rounded rectangle using the brush created
; Filling the entire graphics - from coordinates (0, 0) the entire width and height
; The last parameter (20) is the radius of the circles used for the rounded corners
Gdip_FillRoundedRectangle(G, pBrush, 0, 0, Width, Height, 20)
 
; Delete the brush as it is no longer needed and wastes memory
Gdip_DeleteBrush(pBrush)
 
; We can specify the font to use. Here we use Arial as most systems should have this installed
Font = Arial
; Next we can check that the user actually has the font that we wish them to use
; If they do not then we can do something about it. I choose to give a wraning and exit!
If !hFamily := Gdip_FontFamilyCreate(Font)
{
   MsgBox, 48, Font error!, The font you have specified does not exist on the system
   ExitApp
}
; Delete font family as we now know the font does exist
Gdip_DeleteFontFamily(hFamily)
 
; There are a lot of things to cover with the function Gdip_TextToGraphics
 
; The 1st parameter is the graphics we wish to use (our canvas)
 
; The 2nd parameter is the text we wish to write. It can include new lines `n
 
; The 3rd parameter, the options are where all the action takes place...
; You can write literal x and y coordinates such as x20 y50 which would place the text at that position in pixels
; or you can include the last 2 parameters (Width and Height of the Graphics we will use) and then you can use x10p
; which will place the text at 10% of the width and y30p which is 30% of the height
; The same percentage marker may be used for width and height also, so w80p makes the bounding box of the rectangle the text
; will be written to 80% of the width of the graphics. If either is missed (as I have missed height) then the height of the bounding
; box will be made to be the height of the graphics, so 100%
 
; Any of the following words may be used also: Regular,Bold,Italic,BoldItalic,Underline,Strikeout to perform their associated action
 
; To justify the text any of the following may be used: Near,Left,Centre,Center,Far,Right with different spelling of words for convenience
 
; The rendering hint (the quality of the antialiasing of the text) can be specified with r, whose values may be:
; SystemDefault = 0
; SingleBitPerPixelGridFit = 1
; SingleBitPerPixel = 2
; AntiAliasGridFit = 3
; AntiAlias = 4
 
; The size can simply be specified with s
 
; The colour and opacity can be specified for the text also by specifying the ARGB as demonstrated with other functions such as the brush
; So cffff0000 would make a fully opaque red brush, so it is: cARGB (the literal letter c, follwed by the ARGB)
 
; The 4th parameter is the name of the font you wish to use
 
; As mentioned previously, you don not need to specify the last 2 parameters, the width and height, unless
; you are planning on using the p option with the x,y,w,h to use the percentage
Options = x10p y30p w80p Centre cbbffffff r4 s20 Underline Italic
Gdip_TextToGraphics(G, "Tutorial 8`n`nThank you for trying this example", Options, Font, 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
; With some simple maths we can place the gui in the centre of our primary monitor horizontally and vertically at the specified heigth and width
UpdateLayeredWindow(hwnd1, hdc, (A_ScreenWidth-Width)//2, (A_ScreenHeight-Height)//2, Width, Height)
 
; By placing this OnMessage here. The function WM_LBUTTONDOWN will be called every time the user left clicks on the gui
OnMessage(0x201, "WM_LBUTTONDOWN")
OnMessage(0x03,"WM_Move")
[color=#FF0000]OnMessage(0x200,"ChangeInOpacity")[/color]
 
; 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)
Return
 
;#######################################################################
 
; This function is called every time the user clicks on the gui
; The PostMessage will act on the last found window (this being the gui that launched the subroutine, hence the last parameter not being needed)

WM_Move(wParam, lParam, msg, hwnd) {
   Global
   If (hwnd = hwnd1)
   {
      WinGetPos, X, Y, W, , ahk_id %hwnd1%
      WinMove, TestWindow, , % X+10, % Y+10
   }
}

WM_LBUTTONDOWN()
{
   PostMessage, 0xA1, 2
}

[color=#FF0000]ChangeInOpacity() {
	ToolTip, Mouse is over base GUI
	SetTimer, CloseToolTip, -100 ; This to remove the tooltip if you decide to use a tooltip for you code
}

CloseToolTip:
ToolTip
Return[/color]
 
;#######################################################################
 
GuiClose:
Exit:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp
Return

Ahk_L (Unicode 32-bit)


Ronins
  • Members
  • 120 posts
  • Last active: Apr 18 2016 10:54 PM
  • Joined: 11 Apr 2012
ok, thx a lot bro