如何制作无模形式的“块"? [英] How to make a modeless form "block"?

查看:63
本文介绍了如何制作无模形式的“块"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以同时显示两个表单/窗口的应用程序.第一个通过Form.Show()方法显示,因此它是无模的并且浮动到一侧.第二种形式是通过Form.ShowDialog()显示的,因此它是模式形式的,并且会阻塞.

I have an application that displays two forms/windows at the same time. The first one is shown with the Form.Show() method, so it is modeless and floats off to one side. The second form is shown with Form.ShowDialog(), so it is modal, and it blocks.

它阻塞的事实很重要,因为与第一种形式(基本上只是装饰)不同,第二种形式获取重要信息,所以我不希望我的程序在关闭之前继续运行.

The fact that it blocks is important, because unlike the first form (which is basically just decoration), the second form acquires important information, so I don't want my program to continue running until it closes.

不幸的是,我现在需要允许用户在显示第二个表单的同时与第一个表单进行一些有限的交互(调整其大小的能力以及其他较小的视觉调整).

Unfortunately, I now need to allow the user to have some limited interaction with the first form (the ability to resize it, and other minor visual adjustments) while the second form is also displayed.

很明显,当第二个对话框是模态对话框时,这是行不通的.因此,要么我需要找到一种方法来使第二个表单处于无模式状态,但在打开时仍会阻塞……否则,我需要使第二个表单在模态可见时以某种方式可以访问.

Obviously, that doesn't work while that second dialog is modal. So either I need to find a way to make that second form modeless yet still blocking while it is open...or else I need to make the first form somehow accessible while the second form is modally visible.

我是一位经验丰富的Java Swing程序员,但是我对.NET表单还很陌生,所以这里可能有一个明显的答案,我只是因为对NET api不太熟悉而错过了? /p>

I'm an experienced Java Swing programmer, but I'm fairly new to .NET forms, so maybe there's an obvious answer here that I'm missing simply because I'm not very familiar with the .NET api?

推荐答案

可以通过相反的方法进行操作,同时保持启用助手表单.但是,这需要一些技巧. ShowDialog()方法调用迭代应用程序中的顶级窗口,并在其上调用EnableWindow()以禁用它们.您可以通过自己调用EnableWindow来撤消此操作.棘手的一件事是,ShowDialog()无法阻止. BeginInvoke()可以解决此问题.这是一个示例:

It is possible by doing it the other way around, keeping the helper form enabled. That however requires a few tricks. The ShowDialog() method call iterates the toplevel windows in the app and calls EnableWindow() on them to disable them. You can undo this by calling EnableWindow yourself. One thing that's tricky is that you can't, ShowDialog() blocks. BeginInvoke() can fix that. Here's an example:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    protected override void OnLoad(EventArgs e) {
      mHelper = new Form2();
      mHelper.Show();
      mHelper.FormClosed += (s, ea) => mHelper = null;
    }
    Form2 mHelper;

    private void EnableForm2() {
      if (mHelper.IsHandleCreated) EnableWindow(mHelper.Handle, true);
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool EnableWindow(IntPtr hWnd, bool enable);

    private void button1_Click(object sender, EventArgs e) {
      this.BeginInvoke(new Action(() => EnableForm2()));
      using (var dlg = new Form3()) {
        if (dlg.ShowDialog(this) == DialogResult.OK) {
          // etc...
        }
      }
    }
  }

这篇关于如何制作无模形式的“块"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