XP视觉样式应用到Windows窗体应用程序

介绍 有时,Windows窗体程序默认情况下看起来像老式的Win32 API应用程序——他们根本不使用Windows XP视觉风格呈现控件。 首先,这个问题通常是为。net 1.1创建的项目。但是你可以遇到在。net 2.0(或更高版本)项目的移植,或必须与。net 1.1兼容。 解决方案 然而,它不是那么糟糕。微软允许我们包括XP主题支持到我们的项目中使用下列方法之一: 通过添加清单文件到您的项目。这是一个MSDN文章描述了这个。使用Application.EnableVisualStyles()方法调用,更简单,但有时车在。net 1.1。 就我个人而言,我更喜欢第二种方法,特别是在微软提出了一个解决方案的bug EnableVisualStyles()方法在。net 1.1。 所以,为了使XP风格在你的Windows窗体应用程序中,您只需要把Application.EnableVisualStyles()调用在你的项目的主要()过程,使额外Application.DoEvents()调用在。net 1.1解决可能出现的问题。这是所有吗?不完全是。 如果你只是把你的控制形式从工具箱中,大部分按钮、复选框和其他“按钮”控件还有老会即使你叫EnableVisualStyles()在程序的开始。为什么?因为所有这些组件有FlatStyle属性等于标准,默认情况下,它应该设置为系统。 所以,我们需要运行在每个表单上的所有控制和FlatStyle.System设置FlastStyle属性。为了简化所描述的所有任务,我写了一个特殊的XPStyle类包含几个静态方法。使用这些方法,您可以轻松地将XP风格的支持添加到现有的Windows窗体程序。 隐藏,收缩,复制Code

using System;
using System.Windows.Forms;

namespace Korzh.WinForms
{
    /// <summary>
    /// Represents different procedures that allows to turn
    /// on XP visual style for Windows Forms application
    /// </summary>

    public class XPStyle
    {
        /// <summary>
        /// Gets a value indicating whether XP themes feature is present.
        /// </summary>
        /// <value><c>true</c> if XP themes feature is present;
        ///           otherwise, <c>false</c>.</value>

        public static bool IsXPThemesPresent {
            get { 
                return OSFeature.Feature.IsPresent(OSFeature.Themes); 
            }
        }

        /// <summary>
        /// Enables the visual styles for application.
        /// </summary>
        public static void EnableVisualStyles() {
            if (!IsXPThemesPresent) return;
            Application.EnableVisualStyles();
            Application.DoEvents();
        }

        /// <summary>
        /// Applies the visual styles for some control.
        /// </summary>
        /// <paramname=&quot;control&quot;>The control.</param>

        public static void ApplyVisualStyles(Control control) {
            if (!IsXPThemesPresent) return;
            ChangeControlFlatStyleToSystem(control);
        }

        private static void ChangeControlFlatStyleToSystem(Control control) {
            // If the control derives from ButtonBase, 
            // set its FlatStyle property to FlatStyle.System.
            if(control.GetType().BaseType == typeof(ButtonBase)) {
                ((ButtonBase)control).FlatStyle = FlatStyle.System; 
            }

            // If the control holds other controls, iterate through them also.
            for(int i = 0; i < control.Controls.Count; i++) {
                ChangeControlFlatStyleToSystem(control.Controls[i]);
            }
        }
    }
}

现在,你应该叫EnableVisualStyles()方法,这类程序的开头,然后调用ApplyVisualStyles()为每个表单(最终形式的构造函数或加载事件处理程序)。 隐藏,复制Code

.  .  .  .  .  .  .
static void Main(string[] args) {
    XpStyle.EnableVisualStyles();

    Application.Run(new MainForm());
}
.  .  .  .  .  .  .


.  .  .  .  .  .  .

private void MainForm_Load(object sender, System.EventArgs e) {
    XPStyle.ApplyVisualStyles(this);
}

本文转载于:http://www.diyabc.com/frontweb/news10894.html

原文地址:https://www.cnblogs.com/Dincat/p/13473282.html