Click here to Skip to main content
15,314,594 members
Articles / Programming Languages / XML
Posted 31 May 2003


47 bookmarked

FlatMenusForm - A Class, which Makes the Default 3D Menus Look Flat

Rate me:
Please Sign up or sign in to vote.
3.27/5 (19 votes)
31 May 20034 min read
How to change the default 3D look in Windows 9x, NT and WinXP classic appearance menus

This is menu's default look:

The menu with its default look

And this is the menu with the new look:

The menu with its flat look


I wanted to learn how to change the default 3D look of the menus in Windows XP classic appearance, because when implementing owner-draw menus, windows gives you only the client area of the menu window for drawing. And when I have Windows XP appearance, it is just fine with its flat menus, but when switching to classic - it is awful, the 3D border isn't fitting the menu items at all. Then, I started looping through the .NET Framework SDK searching solution of my problem and finally I gave up - there was not such a class or enum or whatever... Then (as I am a little bit stubborn and wanted to learn the know-how), I went deeper in the Platform SDK and Win32 APIs...

Basic Idea

In a few words, the idea is subclassing the default window class Windows provides for its menus. This subclassing is made by using P/Invokes and calling native APIs.

Using the Code

First, how to give your menus a flat look - simply inherit from Base class and that's it! I added two extra properties to FlatMenuForm:

  1. BorderColor - use it to change the border color around the menus
  2. MenuStyle - it is enumeration which consists of two fields - Flat and Default. Use MenuStyle.Default to use the default menu look and the other for the flat look.

Now let's start from the very beginning - my first efforts in doing custom painting on the menu window. At first, I tried something like this:

