Color + png file on button?


#1

hey there!

I’m looking for a way to change the color of my buttons on the fly. I thought of having a label of a specific color and put ontop a .png file with alpha, so i could change the button color by changing the color of the label. The problem is I have no idea how to do it in dotnet.

Could you give me an example please?
Thank you


#2

Based on your description , there’s no problem .
you can post what you tried with error
if you havn’t try , search “dotnet” there are lots of examples how to start dotnet


#3
rollout testPNG "Testing png" (

	bitmap BitmapImage bitmap:(bitmap 80 80 color:blue)
	
	local img = (dotNetClass "System.Drawing.Image")
	local mcolor = (dotnetclass "System.drawing.color")
	
	local pathImg_Config = img.fromFile("C:/Users/Jen/Desktop/test/a.png")
	
	dotnetcontrol button_Config "Label" pos:[55,30] width:36 height:36	
	
	on testPNG open do
	(
		button_Config.image = pathImg_Config
	)
	
)
createDialog testPNG

No error, just no alpha
Capture

png used: https://www.freepng.fr/png-zmnkmi/


#4

controls’ default state is not Transport , and this is a common problem
to achieve the result , you must rewrite the label

fn TransparentLabelfn =
(
    source = "using System.Windows.Forms;
        class TransparentLabel : Label
        {
            static void Main()
            {
            }
            protected override CreateParams CreateParams
            {
                get
                {
                    this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
                    const int WS_EX_TRANSPARENT = 0x20;
                    CreateParams result = base.CreateParams;
                    result.ExStyle = result.ExStyle | WS_EX_TRANSPARENT;
                    return result;
                }
            }
            protected override void OnPaintBackground(PaintEventArgs pevent)
            {
            }
        }"
    csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
    compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
    CompilerParams.ReferencedAssemblies.Add("System.dll")
    compilerParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
    CompilerParams.GenerateExecutable = true
    CompilerParams.GenerateInMemory = true
    compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
    compilerResults.CompiledAssembly
)
TransparentLabelfn()

run mxs up first , then change dotnetcontrol button_Config "Label" pos:[55,30] width:36 height:36 to dotnetcontrol button_Config "TransparentLabel" pos:[55,30] width:36 height:36
now , you label truly transparent
2020-01-29_5-23-58


#5

Thank you, it works like a charm but there’s an issue about it: you cannot click on the button.

See this code:

