简体   繁体   English

C# Forms. 改变背景颜色(闪烁)

[英]C# Forms. Changing the background color (Blink)

I am working on a program that allows you to control the OBS program.我正在开发一个允许您控制OBS程序的程序。 I receive json messages from OBS which I then process.我收到来自 OBS 的 json 条消息,然后对其进行处理。 These messages control the lighting on the Launchpad .这些消息控制Launchpad上的灯光。 However, I would like the buttons in the program itself to change color in addition to the lights on the Launchpad.但是,除了 Launchpad 上的灯之外,我还希望程序本身的按钮可以改变颜色。 The problem for me is the fact that the colors are not only static, but flickering too.我的问题是 colors 不仅是 static,而且还在闪烁。 I need to change them depending on the state of some control in OBS.我需要根据 OBS 中某些控件的 state 更改它们。 I don't know how to solve my problem.我不知道如何解决我的问题。 Create a new method or something?创建一个新方法还是什么?

Example json i recive例如 json 我收到

{"muted":false,"sourceName":"Audio Device","update-type":"SourceMuteStateChanged"}
{"muted":true,"sourceName":"Audio Device","update-type":"SourceMuteStateChanged"}

In this example, the first message means that the audio input has been unmuted and then that it has been muted.在此示例中,第一条消息表示音频输入已取消静音,然后它已被静音。

The part of c # that handles this c # 处理这个的部分

else if (upadte_type.Groups[0].Value == "SourceMuteStateChanged")
                    {
                        Match sourceName = Regex.Match(e.Data, "(?<=\"sourceName\":\").*?(?=\")");
                        if (sourceName.Success)
                        {
                            Match isMuted = Regex.Match(e.Data, "(?<=\"muted\":).*?(?=,)");
                            if (isMuted.Success)
                            {
                                if(isMuted.Groups[0].Value == "false")
                                {
                                    foreach(MuteListClass s in padsMute)
                                    {
                                        if(s.mute == sourceName.Groups[0].Value)
                                        {
                                            outputDevice.Open();
                                            byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 };
                                            outputDevice.SendSysEx(stat);
                                            outputDevice.Close();
                                        }
                                    }
                                }
                                else
                                {
                                    foreach (MuteListClass s in padsMute)
                                    {
                                        if (s.mute == sourceName.Groups[0].Value)
                                        {
                                            outputDevice.Open();
                                            byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsOFF[s.id, 0], 247 };
                                            outputDevice.SendSysEx(stat);
                                            outputDevice.Close();
                                        }
                                    }
                                }
                            }
                        }
                    }
outputDevice = DeviceManager.OutputDevices[midiOutBox.SelectedIndex];
List<MuteListClass> padsMute = new List<MuteListClass>();

public class MuteListClass
{
    public string mute { get; set; }
    public int id { set; get; }
}
padColorSettingsON //int array which holds a number that corresponds to the color of the light in Launchpad 
byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 }; //byte array that will be sent to Launchpad

I know Regex is not a right way for json, but this part of the code is quite old and I'm going to change it to serialization.我知道 Regex 不是 json 的正确方法,但这部分代码很旧,我打算将其更改为序列化。 (I am still learning all of this) (我还在学习所有这些)

Screenshot of the program程序截图程序

I would like those gray buttons in the program to change color like the lights on the Launchpad.我希望程序中的那些灰色按钮可以像 Launchpad 上的灯一样改变颜色。 For example.例如。 When the device is unmuted the lower left corner is green, but when the device is muted it blinks red and green.当设备未静音时,左下角为绿色,但当设备静音时,它会闪烁红色和绿色。

For Launchpad there is no big problem because this function is built in (I send a different byte array) but I don't know how to solve it in c #.对于 Launchpad 没有大问题,因为这个 function 是内置的(我发送了一个不同的字节数组)但是我不知道如何在 c # 中解决它。 I want to be able to control between static and flickering colors.我希望能够控制在static和闪烁的colors之间。

I've heard about async and await but I don't know how to apply them and if they will work for me in this case.我听说过异步和等待,但我不知道如何应用它们,也不知道在这种情况下它们是否适合我。

I was also thinking about making a separate function that would constantly change the color of the buttons based on boolean or something like that, but I don't know how to make a function that would still run in the background and check everything.我也在考虑制作一个单独的 function,它会根据 boolean 或类似的东西不断改变按钮的颜色,但我不知道如何制作一个仍然会在后台运行并检查所有内容的 function。

I think it is also worth mentioning that the function in c # that I showed earlier is only called when OBS sends a new json message, and all 81 buttons that you can see in the screenshot can be lit simultaneously.我觉得还值得一提的是,我之前展示的c#中的function只有在OBS发送新的json消息时才会调用,截图中你能看到的81个按钮都会同时点亮。 The buttons themselves are also modified and each button has an ID corresponding to the buttons on the Launchpad so they can be edited separately, for example.按钮本身也进行了修改,并且每个按钮都有一个与 Launchpad 上的按钮相对应的 ID,因此可以单独编辑它们,例如。

foreach(LaunchpadButton b in GetAll(this, typeof(LaunchpadButton)))
            {
                if(b.ID == 11){b.BackColor = Color;}
            }

This question is a little vague to be honest, so it's not quite clear what you're asking in full, maybe looking for guidance on how to approach it which isn't really what SO is for, but I'll take a stab.老实说,这个问题有点模糊,所以不太清楚你要问的是什么,也许正在寻找关于如何处理它的指导,这并不是 SO 的真正目的,但我会试一试。

So this kind of creates the effect you're looking for I think?所以我认为这种会产生您正在寻找的效果?

Button targetButton;
public Form1()
{
    InitializeComponent();
    targetButton = button1;
}

private void button2_Click(object sender, EventArgs e) => Task.Run(() => FlickerButton(targetButton, 1, Color.Red));
private void button3_Click(object sender, EventArgs e) => Task.Run(() => FlickerButton2(targetButton, 1, Color.Red, Color.Blue, Color.Green));

public void FlickerButton(Button button, int seconds, Color color)
{
    var currColor = button.BackColor;
    button.BackColor = color;
    Thread.Sleep(seconds * 1000);
    button.BackColor = currColor;
}

public void FlickerButton2(Button button, int seconds, params Color[] colors)
{
    var currColor = button.BackColor;
    foreach (var color in colors)
    {
        button.BackColor = color;
        Thread.Sleep(seconds * 1000);
    }
    button.BackColor = currColor;
}

You can see when I click button 2, changes color for 1 second and then changes back你可以看到当我点击按钮 2 时,改变颜色 1 秒然后变回在此处输入图像描述

在此处输入图像描述

And I'm not going to screenshot this, but clicking on button 3 will do similar but with multiple colors.我不打算对此进行截图,但单击按钮 3 会执行类似操作,但会使用多个 colors。

I'm not quite sure how your byte array fits in if I'm honest, but I guess that's for you to figure out老实说,我不太确定你的字节数组是如何适应的,但我想这是你要弄清楚的

byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 }; byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 };

You can do this 100 times all at the same time, which based on the video I watched on Launchpad is what you're going for.您可以同时执行此操作 100 次,根据我在 Launchpad 上观看的视频,这正是您想要的。

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

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