1

I have listview control. I need to add semi-transparent button with image to listview. Something like this:

enter image description here

I found several projects that use semi-transparent buttons on the form. But when I transfer them to the ListView, they do not work.

Necessary to use .net 2.0 framework.

Nikolay
  • 587
  • 6
  • 20
  • What do you mean _they do not work_ ? Also: what do you mean by _transfer them to the ListView_ ? This may seem trivial but it is not: You can move a Control over another or add it to the other Control's Controls collection. (For containers by using the mouse for other like a Listview by setting its parent property) Finally: There is no working semitransparency in Winforms unless you draw it yourself. Anything not owner drawn will only let an initial background shine through but will not live update it.. – TaW Jul 29 '14 at 08:51
  • 1. "What do you mean they do not work?" - Transparent dont work. 2. I transfered them to the listview two ways: put on the listview and add to Controls collections listview - no one method does not work. – Nikolay Jul 29 '14 at 10:00
  • 1
    I suggest not doing it. Transparent Buttons are an abomination. And awfully hard to get really right in Winforms. But first and foremost they are a terrible idea. – TaW Jul 29 '14 at 15:34

1 Answers1

1

I found some solution.

Making Transparent Controls - No Flickering

I inherit my TransparentToggleButton class from TranspControl class:

public class TransparentToggleButton : TranspControl
{
    private Image _normalState;
    private Image _mouseUpState;
    private Image _activateState;
    private bool _state;
    private bool _mouseUnder;
    public event EventHandler StateChanged;

    public bool ToggleState
    {
        get { return _state; }
        set
        {
            _state = value;
            SetImage();
        }
    }

    public void SetImages(Image normalState, Image mouseUpState, Image activateState)
    {
        BackImage = normalState;
        _normalState = normalState;
        _mouseUpState = mouseUpState;
        _activateState = activateState;
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);
        if (e.Button == MouseButtons.Left)
        {
            _state = !_state;
            if (StateChanged != null)
                StateChanged(this, e);
            SetImage();
        }
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        _mouseUnder = true;
        SetImage();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        _mouseUnder = false;
        SetImage();
    }

    private void SetImage()
    {
        try
        {
            if (_state)
                BackImage = _activateState;
            else
                BackImage = _mouseUnder ? _mouseUpState : _normalState;
        }
        catch (Exception)
        {

        }
    }
}

Function SetImages loads the 3 images that used for normal state, normal state when cursor over the button, activate state.

Besides need catch listview scroll event and Invalidate() TransparentToggleButton.

Community
  • 1
  • 1
Nikolay
  • 587
  • 6
  • 20