[Closed] Color + png file on button?
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
this is the original question…
Two issues are combined in the question – the desire to have a Button with background color, and the desire to have an alpha-transparent Label. What is the reason to have them both? If you want to have the button, let just focus on it – make a Button with color background and alpha-transparent image (icon).
MXS doesn’t provide this kind of control, so you have to use anything else. For example with System.Windows.Forms you can do it, and you don’t need an extra control (Label in your case).
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
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
png used: https://www.freepng.fr/png-zmnkmi/
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
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:100local 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.
well the button is now clickable but i can’t see my png image, only the color.
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
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