我的WinForms应用程序在Vista / Windows 7上具有标准的Aero玻璃外观。
我想自定义绘制窗口标题栏,以便使用玻璃最小/最大/关闭按钮保留Aero玻璃外观,但没有标题文本和窗口图标。我通过覆盖WM_NCPAINT尝试了这一点,但是重写此事件总是会导致玻璃被移除。
是否有人知道如何使用玻璃覆盖WM_NCPAINT以便有效地正确地绘制玻璃区域?
答案 0 :(得分:8)
我没有涉及WM_NCPAINT
的解决方案,但我有一个解决方案可以完成您希望它执行的操作,并且可能比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
我通常使用我所有时髦的扩展行为创建一个实现Form的基类,然后让我的实际表单实现该基类,但如果你只需要一个Form,那就把它全部放在那里。