我有listview控件。我需要将带图像的半透明按钮添加到listview。像这样:
我发现several projects使用表单上的半透明按钮。但是当我将它们传输到ListView时,它们不起作用。
必须使用.net 2.0框架。
答案 0 :(得分: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。