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);
        }
    }
    /// 
    /// Class used to preserve / restore state of the form
    /// 
    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;
        }
    }
}

下面是使用的代码:
        /// 
        /// 正常的窗体状态
        /// 
        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/

你可能感兴趣的:(C#学习笔记)