简体   繁体   English

将Windows窗体颜色从一种更改为另一种

[英]Changing Windows Form color from one to another

I'm currently following the Head First C# but every so often I play around on my own. 我目前正在关注Head First C#,但我经常自己玩。 I ran into a situation that I'm unable to solve. 我遇到了无法解决的情况。 I want to change this.BackColor from MediumBlue to step through green 0 - 255 and back to console with a button I/O. 我想将this.BackColor从MediumBlue更改为通过绿色0-255并通过按钮I / O返回控制台。 The issue is that I can't get the color to stop stepping and go back to MediumBlue. 问题是我无法让颜色停止步进并回到MediumBlue。

private void btnClr_Click(object sender, EventArgs e)
    {
        int fClr = 0;
        while (Visible)
        {
            if (fClr == 0)
            {
                fClr++;
                for (int nBackClr = 0; nBackClr < 255 && Visible; nBackClr++)
                {
                    this.BackColor = Color.FromArgb(nBackClr, 255 - nBackClr, nBackClr);
                    Application.DoEvents();
                    Thread.Sleep(2);
                }
                for (int z = 255; z >= 0 && Visible; z--)
                {
                    this.BackColor = Color.FromArgb(z, 255 - z, z);
                    Application.DoEvents();
                    Thread.Sleep(2);
                }
            }
            else
            {
                fClr--;
                this.BackColor = Color.MediumBlue;
                Application.DoEvents();
                Thread.Sleep(1);
            }
        }
    } 

Another thing I'd love to know is how to get the BackColor to go through the ENTIRE color palette. 我想知道的另一件事是如何使BackColor通过整个调色板。

EDIT: S/n After applying JABFreeware's solution. 编辑:S / n 应用JABFreeware的解决方案后。 I added a breakpoint to see what was going on and fClr gets the value of 1 when I first click btnClr_Click . 我添加了一个断点以查看发生了什么,当我第一次单击btnClr_Click时, fClr的值为1。 However when I click it again fClr gets 0 but then immediately gets 1 again. 但是,当我再次单击它时, fClr 0,但立即又fClr 1。 Not sure if this has to do with the while (Visible) . 不知道这是否与while (Visible) ..Head Scratcher.. ..Head Scratcher ..

One of your problems is that int fClr = 0; 您的问题之一是int fClr = 0; is inside the button click event handler so every time they click the button, its reset to 0. Move the variable outside the click event. 在按钮单击事件处理程序内部,因此每次他们单击按钮时,其重置为0。将变量移到单击事件之外。

Like this: 像这样:

        int fClr = 0;
private void btnClr_Click(object sender, EventArgs e)
    {
        while (Visible)
        {
            if (fClr == 0)
            {
                fClr++;
                for (int nBackClr = 0; nBackClr < 255 && Visible; nBackClr++)
                {
                    this.BackColor = Color.FromArgb(nBackClr, 255 - nBackClr, nBackClr);
                    Application.DoEvents();
                    Thread.Sleep(2);
                }
                for (int z = 255; z >= 0 && Visible; z--)
                {
                    this.BackColor = Color.FromArgb(z, 255 - z, z);
                    Application.DoEvents();
                    Thread.Sleep(2);
                }
            }
            else
            {
                fClr--;
                this.BackColor = Color.MediumBlue;
                Application.DoEvents();
                Thread.Sleep(1);
            }
        }
    }

It is easier to use a timer. 使用计时器更容易。 That way your UI will remain responsive without using DoEvents . 这样,您的UI将保持响应状态,而无需使用DoEvents

const int ColorStep = 16;

private Timer _timer;
private int _r, _g, _b;

public frmRotateColors()
{
    InitializeComponent();
}

private void btnStart_Click(object sender, EventArgs e)
{
    _r = 0;
    _g = 0;
    _b = 0;

    // Start the timer
    _timer = new Timer { Interval = 10 };
    _timer.Tick += Timer_Tick;
    _timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{
    if (_b >= 256) { 
        _timer.Dispose(); // Stop the timer.
        this.BackColor = SystemColors.Control; // Reset the background color.
    } else {
        this.BackColor = Color.FromArgb(_r, _g, _b);

        // Get next color
        _r += ColorStep;
        if (_r >= 256) {
            _r = 0;
            _g += ColorStep;
            if (_g >= 256) {
                _g = 0;
                _b += ColorStep;
            }
        }
    }
}

Here I rotate through all the colors by increasing the red color. 在这里,我通过增加红色旋转所有颜色。 If I am out of range of red colors (0 - 255) I reset the red color and increase the green color. 如果我不在红色范围内(0-255),我将重置红色并增加绿色。 If the green color range is exceeded I reset the green color and increase the blue color. 如果超出了绿色范围,我将重置绿色并增加蓝色。 Finally, if the blue color range is exceeded I terminate the process. 最后,如果超出了蓝色范围,则终止该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM