Jump to content

how do i make a transparent window with borders around it


Recommended Posts

i have been trying and i get a window that isn't transparent and whenever it's dragged over other windows it takes on that windows appearance

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUICreate("", 300, 300, 20, 20, -1, $WS_EX_Transparent)

GUICtrlCreateButton("OK", 30,30, 100, 100)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd
Link to comment
Share on other sites

Why you don't use WinSetTrans()?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUICreate("Example", 300, 300, 20, 20)
GUICtrlCreateButton("OK", 30,30, 100, 100)
WinSetTrans("Example","",127)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

When the words fail... music speaks.

Link to comment
Share on other sites

If I do that and completely set the window to transparent then will i be able to click controls on other windows if its on top?

Edit: I think I answered my own question with testing your code. The answer is yes correct? Thank you andreik.

Edited by Hypertrophy
Link to comment
Share on other sites

I don't understand what you mean Valuater. How does that relate to my question?

Edit: Maybe it would help if I explain what I'm trying to do.

I need to make a transparent window and overlay it on top of another window but I want the transparent window to contain fully visible icons so when it's overlayed on another window you can click on the window like normal but see the icons.

Edited by Hypertrophy
Link to comment
Share on other sites

If I understand. When two windows overlap, you want the window above to be partially transparent

Here are two snippets which will get you mostly there.

Picea

1) Hole

Const $RGN_AND = 1
Const $RGN_OR = 2
Const $RGN_XOR = 3
Const $RGN_DIFF = 4
Const $RGN_COPY = 5

$nada=GUICreate("",500,500,0,0)
_GuiHole($nada,50,50,50,50)
GUISetState()
while 1
    sleep(1000)
WEnd

Func _GuiHole($h_win, $i_x, $i_y, $i_sizew, $i_sizeh)
    Dim $pos, $outer_rgn, $inner_rgn, $wh, $combined_rgn
    $pos = WinGetPos($h_win)
   
    $outer_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", $pos[2], "long", $pos[3])
    $inner_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $i_x, "long", $i_y, "long", $i_x + $i_sizew, "long", $i_y + $i_sizeh)
    $combined_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
    DllCall("gdi32.dll", "long", "CombineRgn", "long", $combined_rgn[0], "long", $outer_rgn[0], "long", $inner_rgn[0], "int", $RGN_DIFF)
    DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $combined_rgn[0], "int", 1)
EndFunc

2) Collision

#include <array.au3>
$Win1 = Run("Notepad.exe")
Sleep(3000)
$Win1 = _WinGetByPID($Win1);get win handle to app window
$Win2 = Run("mspaint.exe")
Sleep(2000)
$Win2 = _WinGetByPID($Win2);get win handle to app window
While 1
   Sleep(10)
   If CheckCollission($Win1, $Win2) Then
      ToolTip("collision")
   Else
      ToolTip("")
   EndIf
WEnd
Func CheckCollission($Win1, $Win2)
  
   $Win1Pos = WinGetPos($Win1)
   $Win2Pos = WinGetPos($Win2)
  
   If _CheckOverlap($Win1Pos, $Win2Pos) Then Return True
   If _CheckOverlap($Win2Pos, $Win1Pos) Then Return True
EndFunc  ;==>CheckCollission
Func _WinGetByPID($iPID)
   Local $aWList = WinList()
   For $iCC = 1 To $aWList[0][0]
      If WinGetProcess($aWList[$iCC][1]) = $iPID And _
            BitAND(WinGetState($aWList[$iCC][1]), 2) Then
         Return $aWList[$iCC][0]
      EndIf
   Next
   Return SetError(1, 0, 0)
EndFunc  ;==>_WinGetByPID
Func _CheckOverlap($ar1, $a2)
   
   
   If _WinAPI_PtInRectEx($ar1[0], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True
   If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True
   If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True
   If _WinAPI_PtInRectEx($ar1[0], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True
   
   
EndFunc  ;==>_CheckOverlap
; Note this_WinAPI_PtInRectEx() Not the same as 1st post.
; Here Width & height used, NOT Bottom right corner position.
; Also, declared $tagREC.  $tagRECT required WinAPI.au3 to be included.
; Author - Malkey
Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight)
Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom"
Local $tRect = DllStructCreate($tagREC)
DllStructSetData($tRect, "Left", $iLeft)
DllStructSetData($tRect, "Top", $iTop)
DllStructSetData($tRect, "Right", $iLeft + $iWidth)
DllStructSetData($tRect, "Bottom", $iTop + $iHeight)
$aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY)
Return $aResult[0] <> 0
EndFunc
Link to comment
Share on other sites

