Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found a code that changes color of my title bar..but it draws a rectangle on title bar so there arent't buttons to close my window or to maximize my window..

<pre>
void CDlgEstimateTime::OnNcPaint()
{
	CDC* pDC = GetWindowDC();
	CRect CapRct;
	GetWindowRect(&CapRct);

	long myFillColour = RGB(200, 0, 0);
	long myTextColour = RGB(0, 0, 0);
	int x1 = GetSystemMetrics(SM_CXDLGFRAME);
	int y1 = GetSystemMetrics(SM_CYDLGFRAME);
	int x2 = CapRct.Width() - GetSystemMetrics(SM_CXDLGFRAME);
	int y2 = GetSystemMetrics(SM_CYICON) - GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYBORDER);

	CapRct.left = x1;
	CapRct.top = y1;
	CapRct.right = x2;
	CapRct.bottom = y2;
	pDC->FillSolidRect(&CapRct, myFillColour);

	CFont* pCurFont = GetFont();
	LOGFONT lf;
	pCurFont->GetLogFont(&lf);
	lf.lfItalic = TRUE;
	lf.lfWeight = FW_NORMAL;
	lf.lfWidth = 12;
	lf.lfHeight = 18;
	strcpy(reinterpret_cast<char*>(lf.lfFaceName), "Veranda");

	CFont capfont;
	capfont.CreateFontIndirect(&lf);
	pCurFont = pDC->SelectObject(&capfont);
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(GetSysColor(COLOR_CAPTIONTEXT));
	pDC->DrawText(_T("<Dialog Text>"), &CapRct, DT_CENTER | DT_VCENTER);
}


What I have tried:

I tried this code but I would like to change only color of my title bar
Posted
Updated 1-Oct-22 1:52am
v3

To manipulate the Caption of an MFC Window you can subclass a CWnd window. The samplecode CSubClassWnd by Paul Dilascia (Microsoft Systems Journal) showed the trick. You get all the messages before the CWnd object gets them, so you can process the messages you want in your own window procedure.

Unfortunately, it is quite confusing and the self-drawn title bar appearance changes. But you can adapt it to the usual look and you get a title bar with arbitrary colors, or a bitmap as background. This very old know-how still works with Windows10. The original sources are hard to find, but some working example for the "CustomCaption" can be found:

Custom Captions (Including Multi-line Captions)
https://www.codeproject.com/Articles/277/Custom-Captions-Including-Multi-line-Captions
The project creates a "CustomCaption.lib", which you can then easily include in your own C++ MFC projects.
 
Share this answer
 
The proposed code segment does not seem to work for either MFC dialogs or MFC programs. When used in a FrameWindow it crashes at the place with the font, because there a null pointer which is not queried.


I have already tried a few things to change colors with simple means. The title bar seems to be especially protected against changes.

There are probably 3 possibilities:

1. Completely disable the themes for the window.
I already suggested to disable the visual styles with SetWindowTheme().

2. Define a theme which has the desired look:
HTHEME hTheme = OpenThemeData();

In Windows Vista and later, the appearance of the non-client areas of application windows (the title bar, icon, window border, and caption buttons) is controlled by the DWM.

Sample: Custom Window Frame Using DWM
Appendix B: Painting the Caption Title
The following code demonstrates how to paint a caption title on the extended frame.

https://learn.microsoft.com/en-us/windows/win32/dwm/customframe#appendix-b-painting-the-caption-title

3. As far as OWNER_DRAWN is still possible you can draw everything yourself. Probably not suitable for MFC.
 
Share this answer
 
v3
The modern approach is to utilize the Desktop Windows Manager (DWM) API. Here is a tutorial on the topic : Custom Window Frame Using DWM - Win32 apps | Microsoft Learn[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900