c#应用程序-每次用户登录Windows用户帐户时弹出一个窗体

本文关键字:用户 窗体 一个 应用程序 登录 Windows | 更新日期: 2023-09-27 18:16:40

大家好,有谁能帮我的c#应用程序吗?我的应用程序将弹出每次用户登录在windows用户帐户我有这个密码。但我的问题是,每次我登录有另一个表单将弹出。如果我登录2次…会有多种形式。我希望它只有一种形式。隐藏和再次弹出只有一个(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;
    }

c#应用程序-每次用户登录Windows用户帐户时弹出一个窗体

您必须更改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;
 }