从Program.cs更改表单背景颜色

本文关键字:表单 背景 颜色 Program cs | 更新日期: 2023-09-27 18:15:20

我一直在这个问题上,无法找到一个解决方案。我想改变一个窗体的背景颜色,当计算机解锁。我正在努力从Program.cs文件访问表单backcolor属性。

我已经在我的form.cs中创建了一个方法,我可以从program.cs中引用,但是,我不知道如何从我的方法中改变背景颜色。

这是我的代码。如有任何意见,我将不胜感激。 //Program.cs

namespace Lums_Status_Client
{
    static class Program
    {
        public static Form statusform = new Form1();
        public static string status = "available";
        private static SessionSwitchEventHandler sseh;
        [STAThread]
        static void Main()
        {
            ThreadStart job = new ThreadStart(ThreadJob);
            Thread thread = new Thread(job);
            thread.Start();
            sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            SystemEvents.SessionSwitch += sseh;
            while (true) { }    
        }
        static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            //get the username
            string get_userName = Environment.UserName;
            Debug.WriteLine(e.Reason);
            Form1.colourchanger();
        }
        static void ThreadJob()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);        
            Application.Run(statusform);
        } 
    }
}

and my form.cs

namespace Lums_Status_Client
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.BackColor = System.Drawing.Color.Green;
        System.Drawing.Rectangle workingRectangle =
        Screen.PrimaryScreen.WorkingArea;

        this.Left = workingRectangle.Width - 120;
    }
    private void Status_Change(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && Program.status == "available")
        {
            this.BackColor = System.Drawing.Color.Orange;
            Program.status = "Busy";
            MessageBox.Show("You status has been updated to " + Program.status);

        }
        else if (e.Button == MouseButtons.Left && Program.status == "Busy")
        {
            this.BackColor = System.Drawing.Color.Green;
            Program.status = "Available";
            MessageBox.Show("You status has been updated to " + Program.status);

        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    public static void colourchanger()
    {
        //Debug.WriteLine("This class is working");
        this.BackColor = System.Drawing.Color.Aqua;
    }


}

}

从Program.cs更改表单背景颜色

您必须引用您已经声明的Form1的实例,并使其成为实例方法,而不是static:

 public void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        //get the username
        string get_userName = Environment.UserName;
        Debug.WriteLine(e.Reason);
        statusform.colourchanger(); //access instance object
    }

假设您只需要一个Form1实例,您可以更改Program.cs以保持准备好的Form1引用:

using System.Drawing;
//...
//...
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        F1 = new Form1();
        Application.Run(F1);
    }
    static Form1 F1;
    public static void ChangeColor(Color newColor)
    {
        F1.BackColor = newColor;
    }
}
现在你可以调用ChangeColor方法:
Program.ChangeColor(Color.Aqua);