我只是想知道在Windows窗体中是否可以在组合框的边框周围创建一条红线?就像一缕红色然后又一次只是为了表明它已被改变。抓住用户的眼睛或其他东西。我将提供屏幕来表示我想要的内容。
如果有可能,请告诉我在哪里可以查看它以获取一些信息。
答案 0 :(得分:5)
任何时候组合框都会改变,我想要闪烁边框以表明它已经改变了。
主要想法是使用计时器并绘制边框一段时间。您可以使用不同的解决方案绘制边框。例如,您可以(1)在ComboBox
或(2)上绘制边框,您可以在Parent
ComboBox
上绘制边框。
在我提出的答案中,我创建了MyComboBox
并添加了FlashHotBorder
方法,可以调用闪存边框。我还添加了HotBorderColor
属性,可用于设置边框颜色。
ComboBox的闪烁边框
要为ComboBox
绘制边框,您可以处理WM_Paint
的{{1}}消息并绘制边框以进行控制。然后要闪烁边框,您需要使用计时器并打开并关闭边框一段时间:
MyComboBox代码
我创建了一个ComboBox
方法,您可以在FlashHotBorder
事件中调用该方法。此外,如果您希望在选定的索引更改时始终闪烁边框,则可以在SelectedIndexChanged
中调用它。我更喜欢在事件处理程序中调用它。以下是实施:
OnSelectedIndexChanged
然后,对于要闪存的eeach组合的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);
}
}
事件,使用此事件处理程序就足够了:
SelectedIndexChanged
答案 1 :(得分:2)
您可以使用DrawRectangle
方法在 comboBox 或任何其他控件之外创建轮廓/绘制边框。
如果 SelectedIndex 范围条件满足,则会在comboBox外部绘制边框,它将恢复为没有轮廓的原始状态。
bool changed = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (changed)
{
Pen p = new Pen(Color.Red);
Graphics g = e.Graphics;
int diff = 1;
g.DrawRectangle(p, new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff));
}
}
而且,我在comboBox的Form1_Paint
事件上调用SelectedIndexChanged
事件。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 1 && comboBox1.SelectedIndex <= 9)
{
changed = true;
this.Refresh();
}
else
{
changed = false;
this.Refresh();
}
}
大纲 没有大纲
答案 2 :(得分:0)
所以我想出了这个。我认为这是最简单,最简单的方法。如果您有任何建议,请随时发布或评论。感谢所有帮助:)。
public partial class Form1 : Form
{
private int tick = 0;
public Form1()
{
InitializeComponent();
}
bool changed = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (changed == true)
{
changed = false;
this.Refresh();
}
else
{
if(tick<3)
{
timer1.Enabled = true;
timer1.Start();
}
changed = true;
this.Refresh();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (changed)
{
Graphics g1 = e.Graphics;
int diff = 1;
Rectangle rect2 = new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff);
using (LinearGradientBrush br = new LinearGradientBrush(rect2,Color.Red,Color.Blue,LinearGradientMode.Horizontal))
{
ColorBlend color_blend = new ColorBlend();
color_blend.Colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Lime, Color.Blue, Color.Indigo, Color.DarkViolet};
color_blend.Positions = new float[] { 0 / 6f, 1 / 6f, 2 / 6f, 3 / 6f, 4 / 6f, 5 / 6f, 6 / 6f };
br.InterpolationColors = color_blend;
Pen p = new Pen(br, 10);
e.Graphics.DrawRectangle(p, rect2);
}
}
else
{
Pen p = new Pen(Color.Transparent);
Graphics g = e.Graphics;
int diff = 1;
g.DrawRectangle(p, new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if(tick<3)
{
comboBox1_SelectedIndexChanged(null, null);
tick++;
}
else
{
timer1.Stop();
tick = 0;
}
}
}