Cancel A WinForm Minimize?

前端 未结 4 853
名媛妹妹
名媛妹妹 2021-01-03 03:06

I have a winform with the minimizeMaximizeClose buttons disabled, but still if someone presses it in the task bar, it will minimize. I want to prevent this from happening.

4条回答
  •  日久生厌
    2021-01-03 03:34

    Override WndProc on your form, listen for minimize messages and cancel.

    Add this code to your form:

    private const int WM_SYSCOMMAND = 0x0112; 
    private const int SC_MINIMIZE = 0xf020; 
    
     protected override void WndProc(ref Message m) 
    { 
        if (m.Msg == WM_SYSCOMMAND) 
        { 
            if (m.WParam.ToInt32() == SC_MINIMIZE) 
            { 
                m.Result = IntPtr.Zero; 
                return; 
            } 
        } 
        base.WndProc(ref m); 
    } 
    

    I modified Rob's code found in this SO thread:
    How to disable the minimize button in C#?

    Works great: no flickering, no nothing when the user attempts to minimize.

提交回复
热议问题