更改组合框边框颜色-更改SelectedIndex时闪烁 [英] Change ComboBox Border Color - Flash when SelectedIndex changed

查看:86
本文介绍了更改组合框边框颜色-更改SelectedIndex时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道在Windows窗体中是否可以在组合框的边框周围创建一条红线?就像只是一闪而过的红色,然后再次消失只是为了表明它已更改。引起用户的注意。我将提供屏幕来表示我想要的内容。



如果可能的话,请告诉我可以在哪里查找以获取一些信息。

>

无边框

更改时边框闪烁

一两秒钟后,边框再次消失了。

解决方案


每当组合框更改时,我要闪烁一个边框以指示其已更改。


主要思想是使用计时器并为某些对象绘制边框次。您可以使用其他解决方案来绘制边框。例如,您可以(1) ComboBox 上绘制边框,或者(2)您可以在父项组合框)。



在我提出的答案中,我创建了 MyComboBox 并添加了 FlashHotBorder 方法,可以调用该方法来显示边框。我还添加了一个 HotBorderColor 属性,该属性可用于设置边框颜色。



ComboBox的闪烁边框



要为 ComboBox 绘制边框,您可以处理



MyComboBox代码



我创建了您可以在 SelectedIndexChanged 事件中调用FlashHotBorder 方法。另外,如果始终希望在选定索引更改时闪烁边框,则可以在 OnSelectedIndexChanged 中调用它。我更喜欢在事件处理程序中调用它。这是实现:

 使用System.Drawing; 
使用System.Windows.Forms;
公共类MyComboBox:ComboBox
{
int flash = 0;
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.Horizo​​ntalScrollBarArrowWidth;
public Color HotBorderColor {get;组; }
私人布尔DrawBorder {get;组; }
Timer计时器;
public MyComboBox()
{
this.HotBorderColor = Color.Red;
timer = new Timer(){Interval = 100};
timer.Tick + = new System.EventHandler(timer_Tick);
}
受保护的覆盖无效WndProc(ref Message m)
{
base.WndProc(ref m);
if(m.Msg == WM_PAINT& this.DrawBorder)
使用(var g = Graphics.FromHwnd(this.Handle))
使用(var p = new Pen( this.HotBorderColor))
g.DrawRectangle(p,0,0,this.Width-1,this.Height-1);
}
public void FlashHotBorder()
{
flash = 0;
timer.Start();
}
void timer_Tick(object sender,System.EventArgs e)
{
if(flash< 10)
{
flash ++;
this.DrawBorder =!this.DrawBorder;
this.Invalidate();
}
else
{
timer.Stop();
flash = 0;
DrawBorder = false;
}
}
受保护的覆盖无效Dispose(布尔处理)
{
if(处理){timer.Dispose(); }
base.Dispose(处置);
}
}

然后足够使用此事件处理程序进行<$要闪烁的每个组合的c $ c> SelectedIndexChanged 事件:

  private void myComboBox1_SelectedIndexChanged(对象发件人,EventArgs e)
{
var combo =发件人为FlatCombo;
if(combo!= null)
combo.FlashHotBorder();
}


I just wanted to know if in windows forms I can create a red line around the border of a combobox when its changed? Like just a flash of red and then gone again just to show that it was changed. Catch the user's eye or something. I will provide screens to represent what i would like.

If it is possible, please tell me where I can look it up to gain some information on it.

No border Border flash on change Border gone again after a second or two

解决方案

Anytime the combobox changes, I want to flash a border to indicate it has changed.

The main idea is using a timer and drawing a border for some times. You can draw the border using different solutions. For example you can (1) draw the border on ComboBox or (2) you can draw border on Parent of ComboBox.

In the answer which I posed, I created a MyComboBox and added a FlashHotBorder method which can be called to flash border. Also I added a HotBorderColor property which can be used to set border color.

Flashing Border of ComboBox

To draw a border for ComboBox you can handle WM_Paint message of ComboBox and draw a border for control. Then to flash the border, you need to use a timer and turn on and turn off border for some times:

MyComboBox Code

I've created a FlashHotBorder method which you can call in SelectedIndexChanged event. Also if always you want to flash border when selected index changes, you can call it in OnSelectedIndexChanged. I prefer to call it in event handler. Here is the implementation:

using System.Drawing;
using System.Windows.Forms;
public class MyComboBox : ComboBox
{
    int flash = 0;
    private const int WM_PAINT = 0xF;
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    public Color HotBorderColor { get; set; }
    private bool DrawBorder { get; set; }
    Timer timer;
    public MyComboBox()
    {
        this.HotBorderColor = Color.Red;
        timer = new Timer() { Interval = 100 };
        timer.Tick += new System.EventHandler(timer_Tick);
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT && this.DrawBorder)
            using (var g = Graphics.FromHwnd(this.Handle))
            using (var p = new Pen(this.HotBorderColor))
                g.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
    }
    public void FlashHotBorder()
    {
        flash = 0;
        timer.Start();
    }
    void timer_Tick(object sender, System.EventArgs e)
    {
        if (flash < 10)
        {
            flash++;
            this.DrawBorder = !this.DrawBorder;
            this.Invalidate();
        }
        else
        {
            timer.Stop();
            flash = 0;
            DrawBorder = false;
        }
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing) { timer.Dispose(); }
        base.Dispose(disposing);
    }
}

Then it's enough to use this event handler for SelectedIndexChanged event of eeach combo which you want to flash:

private void myComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var combo = sender as FlatCombo;
    if (combo != null)
        combo.FlashHotBorder();
}

这篇关于更改组合框边框颜色-更改SelectedIndex时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