Set image as background for Button as default, hover and click

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
74
Programming Experience
1-3
I have a file watcher which basically detects some edits on stuff. When that happens, this method is called. This basically hides for a brief moment the form (and the button that has its same size) so it can capture the screen beneath and make the button's color as default, hover and click, taking the dominant color of the captured area. That, in practical terms, makes the button "almost transparent". More about the matter down below.


C#:
Expand Collapse Copy
        private void colorupdate()
        {
            this.Invoke((MethodInvoker)delegate
            {
                this.Visible = false;
            });
            Dictionary<Color, int[]> colori = new Dictionary<Color, int[]>();
            using (Bitmap pixel = new Bitmap(this.Width, this.Height))
            {
                using (Graphics captureGraphics = Graphics.FromImage(pixel))
                {
                    captureGraphics.CopyFromScreen(new Point(this.Location.X, this.Location.Y), Point.Empty, new Size(this.Width, this.Height));
                }
                for (int x = 0; x < this.Width; x++)
                {
                    for (int y = 0; y < this.Height; y++)
                    {
                        Color colore = pixel.GetPixel(x, y);
                        if (!colori.ContainsKey(colore)) { colori.Add(colore, new int[] { 1, x, y }); }
                        else { colori[colore][0]++; }
                    }
                }
            }
            int dss = colori.OrderByDescending(set => set.Value[0]).First().Value[0];
            int[] ds = colori.OrderByDescending(set => set.Value[0]).First().Value;
            Color color = colori.OrderByDescending(set => set.Value[0]).First().Key;
            this.Invoke((MethodInvoker)delegate
            {
                button1.BackColor = color;
                button1.FlatAppearance.MouseOverBackColor = color;
                button1.FlatAppearance.MouseDownBackColor = color;
                button1.FlatStyle = FlatStyle.Flat;
                button1.FlatAppearance.BorderSize = 0;
                this.Visible = true;
            });
        }

I wish to make the button actually transparent but I cannot because using Color.Transparent makes the button only clickable if the user clicks exactly where the click registers any color that is NOT transparent- i.e. if you click on a transparent area of the button, nothing happens. So, I wish to capture instead the area beneaht the button and the form and use it to set the button's "look" (lke, background image). In particular, i would need to set it as look even when clicked and hovered, because i don't want its default behavior change its appearance when interacting with the mouse (it's only supposed to be interacted with the mouse). Do you know perhaps a simple way to use the screenshot (in this code is already there, it's basically "pixel") as a perennial look and not get overwritten by mouse behavior?
 
If you don't want the button to act like a button anymore, why not just use a panel or a custom control? I believe that you can still get Click events from those controls. That way you can simply put a screenshot of what was under the panel into the panel's client area, and you don't need to contend any of the hover over, or mouse down, mouse up painting behaviors. Why fight with the default windows UI behavior?

<Rant> As an aside, as much as I appreciate you wanting a particular kind of behavior, this behavior that you are making doesn't comply with the standard Windows UI guidelines. This is why people complain that apps on Macs have a "unified" look and feel while Windows is "fragmented" -- nobody follows the Windows UI guidelines. </Rant>
 
Back
Top Bottom