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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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

 

posted @   CHHC  阅读(159)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示