I don't understand what you mean Valuater. How does that relate to my question?

Edit: Maybe it would help if I explain what I'm trying to do.

I need to make a transparent window and overlay it on top of another window but I want the transparent window to contain fully visible icons so when it's overlayed on another window you can click on the window like normal but see the icons.

Exactly... Try ANYGUI.au3

8)

NEWHeader1.png

Link to comment
Share on other sites

I'd listen to Valuator, he's great.

Another alternative is a mask, I think Mr Creator made it

#include <GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WINAPI.au3>
#include <ButtonConstants.au3>

HotKeySet("{ESC}", "QuitApp")

$main_Gui = GUICreate("",30,200,995,140,$WS_POPUP, BitOR($WS_EX_ACCEPTFILES,$WS_EX_TOOLWINDOW,$WS_EX_TOPMOST))
$but1 = GUICtrlCreateButton(" F:\ ", 0, 0,30,30,$BS_ICON)
$but3 = GUICtrlCreateButton(" D:\ ", 0, 100,30,30,$BS_ICON)
$but2 = GUICtrlCreateButton(" C:\ ", 0, 50,30,30,$BS_ICON)
$but4 = GUICtrlCreateButton(" Share", 0, 150,30,30,$BS_ICON)


$masterMask = CreateMasterMask();
AddToMask($masterMask,$main_Gui,$but1);add button to mask
AddToMask($masterMask,$main_Gui,$but2);add button to mask
AddToMask($masterMask,$main_Gui,$but3);add button to mask
AddToMask($masterMask,$main_Gui,$but4);add button to mask
FitMask($masterMask,$main_gui);apply the mask to the window

GUISetState()


While 1    

            $msg = GUIGetMsg()
        Select
            Case $msg = $but1
                Run('f:')
            Case $msg = $but2   
                Run('c:')
            Case $msg = $but3
                Run('d:')
        EndSelect
WEnd

Func CreateMasterMask()
    return DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
EndFunc

Func FitMask($aMask,$hWnd)
    DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $hWnd, "long", $aMask[0], "int", 1)
endfunc

Func AddToMask(ByRef $MM, $hWnd, $ID)
    $pp = ControlGetPos($hWnd,'',$ID)
    Local $Mask1 = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $pp[0], "long", $pp[1], "long", $pp[0] + $pp[2], "long",$pp[1] + $pp[3])
    DllCall("gdi32.dll", "long", "CombineRgn", "long", $MM[0], "long", $Mask1[0], "long", $MM[0],"int",2)
EndFunc


Func QuitApp()
    Exit
EndFunc
Link to comment
Share on other sites

This works on XP. I don't know why.

If you remove any of these three:-

GUISetBkColor($GUI_BKCOLOR_TRANSPARENT,$GUICompDir)

WinSetTrans()

GUICtrlCreatePic(),

and the background is not transparent.

I would be interested to know if this works on other systems.

;
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $GUICompDir, $LabelTrans, $LabelNOTrans, $idBut,$msg
Local $Title = "title"

$GUICompDir = GUICreate($Title, 400, 400, 200, 200,-1,$WS_EX_TOPMOST )
GUISetBkColor($GUI_BKCOLOR_TRANSPARENT,$GUICompDir)
WinSetTrans($GUICompDir, "", 255)

GUICtrlCreatePic("",2, 2, 2, 2)
GUICtrlSetState(-1, $GUI_DISABLE)

$LabelTrans = GUICtrlCreateLabel("BG", 30, 17, 145, 50, $SS_Center)
GUICtrlSetFont($LabelTrans, 12, 800)
GUICtrlSetColor(-1, 0x101010)
GUICtrlSetBkColor($LabelTrans,0xDDDDFF)

$idBut = GUICtrlCreateButton("OK", 30, 130, 100, 100)
GUICtrlSetColor(-1, 0x101010)
GUICtrlSetFont(-1, 10, 800)

$LabelNOTrans = GUICtrlCreateLabel("No BKG", 30, 70, 150, 20)
GUICtrlSetFont($LabelNOTrans, 13, 800)
GUICtrlSetColor($LabelNOTrans, 0x101010)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idBut
            MsgBox(0, "", '"OK" button pressed',0,$GUICompDir)
    EndSwitch
WEnd
;

Edit:The above script has any black, 0x000000, displayed as transparent.

This script uses the green. bmp in AutoIt3 sub-directory to make any green, 0x00FF00, to appear transparent

;
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $GUICompDir, $LabelTrans, $LabelNOTrans, $idBut,$msg
Local $Title = "title"

