0

Short Explanation: I'm trying to create a pop-up Password prompt that is fired when the Maximize-window button is clicked.

Longer Explanation: I'm working on a GUI whose default dimensions hide sensitive controls from the user. Clicking on the Maximize-window button will reveal those controls, but I want to prevent easy access to casual users. Ideally I'd like a simple Password prompt to pop up when the Maximize-window button is clicked, which requires a password BEFORE the Maximize-window action takes place.

I've tried using a MessageBox and a separate form, but I can't seem to prevent the Maximize-window action from taking place before the pop-up appears. Any help would be greatly appreciated.

blockheadjr
  • 53
  • 1
  • 1
  • 5
  • Set the sensitive control's to Visible = false. – LarsTech Sep 09 '15 at 18:05
  • I would suggest you rethink the way you're hiding these sensitive controls. You could put them on a hidden tab or another window or beneath other panels so that they won't appear until whatever password prompt is taken care of. Maybe you could just have a button on your form for "Log In" and when it is clicked, you show your prompt. Then you can show your controls (LarsTech's suggestion works here, too, setting their visibility to false until log-in occurs). But relying on window dimensions sounds like a bad idea. – K_Ram Sep 09 '15 at 18:30
  • 2
    Please don't rely on window dimensions to prevent the use of sensitive controls. It would be better to hide them or not instantiate them at all until authorization is verified. Furthermore it is better to control what things a user can do internally, so even if the buttons do get exposed to an unauthorized user accidentally, they do not cause bad things to happen if that user clicks them. – zstewart Sep 09 '15 at 18:30
  • I agree with everyone here, although I have suggested a solution on my answer, I also would advice you to hide the controls. – Ismael Sep 09 '15 at 18:35

2 Answers2

3

There is no OnMaximize event on WindowsForms. Fortunately, you can manipulate the WndProc event to catch the System Message that corresponds to a click in the maximize button.

Try putting this code on the code-behind of your form:

EDIT: Update to also catch a double-click in the title bar (suggested by Reza Aghaei answer).

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);
}
Ismael
  • 622
  • 6
  • 11
  • Knowing that there is no OnMaximize event, I'll probably default to a different approach. Thanks for the input! – blockheadjr Sep 10 '15 at 16:13
1

Just to complete Ismael's good answer, If you use this way, I should mention that this way user can maximize using double click on title bar, so you should add this case to ismael's code:

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398