繁体   English   中英

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

[英]c# application - popup a form everytime user login in windows useraccount

伙计们,大家好,任何人都可以帮助我处理我的 C# 应用程序。 每次用户登录 Windows 用户帐户时,我的应用程序都会弹出我有此代码。 但我的问题是每次登录时都会弹出另一个 form1。 如果我登录 2 次...将会有多个 form1。 我希望它只是一种形式1。 仅隐藏并再次弹出一个(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;

    }

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

 }

暂无
暂无

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

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