$GUICompDir = GUICreate($Title, 400, 400, 200, 200,-1,$WS_EX_TOPMOST )
GUISetBkColor(0x00FF00,$GUICompDir)
WinSetTrans($GUICompDir, "", 255)

GUICtrlCreatePic("C:\Program Files\AutoIt3\Examples\GUI\Advanced\Images\Green.bmp",2, 2, 2, 2)
GUICtrlSetState(-1, $GUI_DISABLE)

$LabelTrans = GUICtrlCreateLabel("BG", 30, 17, 145, 50, $SS_Center)
GUICtrlSetFont($LabelTrans, 12, 800)
GUICtrlSetColor(-1, 0x101010)
GUICtrlSetBkColor($LabelTrans,0xDDDDFF)

$idBut = GUICtrlCreateButton("OK", 30, 130, 100, 100)
GUICtrlSetColor(-1, 0x101010)
GUICtrlSetFont(-1, 10, 800)

$LabelNOTrans = GUICtrlCreateLabel("No BKG", 30, 70, 150, 20)
GUICtrlSetFont($LabelNOTrans, 13, 800)
GUICtrlSetColor($LabelNOTrans, 0x101010)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idBut
            MsgBox(0, "", '"OK" button pressed',0,$GUICompDir)
    EndSwitch
WEnd
;
Edited by Malcy
Link to comment
Share on other sites

Why you don't use WinSetTrans()?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUICreate("Example", 300, 300, 20, 20)
GUICtrlCreateButton("OK", 30,30, 100, 100)
WinSetTrans("Example","",127)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Very cool ! Thanks..vm
Link to comment
Share on other sites

Is there any way to combine the code below with anygui functionality because I want to lock my transparent gui onto a window.

#include <GUIConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <WINAPI.au3>

;############# Constants ##########
Global Const $LWA_ALPHA = 0x2
Global Const $LWA_COLORKEY = 0x1

;############# Example ############

#Region - GUI Create
$gui = GUICreate("trans", 300, 400, -1, -1, -1, $WS_EX_LAYERED)
GUICtrlCreateLabel("This is text on a transparent Layered GUI", 10, 10, 200, 20, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetTip(-1, "Click label to drag layered window")
$layButt = GUICtrlCreateButton("Button", 10, 40, 40)
GUISetBkColor(0xABCDEF)
_WinAPI_SetLayeredWindowAttributes($gui, 0x010101)
GUISetState()

$guicontrol = GUICreate("ControlGUI", 300, 400, 100, 100)
$checkTrans = GUICtrlCreateCheckbox("Transparent color 0xABCDEF (Checked) Or 0x010101", 10, 10)
$checkBorder = GUICtrlCreateCheckbox("POPUP-Style", 10, 30)
GUICtrlCreateLabel("Transparency for Layered GUI", 10, 50)
$slidTrans = GUICtrlCreateSlider(10, 70, 200, 30)
GUICtrlSetLimit($slidTrans, 255, 0)
GUICtrlSetData(-1, 255)
GUISetState()
#EndRegion - GUI Create

#Region - GUI SelectLoop
While 1
$extMsg = GUIGetMsg(1)
$msg = $extMsg[0]
Switch $extMsg[1]
Case $guicontrol
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $checkTrans Or $msg = $slidTrans

; Change Attributes of Trans-Color and Window Transparency

If BitAND(GUICtrlRead($checkTrans), $GUI_CHECKED) = $GUI_CHECKED Then
_WinAPI_SetLayeredWindowAttributes($gui, 0xABCDEF, GUICtrlRead($slidTrans))
Else
_WinAPI_SetLayeredWindowAttributes($gui, 0x010101, GUICtrlRead($slidTrans))
EndIf

Case $msg = $checkBorder
If BitAND(GUICtrlRead($checkBorder), $GUI_CHECKED) = $GUI_CHECKED Then
GUISetStyle($WS_POPUP, -1, $gui)
Else
GUISetStyle($GUI_SS_DEFAULT_GUI, -1, $gui)
EndIf
EndSelect
Case $gui
Select
Case $msg = $layButt
Dim $transcolor, $alpha
$info = _WinAPI_GetLayeredWindowAttributes($gui,$transcolor, $alpha)
MsgBox(0, 'Layered GUI', "Button on layered Window Clicked" & @CRLF & "Information about this window: " & @CRLF & _
"Transparent Color: " & $transcolor & @CRLF & _
"Alpha Value: " & $alpha & @CRLF & _
"LWA_COLORKEY: " & (BitAND($info,$LWA_COLORKEY)=$LWA_COLORKEY) & @CRLF & _
"LWA_ALPHA: " & (BitAND($info,$LWA_ALPHA)=$LWA_ALPHA) )
Case $msg = $GUI_EVENT_CLOSE
Exit MsgBox(0, '', "Close from Layered GUI")
EndSelect
EndSwitch
WEnd
#EndRegion - GUI SelectLoop

;############# EndExample #########

;===============================================================================
;
; Function Name: _WinAPI_SetLayeredWindowAttributes
; Description:: Sets Layered Window Attributes:) See MSDN for more informaion
; Parameter(s):
; $hwnd - Handle of GUI to work on
; $i_transcolor - Transparent color
; $Transparency - Set Transparancy of GUI
; $isColorRef - If True, $i_transcolor is a COLORREF( 0x00bbggrr ), else an RGB-Color
; Requirement(s): Layered Windows
; Return Value(s): Success: 1
; Error: 0
; @error: 1 to 3 - Error from DllCall
; @error: 4 - Function did not succeed - use
; _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
; Author(s): Prog@ndy
;
; Link : @@MsdnLink@@ SetLayeredWindowAttributes
; Example : Yes
;===============================================================================
;
Func _WinAPI_SetLayeredWindowAttributes($hwnd, $i_transcolor, $Transparency = 255, $dwFlages = 0x03, $isColorRef = False)
; #############################################
; You are NOT ALLOWED to remove the following lines
; Function Name: _WinAPI_SetLayeredWindowAttributes
; Author(s): Prog@ndy
; #############################################
If $dwFlages = Default Or $dwFlages = "" Or $dwFlages < 0 Then $dwFlages = 0x03

