给MDI父窗体添加背景和解决闪烁的问题

 #region 给MDI 父窗体添加背景和解决闪烁的问题
        //1、在Properties/Resources.resx中添加名称为"BackgroundImage"的背景图片

 

//2、在以下位置添加BackgroundNoSplash();

public FrmMain()
        {

          

            InitializeComponent();


            //给MDI 父窗体添加背景和解决闪烁的问题
            BackgroundNoSplash();


        }

 

//以下代码复制到MDI主窗体代码中

        MdiClient mdiClient = new MdiClient();
       
        private void BackgroundNoSplash()
        {
            foreach (Control var in this.Controls)
            {
                if (var is MdiClient)
                {
                    mdiClient = var as MdiClient;
                    break;
                }
            }

            if (mdiClient != null)
            {
                mdiClient.Paint += new PaintEventHandler(OnMdiClientPaint);
                System.Reflection.MethodInfo mi = (mdiClient as Control).GetType().GetMethod("SetStyle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                mi.Invoke(mdiClient, new object[] { ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer
                 | ControlStyles.ResizeRedraw, true });

            }
        }

        private void OnMdiClientPaint(object sender, PaintEventArgs e)
         {
            Graphics g = e.Graphics;
             g.DrawImage(Properties.Resources.BackgroundImage, new Rectangle(0, 0, mdiClient.Width, mdiClient.Height));
             string msg = "系统名称+版本号+最后更新日期";
             SizeF size = e.Graphics.MeasureString(msg, this.Font);
             g.DrawString(msg, this.Font, new SolidBrush(Color.White), mdiClient.Width - size.Width, mdiClient.Height - size.Height);

         }

        #endregion

posted @ 2009-12-02 17:21  tiasys  阅读(940)  评论(1编辑  收藏  举报