Custom Draw Window Title

Posted on 1997-12-06
Last Modified: 2008-02-01
As the title described, I wanted to draw the window title by myself. Just like Microsoft Word 7.0 and Norton AntiVirus. Their window title change from black to my title color from the left to the right.

If possible, also tell me how to draw the menu bar by my self.
Question by:sccheung
  • 3
  • 3

Accepted Solution

galkin earned 50 total points
ID: 1175023
You must handle several non client messages and perform your custom non client painting after processing default painting.
To get device contaext call GetWindowDC() or, if you are programming in VC++ create instance of CWindowDC class
CWindowDC dc(this). Remember when wrting code for WM_NCPAINT you must first call default non client painting implementation to draw other parts of non client area like window borders etc. If you are programming in VC++ call Default() function to do this and then create instance of CWindowDC.

Author Comment

ID: 1175024
I've tried to override OnNcPaint and OnNcActivate. But there is no change!
I declare them:

// Generated message map functions
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      afx_msg void OnNcPaint();
      afx_msg BOOL OnNcActivate(BOOL bActive);

and then write the code:

void CMainFrame::OnNcPaint()
      CWindowDC dc(this);
      CRect WinRect = CRect::CRect();
      dc.FillRect(WinRect, CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));

BOOL CMainFrame::OnNcActivate(BOOL bActive)
      CWindowDC dc(this);
      CRect WinRect = CRect::CRect();
      dc.FillRect(WinRect, CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));
      return true;

But the window frame doesn't turn black. Have I wrote anything wrong or I should handle more messages? If possible, please give me some sample code.

Expert Comment

ID: 1175025
I am sorry I guess(I am sorry if I am mistaken) you didn't add message macros to cpp file. Since you code seems correct. Set breakpoint in OnNcPaint and check whether debugger stops at it.
And also instead of Default function in this case call base class message handlers(CFrameWnd::OnNcPaint(), CFrameWnd::OnNcActivate()).
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Author Comment

ID: 1175026
I've tried to set a breakpoint in OnNcPaint. It doesn't stop. My code isn't executed at all. The window just continue painting with the default painting function.

Author Comment

ID: 1175027
What can I do now?

Expert Comment

ID: 1175028
Do the following:
1.Add afx_msg OnNcPaint() and afx_msg OnNcActivate(BOOL bActivate) message handlers to your frame class declaration.

2.Add ON_WM_NCPAINT() and ON_WM_NCACTIVATE() macros between

I guess you didn't add these macros so you failed in catching these messages

Add these messages handler implementations like

void CMainFrame::OnNcPaint()
      // Let Windows do what it usually does. Let the window
       // caption
       // be empty to avoid any Windows-initiated caption bar
       // drawing

      CWindowDC dc(this);

      CRect rc2, rc1;
    GetWindowRect(&rc2 );

      int x, y;
    // Compute the caption bar's origin. This window has a system box
    // a minimize box, a maximize box, and has a resizeable frame
       x = GetSystemMetrics( SM_CXSIZE ) +
           GetSystemMetrics( SM_CXBORDER ) +
           GetSystemMetrics( SM_CXFRAME );
       y = GetSystemMetrics( SM_CYFRAME );
         rc1.left = x; = y;

    // 2*x gives twice the bitmap+border+frame size. Since there are
    // only two bitmaps, two borders, and one frame at the end of the
    // caption bar, subtract a frame to account for this.
       rc1.right = rc2.right - rc2.left - 2*x -
                   GetSystemMetrics( SM_CXFRAME );
       rc1.bottom = GetSystemMetrics( SM_CYSIZE );
    // Render the caption. Use the active caption color as the text
    // background.
         dc.FillSolidRect(&rc1, 0x000000);
       dc.DrawText((LPSTR)"Left Justified Caption", -1,
               (LPRECT)&rc1, DT_LEFT );

BOOL CMainFrame::OnNcActivate(BOOL bActivate)
      // Add code here to draw caption when window is inactive.
         return TRUE;

// Fall through if wParam == TRUE, i.e., window is active.    

This code is not perfect, for instance there is filickering while drawing caption etc. but you can perform your custom painting.

I also refere you to Microsoft knowlege base article Q99046


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now