开发者

label in MDI parent form do not remain in background of child form?

开发者 https://www.devze.com 2023-01-27 23:21 出处:网络
Please guide and help me. I have a MDI parent form which has a label at its center (to display application name in center). On opening a form in this MDI parent, this label should appear on back side

Please guide and help me.

I have a MDI parent form which has a label at its center (to display application name in center). On opening a form in this MDI parent, this label should appear on back side of newly opened form, but on showing a child form, label appears in front of newly opened form (appears like newly opened form is between label and MDI parent).

How to manage it please开发者_StackOverflow中文版 guide me.

thanks


This will hide the label while you have active MDI Children en show it again once there is no active child anymore.

    private void Form1_MdiChildActivate(object sender, EventArgs e)
    {
        if (ActiveMdiChild != null)
            label1.SendToBack();
        else
            label1.BringToFront();
    }

I hope this helps.


public partial class MyMdiForm : Form
{
    public MyMdiForm()
    {
        InitializeComponent();
        foreach (Control control in Controls)
        {
            if (control is MdiClient)
                control.Paint += mdiBackgroundPaint;
        }
    }

    private void mdiBackgroundPaint(object sender, PaintEventArgs e)
    {
        var mdi = sender as MdiClient;
        if (mdi == null) return;

        e.Graphics.Clip = new System.Drawing.Region(mdi.ClientRectangle);
        e.Graphics.DrawString("*** YOUR NAME HERE ***",this.Font,Brushes.Red,100F,100F);
    }
}


The problem is that your label is not added to the MdiClient (i.e. the grey Mdi container) but to the form.

But unfortunately, AFAIK, it's not possible to add controls to an MdiClient.

The only way is drawing what you want on the Paint event of the MdiClient, as suggested in this article:

http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp


Well, I did one trick and it works for me. We usually write an application name in the center and expect it to show to the user. And many people here said that MdiParent is only for Forms and not for other tools like we cannot hide label/panel behind MdiChild form.

So what I did is I wrote all the things like application name, contact, email etc etc etc in a new Form say frmMdiBody, Set its formBorderStyle = None and set the desired length of the form, StartPosition = CenterScreen and in Timer.Tick, I wrote the following : (Didn't work for me in Load event)

  Dim NewMDIChild As frmMdiBody = MdiChildren.OfType(Of frmMdiBody)().SingleOrDefault
  If NewMDIChild Is Nothing Then
      NewMDIChild = New frmMdiBody
      NewMDIChild.MdiParent = Me
      NewMDIChild.Show()
  End If

This above code also checks if there is one form open so that it won't open many frmMdiBody again and again as we are writing in Timer.Tick event

Someone can rectify me if I am wrong. I'd do the changes too if seems appealing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号