CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2005
    Posts
    218

    Customizing the titlebar color and buttons

    Hi,

    I want to customize the titlebar color and buttons (minimize, maximize and close). I found some samples on the web:
    - customcaptions (somewhere on codeproject or codeguru)
    - captionbutton (somewhere on codeproject or codeguru)
    - shadecap (http://www.microsoft.com/msj/0697/c0697.aspx)
    But they all suffer the same thing when using windows XP theme: the buttons are overpainted, but their clickregion is not right. When you click (mousepressed), you clearly see the old (classic) buttons.

    Are there any nice (working) samples of code of this?

  2. #2
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Customizing the titlebar color and buttons

    For Windows XP look for information about themes. I do not know any samples though.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Nov 2002
    Location
    Washington, D.C.
    Posts
    264

    Re: Customizing the titlebar color and buttons

    Your app's main window will recv WM_NCPAINT when it needs to draw the caption/buttons/frame (the non-client area). Just paint as you wish. Pretty simple.

    sulu
    Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Customizing the titlebar color and buttons

    To the best of my knowledge, it is not that simple in Windows XP.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Nov 2002
    Location
    Washington, D.C.
    Posts
    264

    Re: Customizing the titlebar color and buttons

    Sure it does. I use XP, and this code works fine

    Code:
    void NCPaint(HWND hWnd, HRGN hRgn)
    {
    	HDC hDC;
    	char s[] = "Hello World!";
    
    	hDC = GetWindowDC(hWnd);
    	TextOut(hDC, 0, 0, s, strlen(s));
    	ReleaseDC(hWnd, hDC);
    }
    
    
    BOOL CALLBACK MainDlgProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
    	case WM_NCPAINT:
    		DefWindowProc(hWnd, message, wParam, lParam);
    		NCPaint(hWnd, (HRGN) wParam);
    		return TRUE;
    // ...
    sulu
    Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Customizing the titlebar color and buttons

    I think SetWindowText would have better result than your code snippet.

    Am I missing something?
    Your code paints text with dc background (most likely white) on the top of original window text using system font, not default font.
    I fail to see setting title bar color and button drawing as requirement state.

    Anyway, is this code for Win32 application?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Nov 2002
    Location
    Washington, D.C.
    Posts
    264

    Re: Customizing the titlebar color and buttons

    I was just demonstrating (Microsoft-style, i.e. "hello world", bleh ) that you can use the WM_NCPAINT to draw your own title bar. I could have just as easily drawn a big rectangle over the titlebar... Just wanted to show that you can either draw the entire non-client area or, the better option, call DefWindowProc (as I demonstrated) and then redraw the border or the buttons or whatever.

    [edit]In MFC... You can use the ClassWizard to catch WM_NCPAINT, or use your CWinApp's PreTranslateMessage. I used API because it demonstrates the actual message interpretation.[/edit]

    sulu
    Last edited by mistersulu; July 8th, 2005 at 05:27 PM.
    Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy

  8. #8
    Join Date
    Feb 2005
    Posts
    218

    Re: Customizing the titlebar color and buttons

    Thanks for getting into this, guys.

    The code you gave is pretty straightforward but didn't answer my question. I did already try to paint in the nonclient area. The painting itself isn't too hard, I think. You can see it e.g. in the code of shadecap (http://www.microsoft.com/msj/0697/c0697.aspx). Also see attachment and click on the buttons, you'll see what I mean.

    The problem is I don't know how to solve these bad effects.
    Attached Files Attached Files

  9. #9
    Join Date
    Feb 2005
    Posts
    218

    Re: Customizing the titlebar color and buttons

    Okay, I found another sample, which is http://www.codeproject.com/dialog/skinedcaptionbars.asp

    With some bugfixes, this code works great.
    At least already in classic theme, but when using a winxp theme, problems arise when resizing. The problem are the borders of a window. The coder used code like
    Code:
    void CBeeMainFrame::OnNcPaint() 
    {
    	CMDIFrameWnd::OnNcPaint();//first normal painting
    
    	... //here he starts overpainting the caption, and not the borders
    Off course this causes flickering, and also the effect with the borders is not so good. I added a screenshot of this problem.

    How to solve this 'bug' in this code? Or better, how to draw the borders in OnNcPaint() so I can paint it in my own color?
    Attached Images Attached Images
    Last edited by timv; July 11th, 2005 at 04:22 AM.

  10. #10
    Join Date
    Nov 2002
    Location
    Washington, D.C.
    Posts
    264

    Re: Customizing the titlebar color and buttons

    Since the flickering is caused by essentially "painting" the non-client area twice, the only option is to control all of the painting yourself (although the nonclient area should not have to be refreshed very often... are you Invalidating the entire window rect manually?) It would be nice if you could provide an HDC to the NCPaint function, so that you could backbuffer it's operations, do your stuff, THEN blit to the screen, but you can only do that with a bit of hacking.

    Although, I the MFC code for OnNCPaint is easy enough to find (In the 'SRC' directory for my VS6 Install). You could just copy that code, paste it into yours, and draw the border yourself in there... and never have to call the default (i.e. remove the line "CMDIFrameWnd::OnNcPaint();"). From what I can see, you'll just need to change 'COLOR_WINDOWFRAME' to your own color.

    sulu
    Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy

  11. #11
    Join Date
    Feb 2005
    Posts
    218

    Re: Customizing the titlebar color and buttons

    Thank you mrsulu, that wasn't so hard to do.

    Actually now, it looks quite good.
    One problem is that when I click the min/max/close buttons (put my left mouse button down) the 'old' buttons are painted :-( Like in the screenshot.
    I tried to find where this painting happens, but I don't understand.

    I have a similar problem when maximising a window: while it's maximising you can see the original (color of the) titlebar.
    How to fix these two issues?
    Attached Images Attached Images
    Last edited by timv; July 12th, 2005 at 07:00 AM.

  12. #12
    Join Date
    Feb 2005
    Posts
    218

    Re: Customizing the titlebar color and buttons

    Quote Originally Posted by timv
    I have a similar problem when maximising a window: while it's maximising you can see the original (color of the) titlebar.
    How to fix these two issues?
    I still have this question.
    Or perhaps it is possible to make this effect disappear using some windowstyle? (I'm talking about the color of the titlebar while it is minimizing/maximizing)





    ----
    to be complete: about the previous post: the problem with the captionbuttons has to be solved with WS_SYSMENU

  13. #13
    Join Date
    Aug 2005
    Posts
    14

    Re: Customizing the titlebar color and buttons

    Hi,

    I have the exactly the same problem of yours.
    Can you let me know how you solved the problem?

    I have also two more problems.
    One is that I can't make the corners of titlebar transparent to look round.
    And the other is that I also have to add bitmap background in toolbar like the titlebar.

    If you solved the problem please provide any samples or snippets.
    Thanks
    Attached Images Attached Images

  14. #14
    Join Date
    Feb 2005
    Posts
    218

    Re: Customizing the titlebar color and buttons

    I don't have a solution for the color while resizing.

    I also have no idea how to make the bar transparent at some places, I'm sorry.

    About this: "And the other is that I also have to add bitmap background in toolbar like the titlebar." -> Do you mean you want to have a toolbar with a background? If so, you can find articles of that on codeguru.

    About the old buttons showing up while clicking: You need to paint everything yourself, so do not call the baseclass' ::OnNcPaint(); U also have to do that without the WS_SYSMENU style
    Code:
    //something like: (you can do it in precreatewindow or oninitdialog)
    		long gwl = GetWindowLong(this->m_hWnd,GWL_STYLE);
    		gwl = gwl & ~WS_SYSMENU;
    		SetWindowLong(this->m_hWnd,GWL_STYLE,gwl);
    //or just do this before painting:
    		ModifyStyle(WS_SYSMENU,0);

  15. #15
    Join Date
    Apr 2009
    Posts
    1

    Wink Re: Customizing the titlebar color and buttons

    Use the link below if you just want to customize caption without handling WM_NCPAINT event.

    http://www.codeproject.com/KB/MFC/Co...lebarText.aspx

    Regards,
    Arun
    BOSCH

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured