【问题标题】:Add semi-transparent button to ListView将半透明按钮添加到 ListView
【发布时间】:2014-07-31 18:49:03
【问题描述】:

我有列表视图控件。我需要将带有图像的半透明按钮添加到列表视图。像这样的:

我发现 several projects 在表单上使用半透明按钮。但是当我将它们转移到 ListView 时,它们不起作用。

需要使用 .net 2.0 框架。

【问题讨论】:

  • 你是什么意思他们不工作?另外:将它们转移到 ListView 是什么意思?这可能看起来微不足道,但事实并非如此:您可以将一个控件移到另一个控件上或将其添加到另一个控件的控件集合中。 (对于通过设置其父属性将鼠标用于其他类似 Listview 的容器) 最后:Winforms 中没有工作半透明,除非您自己绘制它。任何不是所有者绘制的东西都只会让初始背景闪耀,但不会实时更新它..
  • 1. “你的意思是他们不工作?” - 透明不起作用。 2.我将它们转移到列表视图有两种方式:放入列表视图并添加到控件集合列表视图 - 没有一种方法不起作用。
  • 我建议不要这样做。透明按钮是可憎的。并且很难在 Winforms 中真正做到正确。但首先,它们是一个糟糕的主意。

标签: c# .net winforms listview transparent


【解决方案1】:

我找到了一些解决方案。

Making Transparent Controls - No Flickering

我从 TranspControl 类继承了我的 TransparentToggleButton 类:

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)
        {

        }
    }
}

函数SetImages加载3个用于正常状态的图像,当光标在按钮上时的正常状态,激活状态。

除了需要 catch listview scroll event 和 Invalidate() TransparentToggleButton。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2015-11-19
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多