If Not $isColorRef Then
$i_transcolor = Hex(String($i_transcolor), 6)
$i_transcolor = Execute('0x00' & StringMid($i_transcolor, 5, 2) & StringMid($i_transcolor, 3, 2) & StringMid($i_transcolor, 1, 2))
EndIf
Local $Ret = DllCall("user32.dll", "int", "SetLayeredWindowAttributes", "hwnd", $hwnd, "long", $i_transcolor, "byte", $Transparency, "long", $dwFlages)
Select
Case @error
Return SetError(@error, 0, 0)
Case $Ret[0] = 0
Return SetError(4, _WinAPI_GetLastError(), 0)
Case Else
Return 1
EndSelect
EndFunc ;==>_WinAPI_SetLayeredWindowAttributes

;===============================================================================
;
; Function Name: _WinAPI_GetLayeredWindowAttributes
; Description:: Gets Layered Window Attributes:) See MSDN for more informaion
; Parameter(s):
; $hwnd - Handle of GUI to work on
; $i_transcolor - Returns Transparent color ( dword as 0x00bbggrr or string "0xRRGGBB")
; $Transparency - Returns Transparancy of GUI
; $isColorRef - If True, $i_transcolor will be a COLORREF( 0x00bbggrr ), else an RGB-Color
; Requirement(s): Layered Windows
; Return Value(s): Success: Usage of LWA_ALPHA and LWA_COLORKEY (use BitAnd)
; Error: 0
; @error: 1 to 3 - Error from DllCall
; @error: 4 - Function did not succeed
; - use _WinAPI_GetLastErrorMessage or _WinAPI_GetLastError to get more information
; - @extended contains _WinAPI_GetLastError
; Author(s): Prog@ndy
;
; Link : @@MsdnLink@@ GetLayeredWindowAttributes
; Example : Yes
;===============================================================================
;
Func _WinAPI_GetLayeredWindowAttributes($hwnd, ByRef $i_transcolor, ByRef $Transparency,$asColorRef = False)
; #############################################
; You are NOT ALLOWED to remove the following lines
; Function Name: _WinAPI_SetLayeredWindowAttributes
; Author(s): Prog@ndy
; #############################################
$i_transcolor = -1
$Transparency = -1
Local $Ret = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $hwnd, "long*", $i_transcolor, "byte*", $Transparency, "long*", 0)
Select
Case @error
Return SetError(@error, 0, 0)
Case $Ret[0] = 0
Return SetError(4, _WinAPI_GetLastError(), 0)
Case Else
If Not $asColorRef Then
$Ret[2] = Hex(String($Ret[2]), 6)
$Ret[2] = '0x' & StringMid($Ret[2], 5, 2) & StringMid($Ret[2], 3, 2) & StringMid($Ret[2], 1, 2)
EndIf
$i_transcolor = $Ret[2]
$Transparency = $Ret[3]
Return $Ret[4]
EndSelect
EndFunc ;==>_WinAPI_GetLayeredWindowAttributes
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...