fn TransparentLabelfn =
(
source = “using System.Windows.Forms;
class TransparentLabel : Label
{
static void Main()
{
}
protected override CreateParams CreateParams
{
get
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
const int WS_EX_TRANSPARENT = 0x20;
CreateParams result = base.CreateParams;
result.ExStyle = result.ExStyle | WS_EX_TRANSPARENT;
return result;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
}”
csharpProvider = dotnetobject “Microsoft.CSharp.CSharpCodeProvider”
compilerParams = dotnetobject “System.CodeDom.Compiler.CompilerParameters”
CompilerParams.ReferencedAssemblies.Add(“System.dll”)
compilerParams.ReferencedAssemblies.Add(“System.Windows.Forms.dll”)
CompilerParams.GenerateExecutable = true
CompilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
compilerResults.CompiledAssembly
)
TransparentLabelfn()

rollout test “test”
(

local colors = DotNetClass "System.Drawing.Color" 

local img = (dotNetClass “System.Drawing.Image”)

dotnetcontrol test “label” width:100 height:100 pos:[0,0]
dotnetcontrol button_Config “TransparentLabel” pos:[0,0] width:100 height:100

local pathImg_Config = img.fromFile(“C:/Users/Jen/Desktop/b/d.png”)

on test open do 
(

button_Config.image = pathImg_Config
test.backColor = colors.fromArgb 0 255 255
)

on button_Config click do
messagebox(“yes”)

)
createdialog test

if the color and the button are at the same position the click event doesn’t work. It’s like the color is on top of the button. If I offset the position of the button of the right for example: the part “outisde of the color boundaries” is clickable.


#6

what about using label’s click event?


#7

you need exchange this 2 line


#8

well the button is now clickable but i can’t see my png image, only the color.


#9

for your requirement , you’d better use dynamic method

rollout test "test"
(
	local colors = DotNetClass "System.Drawing.Color" 
	local img = (dotNetClass "System.Drawing.Image")
	dotnetcontrol test "label" width:100 height:100 pos:[0,0]
	local pathImg_Config = img.fromFile("C:/Users/Jen/Desktop/b/d.png")
	fn onclick s e =
	(
		messagebox("yes")
	)
	on test open do 
	(
		button_Config=dotnetobject  "TransparentLabel"
		button_Config.dock=button_Config.dock.fill
		button_Config.image = pathImg_Config
		test.controls.add button_Config
		dotNet.addEventHandler button_Config "click" onclick
		test.backColor = colors.fromArgb 0 255 255
	)
)
createdialog test

#10

Well it works but I don’t think it’s the best way for what I want to do: I’d like to have multipe buttons next to each other. How do I split the events for each of them?

fn onclick s e

this only refers to every click in the rollout, right?
thanks for your help


#11

you can definite functions for every button or case in function


#12

dotnet control have a special property “.tag” , you can save anything init


#13

2020-01-29_21-30-16
this is my toolbar , every button use the same click event , i save the index and other info in .tag , then use them to do different operations


#14

I tried something. Guess it doesn’t work because the message “button 1” keeps showing up even if i click on the second one:

fn TransparentLabelfn =
(
source = “using System.Windows.Forms;
class TransparentLabel : Label
{
static void Main()
{
}
protected override CreateParams CreateParams
{
get
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
const int WS_EX_TRANSPARENT = 0x20;
CreateParams result = base.CreateParams;
result.ExStyle = result.ExStyle | WS_EX_TRANSPARENT;
return result;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
}”
csharpProvider = dotnetobject “Microsoft.CSharp.CSharpCodeProvider”
compilerParams = dotnetobject “System.CodeDom.Compiler.CompilerParameters”
CompilerParams.ReferencedAssemblies.Add(“System.dll”)
compilerParams.ReferencedAssemblies.Add(“System.Windows.Forms.dll”)
CompilerParams.GenerateExecutable = true
CompilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
compilerResults.CompiledAssembly
)
TransparentLabelfn()

rollout test “test” width:400
(
local colors = DotNetClass “System.Drawing.Color”
local img = (dotNetClass “System.Drawing.Image”)

dotnetcontrol test “label” width:100 height:100 pos:[0,0]
local pathImg_Config = img.fromFile(“C:/Users/Jen/Desktop/b/d.png”)

dotnetcontrol test2 “label” width:100 height:100 pos:[120,0]
local pathImg_Config2 = img.fromFile(“C:/Users/Jen/Desktop/b/d.png”)

fn onclick s e =
(
case of
(
button_Config: messagebox(“button 1”)
button_Config2: messagebox(“button 2”)
default: print “error”
)

)

on test open do
(
button_Config=dotnetobject “TransparentLabel”
button_Config.dock=button_Config.dock.fill
button_Config.image = pathImg_Config
test.controls.add button_Config
dotNet.addEventHandler button_Config “click” onclick
test.backColor = colors.fromArgb 0 255 255

  button_Config2=dotnetobject  "TransparentLabel"
  button_Config2.dock=button_Config2.dock.fill
  button_Config2.image = pathImg_Config2
  test2.controls.add button_Config2
  dotNet.addEventHandler button_Config2 "click" onclick
  test2.backColor = colors.fromArgb 255 0 0

)
)
createdialog test


#15

change to

case s.tag of
(
1: messagebox("button 1")
2: messagebox("button 2")
default: print "error"
)

add below mxs in open

button_Config.tag=1
button_Config2.tag=2

#16

that’s really cool! we can even put a string in .tag

	case s.tag of
	(
	"config": messagebox(button_config.tag)
	"config2": messagebox("button 2")
	default: print "error"
	)

this gives me an error


#17

change to

"config": messagebox(s.tag)


#18

you need distinguish global / local , they are relative and absolute state by different location


#19

I don’t get your last message about the global/local variable, sorry?

by the way “config”: messagebox(s.tag) works well.

by the way, do we really have to put this:

  button_Config=dotnetobject  "TransparentLabel"
  button_Config.dock=button_Config.dock.fill
  button_Config.image = pathImg_Config
  test.controls.add button_Config
  dotNet.addEventHandler button_Config "click" onclick
  test.backColor = colors.fromArgb 0 255 255
  
  button_Config2=dotnetobject  "TransparentLabel"
  button_Config2.dock=button_Config2.dock.fill
  button_Config2.image = pathImg_Config2
  test2.controls.add button_Config2
  dotNet.addEventHandler button_Config2 "click" onclick
  test2.backColor = colors.fromArgb mynewcolor.r mynewcolor.g mynewcolor.b
  
  button_Config.tag="config"
  button_Config2.tag="config2"
  
  dnToolTip = dotnetobject "tooltip"
  dnToolTip.AutoPopDelay = 5000
  dnToolTip.InitialDelay = 300
  dnToolTip.ReshowDelay = 300
  dnToolTip.ShowAlways = true
  dnToolTip.settooltip button_Config "test"
  dnToolTip.settooltip button_Config2 "test2"

in the open event? Can’t we optimize it by putting info just below the variable statement? I have to put the tooltip in the event as well…


#20

you can put them everywhere legal
2020-01-29_23-22-49
it’s the only content of my toolbar’s rollout , all functions definited out