Transparent in Rollout Bgcolor


#1

Good evening everyone.
I know that there is a command “bgcolor:[r,g,b]” to put in createDialog, but I would like to choose a transparent color or another method to make the background of my maxscript transparent.


#2

there is no easy solution for this using pure MXS.
the best and easiest would be to use .NET or WPF windows.


#3

I think that Serejah posted this on the forum, but I can’t find the thread

global WindowTransparency =
(
	source = "using System;\n"
	source += "using System.Runtime.InteropServices;\n"
	source += "class WindowTransparency\n"
	source += "{\n"
	source += " [DllImport(\"user32.dll\")]\n"
	source += " public static extern Int32 GetWindowLong(IntPtr hWnd, Int32 index);\n"
	source += " [DllImport(\"user32.dll\")]\n"
	source += " public static extern Int32 SetWindowLong(IntPtr hWnd, Int32 index, Int32 newVal);\n"
	source += " [DllImport(\"user32.dll\")]\n"
	source += " public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,byte bAlpha, uint dwFlags);\n"
	source += "}\n"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)

	assembly = compilerResults.CompiledAssembly
	assembly.CreateInstance "WindowTransparency"
)


fn SetWindowOpacity hwnd opacity =
(
	local GWL_EXSTYLE   = -20
	local WS_EX_LAYERED = 0x80000
	local LWA_ALPHA     = 0x2 
	local LWA_COLORKEY  = 0x1
	
	local hwnd = if not isKindOf hwnd dotNetObject then dotNetObject "System.IntPtr" hwnd else hwnd
	local windowStyle = ::WindowTransparency.GetWindowLong hwnd GWL_EXSTYLE
		  
	if (bit.and windowStyle (WS_EX_LAYERED + LWA_ALPHA + LWA_COLORKEY)) == 0 do
	(	
		windowStyle = bit.xor windowStyle WS_EX_LAYERED	
		::WindowTransparency.SetWindowLong hwnd GWL_EXSTYLE windowStyle		
	)
	
	-- opacity range 0 - 255
	opacity = int (amin (amax 0 opacity) 255)
	
	/*
	BOOL WINAPI SetLayeredWindowAttributes(
	  _In_ HWND     hwnd,
	  _In_ COLORREF crKey,
	  _In_ BYTE     bAlpha,
	  _In_ DWORD    dwFlags
	);
	*/
	
	-- set opacity by level
    ::WindowTransparency.SetLayeredWindowAttributes hwnd 0 opacity LWA_ALPHA
)


fn SetWindowColorKey hwnd colorkey =
(
	local GWL_EXSTYLE   = -20
	local WS_EX_LAYERED = 0x80000
	local LWA_ALPHA     = 0x2 
	local LWA_COLORKEY  = 0x1
	
	local hwnd = if not isKindOf hwnd dotNetObject then dotNetObject "System.IntPtr" hwnd else hwnd
	local windowStyle = ::WindowTransparency.GetWindowLong hwnd GWL_EXSTYLE
		  
	if (bit.and windowStyle (WS_EX_LAYERED + LWA_ALPHA + LWA_COLORKEY)) == 0 do
	(	
		windowStyle = bit.xor windowStyle WS_EX_LAYERED	
		::WindowTransparency.SetWindowLong hwnd GWL_EXSTYLE windowStyle		
	)
	
	color_as_int = (int colorkey.r) + (bit.shift (int colorkey.g) 8) + (bit.shift (int colorkey.b) 16)
	
	/*
	BOOL WINAPI SetLayeredWindowAttributes(
	  _In_ HWND     hwnd,
	  _In_ COLORREF crKey,
	  _In_ BYTE     bAlpha,
	  _In_ DWORD    dwFlags
	);
	*/
	-- set opacity by color key
    ::WindowTransparency.SetLayeredWindowAttributes hwnd color_as_int 0 LWA_COLORKEY	

)

try (destroydialog X ) catch ()
rollout X "" (

	button btn "button"
	spinner spn "opacity" range:[0,255,128] 
		
	on x open do SetWindowColorKey x.hwnd blue
	
	on btn pressed do SetWindowColorKey x.hwnd blue
	on spn changed val do SetWindowOpacity x.hwnd spn.value

)
createDialog X bgcolor:blue

#4

Wow hahahahaha. Thats nice. Thank you very much.

Could you make this more compact by taking out that spinner and button?

Leaving only the part that applies in the Rollout 100% background transparency?

I’m afraid to mess with your code and break it, since I don’t understand at this level of “hwnd”.


#5

#6

My advice is to never use a solution that you don’t understand.


#7

I don’t understand some my own code after a couple of years :wink:


#8

well… let’s change it up a little bit: never use someone else’s solution that you don’t understand :stuck_out_tongue_winking_eye:


#9

I understand what Denis meant, no problem. I really think I need to understand what I’m doing, but sometimes I use functions with very specific tasks. How to get an image in base 64 code and convert it to dot net image. This is something I don’t feel the slightest need to understand how it works. I just understand what it does and how to apply it to my code. But I agree that you need to know the minimum to use some code. You’ve always been helping me by giving code suggestions and it really helps a lot to understand how to find solutions to logical problems. And soon I will be releasing my scripts in a package, you can be sure that I will come here to share and make them available to you.