3

我是 Windows 应用程序的新手。使用 C# 和 .NET。我想要一些像我附上的图像。当我的弹出窗口打开时,我希望 mdi 父页面被遮蔽或禁用。(就像我们在 Web 应用程序或 Jquery 弹出窗口中所做的那样)

在此处输入图像描述

有可能吗?如果是的话,我该怎么做。

请帮忙。

4

4 回答 4

1

您可以通过使用的opacity属性来实现Windows.Form 这一点,为此创建一个新表单,设置其不透明度(例如:.75)并在显示子窗口时将其显示在父级上。下面给出一个例子

There are three windows used here
1. ParentForm
2. OverlayForm
3. ChildForm

1. 家长表格

1. Create an instance of the Child form 
2. Create an Instance of the Overlayform, Pass the objects Instances of Child and Parent(current form) as a parameter to the Constructor
3. Then Show the OverLay Form by using  ShowDialog Method.

Code:

public partial class ParentForm : Form
{
    public ParentForm()
    {

        InitializeComponent();
    }

    private void ParentForm_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        ChildForm child1 = new ChildForm();
        // Create a new form.
        OverlayForm form2 = new OverlayForm(this, child1);
        child1.OverLay = form2;
        // Display the form as a modal dialog box.
        form2.ShowDialog(this);
    }
}

2.叠加形式

1. In the constructor store the childForm and ParentForm object in a local variables. 
       And Set the The properties (like width,height) to the Overlay Window
    2. In the OverlayForm_Load show the ChildForm window.

public partial class OverlayForm : Form
{
    public Form ParentForm { get; set; }
    public Form child { get; set; }
    public OverlayForm(Form parent, Form child)
    {
        InitializeComponent();
        this.child = child;
        this.ParentForm = parent;

        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.ShowInTaskbar = false;
        this.Width = ParentForm.Width;
        this.Height = ParentForm.Height;
        this.Top = ParentForm.Top;
        this.Left = ParentForm.Left;
        this.StartPosition = ParentForm.StartPosition;
        // Set the opacity to 75%.
        this.Opacity = .75;
    }

    private void OverlayForm_Load(object sender, EventArgs e)
    {
        child.Show();
        child.TopMost = true;
        child.Focus();
        child.BringToFront();
    }
}

这将使父窗体看起来模糊。我们还应该编写一些代码来关闭子窗体中的覆盖

3. 子表

1. Set the object of the Overlay  to a property in Child Window
2. And in the Form_Closing event of the child window, close the Overlay window.

public partial class ChildForm : Form
{
    //This is set in the Parent form where the child form instatce is created
    public Form OverLay { get; set; }
    public ChildForm()
    {
        InitializeComponent();
    }

    private void ChildForm_Load(object sender, EventArgs e)
    {
    }

    private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.OverLay.Close();
    }
}
于 2012-07-30T11:10:37.007 回答
0

对于 Microsoft Windows,实际上并没有子窗口的概念。弹出窗口或模态/非模态对话就像任何其他窗口一样,它们可以定位在屏幕上的任何位置,因此可以超出您所认为的父窗口的范围。

有些网络概念在桌面上不能很好地工作!

于 2012-07-30T08:19:56.627 回答
0

这对我来说可以。但是在打开子 fucus 后应该在子窗体上的第一个按钮上。

于 2015-03-23T09:06:01.217 回答
0

我在这个链接上找到了最优雅的解决方案,它甚至有动画(尽管我出于我的目的删除了那部分)。作者是“TommyCarlier”,定义如下:

class FadingOverlayForm : Form
    {
        readonly double fFinalOpacity;

        public FadingOverlayForm(Form owner, double finalOpacity)
        {
            StartPosition = FormStartPosition.Manual;
            FormBorderStyle = FormBorderStyle.None;
            ShowInTaskbar = false;
            Owner = owner;
            BackColor = Color.FromArgb(235, 245, 255); // or pick your own color
            fFinalOpacity = finalOpacity;
            if (fFinalOpacity < 0.01) fFinalOpacity = 0.01;
            else if (fFinalOpacity > 1.0) fFinalOpacity = 1.0;
        }

        public void FadeIn(TimeSpan duration)
        {
            Opacity = 0.01;
            Rectangle lWorkingArea = CalculateTotalScreenBounds();
            Bounds = new Rectangle(lWorkingArea.X - 150, lWorkingArea.Y - 150, 100, 100);
            Show();
            Bounds = new Rectangle(Owner.PointToScreen(Point.Empty), Owner.ClientSize);
            Animator.Animate(this, "Opacity", 0.01, fFinalOpacity, duration);
        }

        public void FadeOut(TimeSpan duration)
        {
            Animator.Animate(this, "Opacity", Opacity, 0, duration, EndFadeOut);
        }

        void EndFadeOut()
        {
            Form lOwner = Owner;
            Dispose();
            if (lOwner != null && !lOwner.IsDisposed)
                ActivateFirstOwnedForm(lOwner);
        }

        static void ActivateFirstOwnedForm(Form form)
        {
            foreach(Form lOwnedForm in form.OwnedForms)
                if (!lOwnedForm.IsDisposed)
                {
                    ActivateFirstOwnedForm(lOwnedForm);
                    return;
                }
            form.Activate();
        }

        static Rectangle CalculateTotalScreenBounds()
        {
            Rectangle lBounds = Rectangle.Empty;
            foreach(Screen lScreen in Screen.AllScreens)
                lBounds = Rectangle.Union(lBounds, lScreen.Bounds);
            return lBounds;
        }
    }

以下是如何使用它:

DialogResult ShowMyDialog(Form owner)
{
    using(MyDialog lDialog = new MyDialog())
    {
        FadingOverlayForm lOverlay = new FadingOverlayForm(owner, 0.6);
        lDialog.Owner = lOverlay;
        lOverlay.FadeIn(TimeSpan.FromSeconds(0.7));
        DialogResult lResult = lDialog.ShowDialog(lOverlay);
        lOverlay.FadeOut(TimeSpan.FromSeconds(0.1));
        return lResult;
    }
}
于 2015-07-30T21:46:45.187 回答