C#关于Windows窗体最大化所引起的闪烁问题的处理经验

1:窗体最大化前,窗体最好没有任何控件在显示,并且控件不要有背景图,等窗体最大化后才显示控件以及加载背景图。
2:最大化源码(从网上搜回来的:)

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace XXXX.Definition
{
    public class WinApi
    {
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);

        [DllImport("user32.dll")]
        public static extern void
            SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);

        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0x0040

        public static int ScreenX
        {
            get { return GetSystemMetrics(SM_CXSCREEN); }
        }

        public static int ScreenY
        {
            get { return GetSystemMetrics(SM_CYSCREEN); }
        }

        public static void SetWinFullScreen(IntPtr hwnd)
        {
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
        }
    }
    /// <summary>
    /// Class used to preserve / restore state of the form
    /// </summary>
    public class FormState
    {
        private FormWindowState winState;
        private FormBorderStyle brdStyle;
        private bool topMost;
        private Rectangle bounds;

        private bool isMaximized = false;

        public bool IsMaximized
        {
            get { return isMaximized; }
        }

        public void Maximize(Form targetForm)
        {
            if (!isMaximized)
            {
                targetForm.SuspendLayout();
                isMaximized = true;
                Save(targetForm);
                targetForm.WindowState = FormWindowState.Maximized;
                targetForm.FormBorderStyle = FormBorderStyle.None;
                targetForm.TopMost = false;
                WinApi.SetWinFullScreen(targetForm.Handle);
                targetForm.ResumeLayout(true);
            }
        }

        public void Save(Form targetForm)
        {
            targetForm.SuspendLayout();
            winState = targetForm.WindowState;
            brdStyle = targetForm.FormBorderStyle;
            topMost = targetForm.TopMost;
            bounds = targetForm.Bounds;
            targetForm.ResumeLayout(true);
        }

        public void Restore(Form targetForm)
        {
            targetForm.WindowState = winState;
            targetForm.FormBorderStyle = brdStyle;
            targetForm.TopMost = topMost;
            targetForm.Bounds = bounds;
            isMaximized = false;
        }
    }
}

下面是使用的代码:
        /// <summary>
        /// 正常的窗体状态
        /// </summary>
        public  FormState FrmState=new FormState();
//窗体点击esc时切换
        private void MaxForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
                if (FrmState.IsMaximized)
                    FrmState.Restore(this);
                else
                    FrmState.Maximize(this);
        }
下面要留意的,就是闪烁问题的,大家留意下:
窗体的Shown事件:
        private void frm_Main_Shown(object sender, EventArgs e)
        {
            this.Visible = false;
            FrmState.Maximize(this);
            this.Visible = true;
            //加载背景图
            AutoLoadBackImage();
	 }
其实,窗体闪烁严重的在于窗体有很多控件或者是存在背景图。因此,在最大化前必须让窗体处于控件量最小,没有背景图的情况。在最大化后再对控件以及背景图进行加载。

原创作品出自努力偷懒,转载请说明 文章出处 http://blog.csdn.net/kfarvid 或  http://www.cnblogs.com/kfarvid/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值