代码之家  ›  专栏  ›  技术社区  ›  Phil Wright

自定义绘制Aero标题栏,不扩展到工作区

  •  2
  • Phil Wright  · 技术社区  · 14 年前

    我的WinForms应用程序在Vista/Windows7上具有标准的Aero玻璃外观。

    我想自定义绘制窗口标题栏,这样它就保留了Aero玻璃的外观,带有玻璃最小/最大/关闭按钮,但没有标题文本和窗口图标。我尝试过通过覆盖wm_cpaint来实现这一点,但覆盖此事件始终会导致移除玻璃。

    是否有人知道如何在玻璃到位的情况下覆盖WM NCPAINT,以便有效地将玻璃区域吸引过来?

    1 回复  |  直到 13 年前
        1
  •  8
  •   Alex    14 年前

    WM_NCPAINT

    internal class NonClientRegionAPI
    {
        [DllImport( "DwmApi.dll" )]
        public static extern void DwmIsCompositionEnabled( ref bool pfEnabled );
    
        [StructLayout( LayoutKind.Sequential )]
        public struct WTA_OPTIONS
        {
            public WTNCA dwFlags;
            public WTNCA dwMask;
        }
    
        [Flags]
        public enum WTNCA : uint
        {
            NODRAWCAPTION = 1,
            NODRAWICON = 2,
            NOSYSMENU = 4,
            NOMIRRORHELP = 8,
            VALIDBITS = NODRAWCAPTION | NODRAWICON | NOSYSMENU | NOMIRRORHELP
        }
    
        public enum WINDOWTHEMEATTRIBUTETYPE : uint
        {
            /// <summary>Non-client area window attributes will be set.</summary>
            WTA_NONCLIENT = 1,
        }
    
        [DllImport( "uxtheme.dll" )]
        public static extern int SetWindowThemeAttribute(
            IntPtr hWnd,
            WINDOWTHEMEATTRIBUTETYPE wtype,
            ref WTA_OPTIONS attributes,
            uint size );
    }
    

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            // Set your options. We want no icon and no caption.
            SetWindowThemeAttributes( NonClientRegionAPI.WTNCA.NODRAWCAPTION | NonClientRegionAPI.WTNCA.NODRAWICON );
        }
    
        private void SetWindowThemeAttributes( NonClientRegionAPI.WTNCA attributes )
        {
            // This tests that the OS will support what we want to do. Will be false on Windows XP and earlier,
            // as well as on Vista and 7 with Aero Glass disabled.
            bool hasComposition = false;
            NonClientRegionAPI.DwmIsCompositionEnabled( ref hasComposition );
            if( !hasComposition )
                return;
    
            NonClientRegionAPI.WTA_OPTIONS options = new NonClientRegionAPI.WTA_OPTIONS();
            options.dwFlags = attributes;
            options.dwMask = NonClientRegionAPI.WTNCA.VALIDBITS;
    
            // The SetWindowThemeAttribute API call takes care of everything
            NonClientRegionAPI.SetWindowThemeAttribute(
                this.Handle,
                NonClientRegionAPI.WINDOWTHEMEATTRIBUTETYPE.WTA_NONCLIENT,
                ref options,
                (uint)Marshal.SizeOf( typeof( NonClientRegionAPI.WTA_OPTIONS ) ) );
        }
    }
    

    http://img708.imageshack.us/img708/1972/noiconnocaptionform.png

    推荐文章