【问题标题】:How to forced focusing on child form?如何强制专注于子窗体?
【发布时间】:2013-06-22 11:44:17
【问题描述】:

当我们点击它的父窗体的客户区时,如何强制完全聚焦子窗体? [像 MessageBox 或 Error message 有这样的焦点,所以我们必须点击 messagebox 的 Ok 按钮。]

这是我的代码:

form = new Form2(); 
form.MdiParent = this; 
form.ShowDialog(ParentForm);

但它给了我错误:

非顶级表单的表单无法显示为模态 对话框。在调用之前从任何父表单中删除表单 显示对话框。

【问题讨论】:

  • 您能否展示一个您尝试完成此操作的代码示例?
  • 我使用 TopMost = true 但这不是我想要的。

标签: c# winforms focus


【解决方案1】:

原始答案 - 适用于非 MDIChild 使用 ShowDialog 方法使子窗体模态化。

ChildForm.ShowDialog (ParentForm);

要将 MDI 子窗体保持在顶部: 处理父窗体上的 MDIChildActivate 事件,并在其中将要保持可见的子窗体设置为处于活动状态:。在本例中,Form2 是模态窗体,Form3 是另一个用于测试的窗体。

private Form2 frm2;
private Form3 frm3;
private void Form1_Load(object sender, EventArgs e)
{
    frm2=new Form2();
    frm2.MdiParent=this ;
    frm2.Show();

    frm3= new Form3() ;
    frm3.MdiParent=this;
    frm3.Show();
}

private void Form1_MdiChildActivate(object sender, EventArgs e)
{
        frm2.Activate();
}

【讨论】:

  • 我试试这个,但这只有在我删除父表单时才有效。
  • 当与父表单一起使用时,它给我错误:不是顶级表单的表单不能显示为模式对话框。在调用 showDialog 之前从任何父窗体中删除该窗体。
  • form = new Form2(); form.MdiParent = 这个; form.ShowDialog(ParentForm);这是我的代码。
  • 您不能在 MDI 子窗体上使用 showDialog。您可以通过不设置 Mdiparent 使表单不是 MDI 子表单。
  • 使用Activate() 似乎可以解决问题,但effect 不像Dialog
【解决方案2】:

我想你想在MDI Parent FormTopLevel = false 中有某种MessageBox。为了使效果看起来像ShowDialog() 显示的窗口的真实效果,我认为这是唯一需要您为MDI child form 创建自定义表单的解决方案:

public class ChildForm : Form
{
    [DllImport("user32")]
    private static extern int FlashWindowEx(FLASHWINFO flashInfo);
    struct FLASHWINFO
    {
        public int cbSize;
        public IntPtr hwnd;
        public uint flags;
        public int count;
        public int timeOut;
    }
    protected override void WndProc(ref Message m)
    {            
        if (ParentForm != null)
        {                
            ChildForm dialog = ((Form1)ParentForm).Dialog;
            if (dialog != this && dialog!=null)
            {                    
                if (m.Msg == 0x84) //WM_NCHITTEST
                {
                    if (MouseButtons == MouseButtons.Left)
                    {
                        Flash(dialog.Handle);
                    }
                    return;
                }                    
            }
        }
        base.WndProc(ref m);
    }
    private void Flash(IntPtr handle)
    {
        FLASHWINFO flashInfo = new FLASHWINFO()
        {
            hwnd = handle,
            count = 9,
            cbSize = Marshal.SizeOf(typeof(FLASHWINFO)),
            flags = 3,
            timeOut = 50
        };
        FlashWindowEx(flashInfo); 
    }
    public void Flash()
    {
        Flash(Handle);
    }
    //This is to disable Tab when the Dialog is shown
    protected override bool ProcessTabKey(bool forward)
    {
        if(((Form1)ParentForm).Dialog == this) return true;
        return base.ProcessTabKey(forward);
    }
}
//Here is your MDI parent form
public class Form1 : Form {
   public Form1(){
       InitializeComponent();
       IsMdiContainer = true;
       f1 = new ChildForm();
       f2 = new ChildForm();
       f1.MdiParent = this;
       f2.MdiParent = this;
       //Get MDI Client
       foreach(Control c in Controls){
          if(c is MdiClient){
               client = c;
               break;
          }
       }
       client.Click += ClientClicked;           
   }
   ChildForm f1, f2; 
   MdiClient client;
   public ChildForm Dialog {get;set;}
   private void ClientClicked(object sender, EventArgs e){
       if(Dialog != null) Dialog.Flash();
       else {//Show dialog, you can show dialog in another event handler, this is for demonstrative purpose
           Dialog = new ChildForm(){MdiParent = this, Visible = true};
           ActivateMdiChild(Dialog);
           //Suppose clicking on the Dialog client area will close the dialog
           Dialog.Click += (s,ev) => {
              Dialog.Close();
           };
           Dialog.FormClosed += (s,ev) => {
              Dialog = null;
           };
       }
   }
}

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多