繁体   English   中英

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

[英]Change form background colour from Program.cs

我已经有很长一段时间了,无法找到解决方案。 我想在解锁计算机时更改表单的背景色。 我正在努力从Program.cs文件访问表单BackColour属性。

我已经在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);
        } 
    }
}

和我的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;
    }




}

}

您必须引用已经声明的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);

暂无
暂无

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

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