C# WinForm中的闪烁按钮

C# WinForm中的闪烁按钮,c#,winforms,visual-studio,user-interface,flicker,C#,Winforms,Visual Studio,User Interface,Flicker,我正在制作一个BCI界面,在我使用System.Windows.Timer时,按钮必须以特定频率闪烁,但是这不是非常精确,闪烁不是指定的频率。我用来制作闪烁按钮的类有: StateWithColor[] colors = new StateWithColor[] { new StateWithColor(0, Color.Black), new StateWithColor(1, Color.White) }; public class FlickTim

我正在制作一个BCI界面,在我使用System.Windows.Timer时,按钮必须以特定频率闪烁,但是这不是非常精确,闪烁不是指定的频率。我用来制作闪烁按钮的类有:

StateWithColor[] colors = new StateWithColor[] {
        new StateWithColor(0, Color.Black),
        new StateWithColor(1, Color.White)
    };

public class FlickTimer<T> : IDisposable
      where T : Control
{
    public T Target { get; set; }

    protected readonly IList<StateWithColor> possibleStates = new          List<StateWithColor>();
    protected int currentState = 0;
    //protected int currentState_2 = 1;
    protected object lockState = new object();
    protected Timer timer = new Timer();

    protected void Flicker(object sender, EventArgs e)
    {
        if (Target == null)
        {
            return;
        }
        if (Target.InvokeRequired)
        {
            Target.Invoke(new EventHandler(Flicker), sender, e);
            return;
        }
        lock (lockState)
        {
            Target.BackColor = possibleStates[currentState].Color;
            //int color = currentState + 1;
            //Target.ForeColor = possibleStates[currentState_2].Color;
            currentState++;
            //currentState_2--;
            if (currentState >= possibleStates.Count)
            {
                currentState = 0;
                //currentState_2 = 1;
            }
        }
    }

    public FlickTimer(StateWithColor[] states, int timeout = 0, T target = null)
    {
        Target = target;
        lock (lockState)
        {
            foreach (var state in states)
            {
                possibleStates.Add(state);
            }
        }
        timer.Interval = timeout;
        timer.Tick += Flicker;
        Start();
    }

    public void Start()
    {
        timer.Start();
    }

    public void Stop()
    {
        timer.Stop();
    }

    public void Dispose()
    {
        if (timer != null)
        {
            Stop();
            timer.Tick -= Flicker;
            timer = null;
        }
    }
}

public struct StateWithColor
{    public int State;
    public Color Color;

    public StateWithColor(int state, Color color)
    {
        Color = color;
        State = state;
    }
}
StateWithColor[]colors=新StateWithColor[]{
新StateWithColor(0,颜色为黑色),
新StateWithColor(1,彩色。白色)
};
公共类FlickTimer:IDisposable
其中T:控制
{
公共T目标{get;set;}
受保护的只读IList possibleStates=新列表();
受保护的int currentState=0;
//受保护的int电流状态_2=1;
受保护对象锁定状态=新对象();
受保护定时器=新定时器();
受保护的无效闪烁(对象发送器、事件参数e)
{
if(Target==null)
{
返回;
}
if(Target.invokererequired)
{
Invoke(新的EventHandler(Flicker)、sender、e);
返回;
}
锁定(锁定状态)
{
Target.BackColor=possibleStates[currentState].Color;
//int color=currentState+1;
//Target.ForeColor=possibleStates[currentState_2]。颜色;
currentState++;
//当前状态_2--;
如果(currentState>=可能的不动产计数)
{
currentState=0;
//当前状态_2=1;
}
}
}
公共FlickTimer(StateWithColor[]状态,int timeout=0,T target=null)
{
目标=目标;
锁定(锁定状态)
{
foreach(状态中的var状态)
{
可能的不动产。添加(州);
}
}
timer.Interval=超时;
定时器。滴答+=闪烁;
Start();
}
公开作废开始()
{
timer.Start();
}
公共停车场()
{
timer.Stop();
}
公共空间处置()
{
如果(计时器!=null)
{
停止();
timer.Tick-=闪烁;
定时器=空;
}
}
}
公共结构StateWithColor
{国家公共部门;
公共色彩;
公共状态WithColor(int状态,颜色)
{
颜色=颜色;
状态=状态;
}
}
这并没有给出我在运行接口时指定的频率。界面使用的屏幕设置为120HZ,界面包含9个按钮,其频率应为:6、6.5、7、7.5、8、9、10、11、12.5。或在6-15HZ范围内的九个频率。频率可以是6.546或6.5并不重要


有没有办法做到这一点,并获得准确频率的飞行记录?

这相当愚蠢,一定是家庭作业。毫无疑问,你应该自己发现的是,计时器的滴答声有多准确并不重要。您只使用它来更新按钮的颜色,而不是跟踪时间。正如Hans所说,使用Winforms无法做到这一点。计时器类都是基于计时机制的,根本没有您想要的精度。我还将注意到,在120 Hz的显示器上,即使您可以达到这种精度(例如,使用较低级别的API,如Direct2D),您也不会得到想要的效果,除非频率均匀地除以120 Hz。如果“BCI”的意思是“脑-机接口”(你需要定义你的术语),你需要一个精确的闪烁信号,我想你将不得不使用计算机显示器以外的东西。以前在Silverlight中已经完成了,但我很乐意以一种新的接口形式来完成,这就是choise成为WinForm的原因@正如我提到的,从物理上讲,不可能获得您所声明的作为目标的粒度级别。不管是Silverlight还是其他,说“已经完成了”是不可信的。如果你的研究基于错误的假设,即你或其他人成功地生成了某些视觉指示器的更新频率(即“闪烁”),那么该研究是有缺陷的。