• C# WinForm MDI闪屏问题解决方案


    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace MDIFlash
    {
        public partial class FormMain : Form
        {
            Dictionary<int, Form> dicForm = new Dictionary<int, Form>();
            int FormCount = 0;
    
            public FormMain()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
            }
    
            // 解决MDI闪屏
    
            // 方案1
            //protected override CreateParams CreateParams
            //{
            //    get
            //    {
            //        CreateParams cp = base.CreateParams;
            //        cp.ExStyle |= 0x02000000;
            //        return cp;
            //    }
            //}
            // =======
    
            // 方案2
            [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
            public const int WM_MDINEXT = 0x224;
    
            public new void ActivateMdiChild(Form childToActivate)
            {
                if (this.ActiveMdiChild != childToActivate)
                {
                    MdiClient mdiClient = GetCurrentMdiControl();
                    if (mdiClient == null)
                    {
                        return;
                    }
                    int count = this.MdiChildren.Length;
                    Control form;  // next or previous MDIChild form
                    int pos = mdiClient.Controls.IndexOf(childToActivate);
                    if (pos < 0)
                        throw new InvalidOperationException("MDIChild form not found");
                    if (pos == 0 && count > 1)
                        form = mdiClient.Controls[1];  // get next and activate previous
                    else
                        form = mdiClient.Controls[pos - 1];  // get previous and activate next
    
    
                    // flag indicating whether to activate previous or next MDIChild
                    IntPtr direction = new IntPtr(pos == 0 ? 1 : 0);
    
                    // bada bing, bada boom
                    SendMessage(mdiClient.Handle, WM_MDINEXT, form.Handle, direction);
                }
            }
    
            public MdiClient GetCurrentMdiControl()
            {
                foreach (var c in this.Controls)
                {
                    if (c is MdiClient)
                    {
                        return c as MdiClient;
                    }
                }
                return null;
            }
            // =======
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 frmTemp = new Form1();
                frmTemp.MdiParent = this;
                frmTemp.Text = "窗体0" + (FormCount++).ToString();
                frmTemp.Dock = DockStyle.Fill;
                frmTemp.SetLabel(frmTemp.Text); 
                dicForm.Add(FormCount, frmTemp);
                frmTemp.Show();
            }  
    
            int showIndex = 1;
    
            private void button2_Click(object sender, EventArgs e)
            { 
                if (showIndex > dicForm.Count) return;
    
                // 方案1使用
                //dicForm[showIndex].Show();
                //dicForm[showIndex].BringToFront(); showIndex++;
    
                // 方案2使用
                ActivateMdiChild(dicForm[showIndex++]);
            }
        }
    }
    

     

    文件名:MDIFlash.rar

    qq:505645074
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
    记一次odoo创建新的模块时,但是在odoo web界面找不到应用的案例
    python实现格式化输出9*9乘法表
    format和urlencode的使用对比
    python字典小知识
    01
    深浅拷贝再回顾
    DRF的路由生成类的使用
  • 原文地址:https://www.cnblogs.com/chen1880/p/15437755.html
Copyright © 2020-2023  润新知