Dimming inactive forms

前端 未结 3 1385
囚心锁ツ
囚心锁ツ 2021-01-16 13:36

When opening Dialog form using Form.ShowDialog() I want to dim the rest of application with a shade of gray.

From my own research it seems that the way to do it is

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 14:14

    An alternative to this approach is to use an inherited form instance which hides the showDialog. Then you can disable/enable all forms bar the current one. The code for the dummy form is to deal with the issue described here; Cannot fire form activate event - issue with disabled forms

    One of the advantages to this approach is that it requires no change to the normal handling of the showDialog method. Just call it like;

    if (dlg.ShowDialog(this) == DialogResult.OK) {
                // etc...
            }
    

    In your inherited form

    public abstract class MyBaseForm : XtraForm
    {
        private DialogResult setFormsToBackground(Form fParent)
        {
            Form dummyForm = new Form();
            dummyForm.ShowInTaskbar = false;
            dummyForm.FormBorderStyle = FormBorderStyle.None;
            dummyForm.Load += ((object sender, EventArgs e) => { (sender as Form).Size = new Size(0, 0); });
    
            List
    lstFormsToEnable = new List(); for (int i = Application.OpenForms.Count - 1; i >= 0; i--) { try { Form checkfrm = Application.OpenForms[i]; if (checkfrm != this && dummyForm != this && checkfrm.Enabled) { lstFormsToEnable.Add(checkfrm); checkfrm.Enabled = false; } } catch (Exception ex) { } } dummyForm.Show(); DialogResult result = DialogResult.None; if (fParent == null) result = base.ShowDialog(); else result = base.ShowDialog(fParent); for (int i = lstFormsToEnable.Count - 1; i >= 0; i--) { try { Form checkfrm = Application.OpenForms[i]; checkfrm.Enabled = true; } catch (Exception ex) { } } dummyForm.Close(); return result; } }

提交回复
热议问题