Click here to Skip to main content
15,908,173 members
Articles / Programming Languages / C#

Vista Aero ToolStrip on Non-Client Area

Rate me:
Please Sign up or sign in to vote.
4.89/5 (61 votes)
8 Feb 2009CPOL2 min read 224K   13.3K   225   58
Place a ToolStrip on Aero's Glass non-client area
In this post, you will see a solution for giving the Ribbon an orb and the quick access toolbar.

NCBDemo

Introduction

When trying to figure out how to give the Ribbon an orb and the quick access toolbar, I found myself in trouble. I hope this example works for something you may need.

Background

Take a look to some Office app with a ribbon (Word, Excel, PowerPoint). There are some things that you might notice at first sight:

AeroNonClientAreaButtons/nonclient_002.jpg

  1. The orb is drawn half on the client area, half on the title bar.
  2. The quick access tools are drawn (and sensed) on the title bar.
  3. The title of the window is centered between the available space and the start of the caption buttons (minimize, maximize, close).

Using the Code

Although it seems like the orb and the tools are drawn on the non-client area, they are not. This trick is done with one of the features of Vista's Desktop Window Manager (DWM).

How? Well, a regular window has a client area and a non-client area:

AeroNonClientAreaButtons/nonclient_003.jpg

But, a form with the trick done has no client area at all:

AeroNonClientAreaButtons/nonclient_004.jpg

So, why does the form look like it has a client area? A very well known trick: DwmExtendFrameIntoClientArea.

Turns out that when extending the frame into the client area, caption buttons (minimize, maximize, close) are drawn by default. To give life to these caption buttons, the DwmDefWindowProc function is called.

C#
protected override void WndProc(ref Message m)
{
    ...

    int dwmHandled = Dwm.DwmDefWindowProc(m.HWnd, m.Msg, 
                         m.WParam, m.LParam, out result);

    if (dwmHandled == 1)
    {
        m.Result = result;
        return;
    }

    ... 

Now, we have to provide basic frame features like window moving and resizing. This is solved by handling the WM_NCHITTEST message on the WndProc. The return value for this message can specify which part of the frame the mouse is currently hitting. By a measurement of areas, the code returns the correct result:

  • Caption
  • North
  • South
  • East
  • West
  • NorthWest
  • NorthEast
  • SouthEast
  • SouthWest

Drawing the ToolStrip

To add a ToolStrip to the window, I just placed it as usual:

AeroNonClientAreaButtons/nonclient_005.jpg

and modified the render process so the ToolStrip gets no background:

C#
public class NonClientAreaRenderer
        : ToolStripProfessionalRenderer
{

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
    {
        if (e.ToolStrip.IsDropDown)
        {
            base.OnRenderToolStripBackground(e);
        }
        else
        {
            //Clear so Aero glass covers the area
            e.Graphics.Clear(Color.Transparent);
        }
    }
    ...

And, there you go. A nice toolbar just on the non-client area, but you know, it's not the non-client area. :)

History

  • 17th January, 2009 - Original post
  • 6th February, 2009 - Focus bug fixed

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
United States United States
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is product, user interface, and usability.
- TypeScript / React expert

@geeksplainer

Comments and Discussions

 
Generaldwmapi.dll missing Pin
Divyesh Kharade26-Apr-10 2:07
Divyesh Kharade26-Apr-10 2:07 
GeneralMDIContainer Pin
Skippy II14-Apr-10 8:13
Skippy II14-Apr-10 8:13 
QuestionWindow's context menu Pin
DaHGBR6-Sep-09 4:41
DaHGBR6-Sep-09 4:41 
GeneralMinimize/Restore Resize bug Pin
plehxp29-May-09 6:46
plehxp29-May-09 6:46 
GeneralRe: Minimize/Restore Resize bug Pin
me6553218-Nov-09 17:25
me6553218-Nov-09 17:25 
GeneralRe: Minimize/Restore Resize bug Pin
code17002-Jan-11 8:17
code17002-Jan-11 8:17 
GeneralRe: Minimize/Restore Resize bug Pin
AETCoder16-May-11 12:11
AETCoder16-May-11 12:11 
AnswerRe: Minimize/Restore Resize bug Pin
Boudi AlSayed22-Dec-12 11:59
Boudi AlSayed22-Dec-12 11:59 
Hey Add this Code To Fix Some Size Bugs :
VB
    Protected Overrides Sub SetBoundsCore( _
   ByVal x As Integer, _
   ByVal y As Integer, _
   ByVal width As Integer, _
   ByVal height As Integer, _
   ByVal specified As BoundsSpecified _
)
    End Sub

Generalcool article Pin
Donsw17-Feb-09 12:54
Donsw17-Feb-09 12:54 
GeneralFYI .. re: Localizable Pin
mjmeans9-Feb-09 23:11
mjmeans9-Feb-09 23:11 
QuestionUnusual bug Pin
ultrafez0030-Jan-09 13:45
ultrafez0030-Jan-09 13:45 
AnswerRe: Unusual bug Pin
JoseMenendez6-Feb-09 4:18
JoseMenendez6-Feb-09 4:18 
GeneralAbout XP... Pin
bobert_smallgeek22-Jan-09 14:46
bobert_smallgeek22-Jan-09 14:46 
GeneralRe: About XP... Pin
Jamie Nordmeyer9-Feb-09 4:07
Jamie Nordmeyer9-Feb-09 4:07 
GeneralRe: About XP... Pin
JoseMenendez9-Feb-09 4:12
JoseMenendez9-Feb-09 4:12 
GeneralRe: About XP... Pin
bobert_smallgeek10-Feb-09 16:38
bobert_smallgeek10-Feb-09 16:38 
GeneralNice. Pin
Jonathan C Dickinson20-Jan-09 19:22
Jonathan C Dickinson20-Jan-09 19:22 
QuestionHow to RUN with XP or Win2k3 OS Pin
pcm_it18-Jan-09 18:20
pcm_it18-Jan-09 18:20 
AnswerRe: How to RUN with XP or Win2k3 OS Pin
JoseMenendez18-Jan-09 18:28
JoseMenendez18-Jan-09 18:28 
GeneralRe: How to RUN with XP or Win2k3 OS Pin
Edward11127-Jan-09 15:34
Edward11127-Jan-09 15:34 
GeneralRe: How to RUN with XP or Win2k3 OS Pin
JoseMenendez27-Jan-09 15:43
JoseMenendez27-Jan-09 15:43 
GeneralGlitching Pin
Sidneys118-Jan-09 3:40
Sidneys118-Jan-09 3:40 
GeneralRe: Glitching Pin
VCSKicks18-Jan-09 17:52
VCSKicks18-Jan-09 17:52 
GeneralRe: Glitching Pin
trackmz2k19-Jan-09 0:04
trackmz2k19-Jan-09 0:04 
GeneralRe: Glitching Pin
Sidneys119-Jan-09 3:30
Sidneys119-Jan-09 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.