c#application - 每次用户登录windows useraccount时弹出一个表单

时间:2015-10-07 12:51:34

标签: c#

好日子,有人可以帮助我使用我的c#应用程序。每次用户登录windows useraccount时,我的应用程序都会弹出 我有这个代码。但我的问题是每次我登录时都会弹出另一个form1。如果我登录2次......会有多个form1。 我希望它只是一个form1。再次隐藏和弹出一个(form1)

由于

using Microsoft.Win32;

 public Form1()
    {
        InitializeComponent();
        SystemEvents.SessionSwitch += OnSessionSwitch;

        this.WindowState = FormWindowState.Normal;

        this.Focus();
        this.BringToFront();
        this.TopMost = true;
    }


static void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
     {

        switch (e.Reason)
        {
            case SessionSwitchReason.SessionLogon:
                // User has logged on to the computer.

                break;

            case SessionSwitchReason.SessionLogoff:
                // User has logged off from the computer.
                break;

            case SessionSwitchReason.SessionUnlock:

                Form1 frm1 = new Form1();
                frm1.Show();

                break;

            case SessionSwitchReason.SessionLock:
                // The computer has been locked.
                break;
        }


    }

 private void button1_Click(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
        frm1.Hide();
        this.WindowState = FormWindowState.Minimized;

    }

1 个答案:

答案 0 :(得分:0)

您必须更改static方法。查看下面的示例。当您在活动中展示时,您也忘记了Maximize表单。

using Microsoft.Win32;

 public Form1()
    {
        InitializeComponent();
        SystemEvents.SessionSwitch += OnSessionSwitch;

        this.WindowState = FormWindowState.Normal;

        this.Focus();
        this.BringToFront();
        this.TopMost = true;
    }


void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
     {

        switch (e.Reason)
        {
            case SessionSwitchReason.SessionLogon:
                // User has logged on to the computer.

                break;

            case SessionSwitchReason.SessionLogoff:
                // User has logged off from the computer.
                break;

            case SessionSwitchReason.SessionUnlock:
                this.WindowState = FormWindowState.Normal;
                this.Show();

                break;

            case SessionSwitchReason.SessionLock:
                // The computer has been locked.
                break;
     }


 }

 private void button1_Click(object sender, EventArgs e)
 {
     this.Hide();
     this.WindowState = FormWindowState.Minimized;

 }