Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-使用计时器和轨迹栏逐渐填充矩形以控制速度_C#_Winforms - Fatal编程技术网

C#-使用计时器和轨迹栏逐渐填充矩形以控制速度

C#-使用计时器和轨迹栏逐渐填充矩形以控制速度,c#,winforms,C#,Winforms,我在尝试通过使用轨迹栏值来控制填充速度来填充矩形时遇到了一些问题。想象一下,水流入杯子,所以水倒进杯子,但水流的速度由轨迹栏的值控制。我不能让它从下往上填满。它从上往下开始,我需要它从下往上填充。我的代码如下: private void trackBar_Scroll(object sender, EventArgs e) { trackBarSpeed = trackBar.Value; } private void tmrTimer_Tic

我在尝试通过使用轨迹栏值来控制填充速度来填充矩形时遇到了一些问题。想象一下,水流入杯子,所以水倒进杯子,但水流的速度由轨迹栏的值控制。我不能让它从下往上填满。它从上往下开始,我需要它从下往上填充。我的代码如下:

    private void trackBar_Scroll(object sender, EventArgs e)
    {
        trackBarSpeed = trackBar.Value;

    }

    private void tmrTimer_Tick(object sender, EventArgs e)
    {
        timerCounter++;

        Graphics graphics = CreateGraphics();
        Rectangle rectangle = new Rectangle(81, 201, 219, (10 + timerCounter) * trackBarSpeed);

        graphics.FillRectangle(new SolidBrush(colour), rectangle);
    }

不要在每个计时器刻度中创建新的
矩形
。相反,只需创建一次矩形,并在计时器刻度中修改其大小。

当前解决方案存在一些问题

首先,您不应该在
Paint
事件之外绘制控件。在
勾选
事件处理程序上,您应该更新要绘制的矩形,然后在控件上调用
Invalidate()
,这将导致它触发绘制事件,您可以在
OnPaint
覆盖中执行绘制。事件处理程序中为您提供了一个
Graphics
对象

其次,
矩形
的位置是其左上角的位置,因此如果您想让矩形从底部“填充”,则需要使用一些数学方法调整矩形的位置

第三,你应该养成一种习惯,正确地
处理你创建的
画笔
,理想的方法是使用
语句将其放入

以下是LINQPad C#声明,其中说明了这些原则:

var label = new Label();

var trackBar = new TrackBar();
trackBar.Minimum = 0;
trackBar.Maximum = 10;
trackBar.ValueChanged += (s, e) => {
    label.Text = trackBar.Value.ToString();
};
trackBar.Value = 5;
trackBar.TickFrequency = 1;
trackBar.Width = 200;

var fillingRectangleHeight = 0;
var fullRectangleBounds = new Rectangle(40, 40, 100, 400);
var fillColor = Color.Blue;

var panel = new FlowLayoutPanel();
panel.Controls.Add(trackBar);
panel.Controls.Add(label);
panel.Paint += (s, e) => {

    var bottom = fullRectangleBounds.Bottom;
    var top = bottom - fillingRectangleHeight;
    var fillingRectangleBounds = new Rectangle(
        fullRectangleBounds.X, top, fullRectangleBounds.Width, fillingRectangleHeight);

    using (var fillBrush = new SolidBrush(fillColor))
    {
        e.Graphics.FillRectangle(fillBrush, fillingRectangleBounds);
    }

};

var timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += (s, e) => {

    fillingRectangleHeight = Math.Min(
        fillingRectangleHeight + trackBar.Value, 
        fullRectangleBounds.Height);
    panel.Invalidate();

    if (fillingRectangleHeight == fullRectangleBounds.Height)
    {
        // once the rectangle is full, no point in the timer
        // running anymore
        timer.Stop();
    }

};
timer.Start();

panel.Dump();

Winforms?WPF?(请添加标签!)Windows窗体,对不起,我的朋友,我不确定这是如何解决这个问题的。