Click here to Skip to main content
Licence CPOL
First Posted 17 Jan 2009
Views 90,580
Downloads 4,393
Bookmarked 190 times

Vista Aero ToolStrip on Non-Client Area

By | 8 Feb 2009 | Article
Place a ToolStrip on Aero's Glass non-client area.

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:

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:

nonclient_003.jpg

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

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.

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:

nonclient_005.jpg

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

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

  • 17 Jan. 09 - Original post.
  • 06 Feb. 09 - Focus bug fixed.

License

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

About the Author

Jose Menendez Póo

Team Leader

Mexico Mexico

Member

Jose Manuel Menéndez Poó
 
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.
 
Questions and stuff by twitter: @menendezpoo
 
Jose Runs Goplek in Mexico
www.goplek.com
 
Blog
menendezpoo.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThe controls at top position of form move to title bar ,can't been showned. [modified] PinmemberMember 268840715:48 30 Dec '11  
GeneralMDI makes it white and undraggable PinmemberAETCoder12:12 16 May '11  
GeneralMy vote of 5 Pinmemberleesong3:09 3 Mar '11  
GeneralMy vote of 5 PinmemberVB_ Coder7:12 22 Feb '11  
GeneralMinimize/Maximize bug Pinmembercode17008:13 2 Jan '11  
QuestionHow to reduce distance from top and the left edge?? Pinmembervirtyaluk4:42 16 Oct '10  
QuestionHow to aero on client area but no Non-client area? Pinmemberstevenyoung2:05 8 Oct '10  
GeneralMy vote of 5 PinmemberMukit, Ataul8:30 13 Aug '10  
GeneralBlack bar bug Pinmemberscruffyfox3:45 6 Aug '10  
GeneralRe: Black bar bug [modified] Pinmember3of420:45 6 Sep '10  
GeneralMy vote of 5 PinmemberMikeDaMan259416:14 2 Jul '10  
GeneralMy vote of 5 PinmemberAlex Essilfie2:11 30 Jun '10  
GeneralArticle excellent! But... PinmemberEvgeniy Stepanow11:45 21 May '10  
QuestionHave DWM Draw the Window Icon? Pinmembersidneys110:47 7 May '10  
Generaldwmapi.dll missing PinmemberDivyesh Kharade2:07 26 Apr '10  
GeneralMDIContainer PinmemberSkippy II8:13 14 Apr '10  
QuestionWindow's context menu PinmemberDaHGBR4:41 6 Sep '09  
GeneralMinimize/Restore Resize bug Pinmemberplehxp6:46 29 May '09  
GeneralRe: Minimize/Restore Resize bug Pinmemberme65532117:25 8 Nov '09  
GeneralRe: Minimize/Restore Resize bug Pinmembercode17008:17 2 Jan '11  
GeneralRe: Minimize/Restore Resize bug PinmemberAETCoder12:11 16 May '11  
Generalcool article PinmemberDonsw12:54 17 Feb '09  
GeneralFYI .. re: Localizable Pinmembermjmeans23:11 9 Feb '09  
QuestionUnusual bug Pinmemberultrafez0013:45 30 Jan '09  
AnswerRe: Unusual bug PinmemberJose M. Menendez Poó4:18 6 Feb '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Feb 2009
Article Copyright 2009 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid