Non Client Painting on Aero Glass Window

Non client painting on aero glass window

Skype cheats it, and has a little sliver along the top of their window; where they can draw it.

You can see it with Process Explorer to SpyXX:

Sample Image

See also

MSDN: Custom Window Frame Using DWM

Custom draw Aero title bar without extending into client area

I don't have a solution involving WM_NCPAINT, but I have a solution that does what you want it to do, and perhaps cleaner than the WM_NCPAINT-version would be.

First define this class. You'll use its types and functions to achieve your desired functionality:

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 );
}

Next, in your form, you simply do this:

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 ) ) );
}
}

Here's the result:

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

I normally make a base class that implements Form with all my funky extended behavior and then let my actual forms implement that base class, but if you only need it for one Form, just put it all in there.

Documentation and API Samples for drawing on Windows Aero Glass (DWM, GDI, GDI+) for all Win32 programmers

This is a subset of my "Glass" bookmarks folder, the result of a lot of research / searching on this topic. I've found all of these useful for learning about glass and solving various problems drawing on it. Most of these include Delphi code, but I've noted where it's for another language.

Plain Glass links

  • Using translucent windows with Delphi: good introduction (from the very basics) for using Glass in Delphi forms
  • Custom drawing on glass: covers how to draw a bitmap or other image on a glass area of the window. Covers alpha channels etc too, good overview
  • Using glass in a VC++ project: covers turning glass on, drawing text, handling notifications etc - a good general introduction to how it works. A lot of the underlying details are handled by the VCL (eg the GlassFrame property and TForm internals look after a lot of this) but it's very useful to understand the basics of how it's implemented at an API level anyway
  • How to draw on the non-client area: this shows how to draw something like Office's toolbar in the title bar. .Net code, but translatable
  • Setting up a custom title bar: very detailed article about non-client-area drawing (in Delphi, so Delphi code). Followed up by part 2, which demonstrates completely taking over the entire window and mimicking the standard title bar yourself. These two articles will let you mimic Office and Chrome as you requested in the question
  • How to set up various VCL controls to work best on a glass area: the VCL does not support glass very well. You'll often get artifacts, or controls simply not drawing properly at all, no matter what you do to try and solve it. This article lists the basic VCL visual components (labels, buttons, progress bars, etc) and what to set up for each so they draw perfectly, or at least 'as well as possible' when they're placed on a glass area

Advanced, or tangentially related:

  • How desktop composition works, with GDI and DirectX surfaces
  • List of desktop manager APIs (only some of which are Aero-related)

Handling WM_NCPAINT breaks DWM glass rendering on Vista/Aero

When toggling between Aero/Glass and your custom rendered frame it you can use the following to explicitly control the non-client area rendering policy:

DWMNCRENDERINGPOLICY policy = DWMNCRP_ENABLED; // DWMNCRP_DISABLED to toggle back
DwmSetWindowAttribute(hwnd,
DWMWA_NCRENDERING_POLICY,
(void*)&policy,
sizeof(DWMNCRENDERINGPOLICY));


Related Topics



Leave a reply



Submit