我是新的Windows应用程序。 与C#和.NET。 我想要像我附加的图像。 当我的popup窗口打开时,我想要mdi父页面被隐藏或disbaled。 (就像我们在Web应用程序或JQuery中popup)
有可能吗? 如果是的我怎么能这样做。
请帮忙。
您可以通过使用Windows.Form
的opacity属性来实现此目的。为此,创建一个新窗体,设置其不透明度(例如:.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(); } }
在Microsoft Windows中,并没有真正的子窗口的概念。 弹出窗口或模态/非模态对话就像任何其他窗口一样,它们可以放置在屏幕上的任何位置,所以可以超出你想要作为父窗口的界限。
有一些网络概念,只是不能在桌面上运行良好!
这对我来说可以。 但打开后的小孩应该是小孩形式上的第一个按钮。
我已经找到了这个链接上最优雅的解决方案,它甚至有动画(虽然我为我的目的删除了这部分)。 作者是“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; } }