How to make an image clickable (image as a button)?

64 views
Skip to first unread message

Fei XU

unread,
Apr 16, 2016, 8:28:26 AM4/16/16
to Eto.Forms
Hi,

I'd like to create an image button, for example, a round image (like home icon) but it's clickable. When the user clicks on the image, it behaves like a button. 

Current button with image does not meet my requirement. I can put an image inside a button, but there still is a rectangle button box around the image. Actually I just want the image, and the user can't see the button rectangle.

I think one option is to use an image, and handle the button down/up message by myself, is it the only available solution?

Fei

Michael Davis

unread,
Apr 25, 2016, 11:50:53 AM4/25/16
to Eto.Forms
Not the best solution, but it is a quick one:

    public class ImageButton : ImageView
    {
        private Color TransparentColor { get; } = new Color(0, 0, 0, 0);
        public event EventHandler<EventArgs> Click;
        
         
        public ImageButton()
        {
            MouseEnter += OnMouseEnter;
            MouseLeave += OnMouseLeave;
            MouseUp += (s, e) => OnClick();
        }


        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            BackgroundColor = TransparentColor;
        }


        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            var highlight = SystemColors.Highlight;
            highlight.A = .7f;

            BackgroundColor = highlight;
        }


        protected virtual void OnClick()
        {
            Click?.Invoke(this, EventArgs.Empty);
        }
    }
Reply all
Reply to author
Forward
0 new messages