代码之家  ›  专栏  ›  技术社区  ›  blockheadjr

在最大化之前触发密码提示的最大化按钮?

  •  0
  • blockheadjr  · 技术社区  · 10 年前

    简短解释:我正在尝试创建一个弹出的密码提示,当单击“最大化”窗口按钮时,该提示将被触发。

    更长的解释:我正在开发一个GUI,它的默认尺寸对用户隐藏敏感控件。单击“最大化”窗口按钮将显示这些控件,但我希望防止临时用户轻松访问。理想情况下,我希望在单击“最大化”窗口按钮时弹出一个简单的密码提示,这需要在“最大化窗口”操作发生之前输入密码。

    我尝试过使用MessageBox和单独的表单,但似乎无法阻止在弹出窗口出现之前执行“最大化”窗口操作。如有任何帮助,将不胜感激。

    2 回复  |  直到 10 年前
        1
  •  3
  •   Ismael    10 年前

    WindowsForms上没有OnMaximize事件。幸运的是,您可以操纵WndProc事件来捕获与单击最大化按钮相对应的系统消息。

    尝试将此代码放在表单的代码后面:

    编辑: 更新以在标题栏中双击(由Reza Aghaei回答建议)。

    protected override void WndProc(ref Message m)
    {
        // 0x112: A click on one of the window buttons.
        // 0xF030: The button is the maximize button.
        // 0x00A3: The user double-clicked the title bar.
        if ((m.Msg == 0x0112 && m.WParam == new IntPtr(0xF030)) || (m.Msg == 0x00A3 && this.WindowState != FormWindowState.Maximized))
        {
            // Change this code to manipulate your password check.
            // If the authentication fails, return: it will cancel the Maximize operation.
            if (MessageBox.Show("Maximize?", "Alert", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                // You can do stuff to tell the user about the failed authentication before returning
                return;
            }
        }
    
        // If any other operation is made, or the authentication succeeds, let it complete normally.
        base.WndProc(ref m);
    }
    
        2
  •  1
  •   Reza Aghaei    10 年前

    为了完成Ismael的好答案,如果你使用这种方式,我应该提到,这种方式用户可以通过双击标题栏来最大化,所以你应该将这个案例添加到Ismael代码中:

    case 0x00A3:
        // Change this code to manipulate your password check.
        // If the authentication fails, return: it will cancel the Maximize operation.
        if (MessageBox.Show("Maximize?", "Alert", MessageBoxButtons.YesNo) == DialogResult.No)
        {
            // You can do stuff to tell the user about the failed authentication before returning
            return;
        }
        break;