0

I want to change color of title bar of my application using mfc library..I used this code:

void CDlgMine::OnNcPaint() 
{

    int aElements[2] = { COLOR_WINDOW, COLOR_HOTLIGHT };

    DWORD aOldColors[2];
    DWORD aNewColors[2];

    aNewColors[0] = RGB(255,255, 255);  // light gray 
    aNewColors[1] = RGB(0, 0, 255);  // dark purple 

    int  flag = COLOR_HOTLIGHT;
    SetSysColors(2, aElements, aNewColors);
}

but sometimes my title bar becomes ble and sometimes becomes white without title..I don't know why

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
student_qt
  • 39
  • 3
  • 1
    [`OnNcPaint`](https://learn.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=msvc-170#onncpaint) is an event-handling function that is called when the application needs to *paint*, actually *draw* non-client areas of the Window. That's not really what you're doing. [`SetSysColors`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setsyscolors) should be called only once, not as part of an event-handler. – Some programmer dude Sep 27 '22 at 09:10
  • So I must not use OnNcPaint? I use this code in my OnInitDialog? – student_qt Sep 27 '22 at 09:23
  • I tried to insert my code in my OnInitDialog and color of my client area chenges..but title bar is white.. – student_qt Sep 27 '22 at 09:28
  • 1
    [`SetSysColor`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setsyscolors): *"It is best to respect the color settings specified by the user."* You're applying a global solution to a local problem. You *should* be handling the `WM_NCPAINT` message, but you shouldn't make global changes from the event handler. The documentation for [`WM_NCPAINT`](https://learn.microsoft.com/en-us/windows/win32/gdi/wm-ncpaint) is far more complete than its `OnNcPaint` equivalent (which is missing a vital parameter, too). – IInspectable Sep 27 '22 at 09:46
  • 2
    Furthermore, `SetSysColors()` affects the whole system, not only your application, so this is not acceptable, it's a choice that should be done by the user, not the application. You correctly override `OnNcPaint()` (handles the `WM_NCPAINT` message), only you should put some (custom) drawing there, instead of setting system-wide options. Search examples of `OnNcPaint()` calls, or handling of the `WM_NCPAINT` message, or even the `OnNcPaint()` functions in many MFC classes' implementation (in VS Find In Files in the sources directories - search for `::OnNcPaint`), for examples. – Constantine Georgiou Sep 27 '22 at 10:00
  • I found a derived class that draws an area rectangular for title bar but the title bar is completely redrawn not only changed color – student_qt Sep 27 '22 at 12:30
  • I tried a derived class: https://www.codeproject.com/Articles/717/A-Gradient-Title-Bar-for-modal-and-modeless-dialog...but all the title bar is drawn and not only changed color and my menu is hidden – student_qt Sep 27 '22 at 12:34
  • I don't think it is straightforward to do what you want. You would have to draw the title bar completely I think - including any items. I don't believe you can just draw rge same bar with different colour and existing controls like menus still show. – Andrew Truckle Sep 27 '22 at 14:14
  • Perhaps you are using the more modern version of MFC, which uses the MFC Feature Pack. There is implemented a lot of add-on functionality which may be overriding the place where you are implementing this. I would search for `OnUpdateFrameTitle` on the MFC source code, put a breakpoint there and see what is going on. I successfully overwrote the title by implementing an override of this function on my frame class. See https://stackoverflow.com/questions/66186021/mfc-after-applying-a-csplitterwnd-to-my-cchildframe-the-main-window-title-isnt/66209466#66209466 and its comments. – sergiol Oct 11 '22 at 16:17
  • See also https://stackoverflow.com/questions/37309042/remove-untitled-from-main-frame-window-caption/37311433#37311433 – sergiol Oct 11 '22 at 16:19

0 Answers0