IntPtr hdc = GetWindowDC(mainMenu1.Handle);
Graphics g = Graphics.FromHdc(hdc);
Rectangle r = new Rectangle(0,0,(int)g.VisibleClipBounds.Width-1, 
g.DrawRectangle(new Pen(borderColor),r);

Well, it did not work at all, because I always got an exception "Out of Memory" (it is because, as I traced why that is so, an INVALID_WINDOW_HANDLE win32 error is thrown when calling GetWindowDC ). I tried that code with a MenuItem.Handle property but got the same exception. And then I started reading for subclassing a window and changing its default WndProc. And then an idea arose - why not try to subclass the menu window first... Everything seems OK till now, but how to subclass a window when I don't have a valid window handle? Now in help comes the SetWindowsHookEx API with WH_CALLWNDPROC hook type specified - it installs a hook procedure that monitors messages before the system sends them to the destination window procedure.

hookHandle = SetWindowsHookEx(4,hookProc,IntPtr.Zero,Win32.GetWindowThreadProcessId(Handle,0));
//WH_CALLWNDPROC is defined as 4 in winuser.h

The second parameter of that function is of great importance - it is the address of my HookProc which will monitor for special messages (by the way, when you have to declare API in managed code and you have a function pointer, you use delegate ). Here is the HookProc delegate declaration:

delegate int HookProc(int code, IntPtr wparam, ref Win32.CWPSTRUCT cwp);

The CWPSTRUCT structure defines the message parameters passed to a WH_CALLWNDPROC hook procedure:

public struct CWPSTRUCT
  public IntPtr lparam;
  public IntPtr wparam;
  public int message;
  public IntPtr hwnd;

As I needed the window (I mean the main form window) to be hooked when constructed, I put this SetWindowsHookEx call in the form's constructor. And here is the implementation of the Hook procedure:

int Hooked(int code, IntPtr wparam, ref Win32.CWPSTRUCT cwp)
    case 0:  //HC_ACTION -> this means that the hook 
             //procedure should process the message contained in CWPSTRUCT
        case 0x0001://WM_CREATE - catch this before the window is created
          string s = string.Empty;
          char[] className = new char[10];
          //Get the window class name
          int length = Win32.GetClassName(cwp.hwnd,className,9);
          //Convert it to string
          for(int i=0;i<length;i++)
            s += className[i];
          //Now check if the window is a menu
          if(s == "#32768")//System class for menu
            //if true - subclass the window
            defaultWndProc = SetWindowLong(cwp.hwnd, (-4), subWndProc);
  return Win32.CallNextHookEx(hookHandle,code,wparam, ref cwp);
  //Let other applications use hook codes

Another great difficulty was to get the appropriate window class. This is system defined class for use only by the system but its name is given in the Platform SDK documentation - it is "#32768". And when get the right class - subclass it using SetWindowLong API with GWL_WNDPROC value which sets a new address for the window procedure ((-4) stands for GWL_WNDPROC). The subWndProc parameter is delegate of type MyWndProc:

delegate int MyWndProc(IntPtr hwnd,int msg,IntPtr wparam,IntPtr lparam);

The implementation of the SubclassWndProc:

int SubclassWndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
  case 0x0085:     //WM_NCPAINT
   IntPtr menuDC  = Win32.GetWindowDC(hwnd);                    
   Graphics g = Graphics.FromHdc(menuDC);
   return 0;                    
  case 0x0317:     //WM_PRINT
   int result = Win32.CallWindowProc(defaultWndProc,hwnd,msg,wparam,lparam);
   menuDC  = wparam;
   g = Graphics.FromHdc(menuDC);
   //Draw the border around the menu
   return result;
return Win32.CallWindowProc(defaultWndProc,hwnd,msg,wparam,lparam);//In all other cases 
                                                                   //use default wndproc

It may seem strange, but Windows sends a WM_PRINT message AFTER WM_NCPAINT. I spent hours and hours trying to understand what is wrong with my code and why it is not working until I put a simple tracer to the SubclassWndProc and found out what messages are sent to the menu window. And when processing the WM_PRINT message, it all worked fine - BINGO! Finally, I changed the default appearance of the menu window!

I wanted also to process the WM_NCCALCSIZE message in order to reduce the non-client area of the menu but failed... Any suggestion on how this might be done in managed code (I achieved it in MFC) would be very much appreciated! Also, I couldn't override the default implementation of WM_WINDOWPOSCHANGING and WM_WINDOWPOSCHANGED - I lost the default system animation...

And yet another thing - I haven't tested this code on other platforms (mine is Windows XP) so if you find some bugs in it please, let me know!!!

Points of Interest

I have also added an implementation of owner-draw menus with flat look and to some extent, they now really look like the Visual Studio .NET ones! I haven't implemented the shadow on the right side of the top menu items. May be it might be achieved by getting the desktop window DC, draw on it and then invalidate that rectangle - if I have enough time, I will try it.

Here is what I finally got:

The menu with its flat look

Well, that's it!

Once again - any comments or suggestions or even criticism are welcome!


  • 1st June, 2003: First revision


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Georgi Atanasov
Team Leader Telerik Corp.
Bulgaria Bulgaria
.NET & C# addicted. Win8 & WinRT enthusiast and researcher @Telerik.

Comments and Discussions

QuestionStartPosition CenterScreen not working anymore ! Pin
Le_MuLoT15-Nov-05 9:10
MemberLe_MuLoT15-Nov-05 9:10 
AnswerRe: StartPosition CenterScreen not working anymore ! Pin
Le_MuLoT15-Nov-05 9:54
MemberLe_MuLoT15-Nov-05 9:54 
GeneralStackOverflowException with .NET 2.0 Pin
andrew|7-Nov-05 8:47
Memberandrew|7-Nov-05 8:47 
GeneralRe: StackOverflowException with .NET 2.0 Pin
thomasholme20-Jun-07 3:56
Memberthomasholme20-Jun-07 3:56 
GeneralRe: StackOverflowException with .NET 2.0 Pin
shellscape20-Jun-07 4:21
Membershellscape20-Jun-07 4:21 
GeneralWM_ERASEBKGND msg problem Pin
martin_bozhilov18-Apr-05 6:23
Membermartin_bozhilov18-Apr-05 6:23 
Generalreducing NCarea, WM_PRINT is not needed Pin
inspirer28-Sep-04 20:44
Memberinspirer28-Sep-04 20:44 
QuestionWhere is the menuLibrary?? Pin
MarcosB10-May-04 9:18
MemberMarcosB10-May-04 9:18 
GeneralMenuLibrary.dll Pin
Sefai3-Aug-03 21:45
MemberSefai3-Aug-03 21:45 
Generalwindows 98 Pin
Giancarlo Aguilera18-Jul-03 10:53
MemberGiancarlo Aguilera18-Jul-03 10:53 
GeneralFullyCustomMenu code Pin
wondersquirrel3-Jun-03 11:30
Memberwondersquirrel3-Jun-03 11:30 
GeneralA dormant issue... Pin
Vlad Vissoultchev2-Jun-03 9:43
MemberVlad Vissoultchev2-Jun-03 9:43 

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.