2016-11-09 130 views
1

恐怕答案可能不是......但有些背景。要在尺寸逻辑超出可见边框的窗口上绘制自定义边框(就像它在Windows 10上那样),我在边缘周围添加了分层窗口以捕获消息,然后将它们转发到中央窗口。这工作得很好,直到表单模态显示,此时所有的边缘窗口都自动禁用。显然这是设计的......但我不确定是否有解决办法。我尝试制作中央窗口所拥有的边缘窗口,但那不起作用。当显示模态窗口时,有没有办法让其他窗口保持活动状态?

或者也许有一个更好的方法完全。

这里是问题的一个样本:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
     } 

     protected override void OnClick(EventArgs e) 
     { 
     base.OnClick(e); 

     Form f2 = new Form(); 

     f2.Text = "Non Modal"; 

     f2.Show(); 

     Form f3 = new Form(); 

     f3.Text = "Modal"; 

     f3.ShowDialog(this); 
     } 
    } 
+1

模式对话框仅禁用它的所有者。目前还不清楚你已经实现了什么设置,所以模态对话框也会禁用这些额外的窗口。 – IInspectable

+0

增加了一个示例来说明您所说的看起来不是这种情况。 – user109078

回答

0

我认为你可以假模态窗口,因此它不是模式,但禁用调用者。我在一个自己的项目中使用了这个。我这样做:

//Setup small Interface 
public interface IDialog 
{ 
    //Our own Event which tell the caller if the Dialog is active/inactive 
    public event DialogChangedEventArgs DialogChanged; 
} 

//Setup EventArgs for our own Event 
public class DialogChangedEventArgs : EventArgs 
{ 
    public bool DialogActive{get;} 

    public DialogChangedEventArgs(bool dialogActive) 
    { 
     DialogActive = dialogActive; 
    } 
} 

//Setup the Form which act as Dialog in any other form 
public class Form2 : Form, IDialog 
{ 
    public event EventHandler<DialogChangedEventArgs> DialogChanged; 

    //If this Form is shown we fire the Event and tell subscriber we are active 
    private void Form2_Shown(object sender, EventArgs e) 
    { 
     DialogChanged?.Invoke(this, true); 
    } 

    //If the user close the Form we telling subscriber we go inactive 
    private void Form2_Closing(object sender, CancelEventArgs e) 
    { 
     DialogChanged?.Invoke(this, false); 
    } 
} 

public class Form1 : Form 
{ 
    //Setup our Form2 and show it (not modal here!!!) 
    private void Initialize() 
    { 
    Form2 newForm = new Form2(); 
    newForm.DialogChanged += DialogChanged; 
    newForm.Show(); 
    } 

    private void Form2_DialogChanged(object sender, DialogChangedEventArgs e) 
    { 
     //Now check if Form2 is active or inactive and enable/disable Form1 
     //So just Form1 will be disabled. 
     Enable = !e.DialogActive; 
    } 
} 

这很简单。只需使用一个事件来告诉你的第一个表格:Hey iam第二个表格并激活。然后,您可以在第二个活动时禁用第一个窗体。您可以完全控制哪些表单处于活动状态。希望这可以帮助。

+0

使用EnableWindow可能会更好,因此mainform上的所有控件都不会更改为禁用外观,但我认为这种技术可以很好地工作! – user109078

相关问题