Skip to content

Commit 2c6b668

Browse files
YellowNestge0rdi
andauthored
Add dark mode support to Classic Explorer toolbar and status bar (#2553)
Add dark mode support to Classic Explorer Port and harden the Classic Explorer dark-mode work developed by @DartVanya for issue #361. Keep this change limited to toolbar, tooltip, rebar, and custom status-bar rendering; toolbar locking remains separate. Co-authored-by: ge0rdi <ge0rdi@users.noreply.github.com>
1 parent e1fe220 commit 2c6b668

5 files changed

Lines changed: 380 additions & 0 deletions

File tree

‎Src/ClassicExplorer/ExplorerBHO.cpp‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <dwmapi.h>
2020
#include <Ntquery.h>
2121
#include <algorithm>
22+
#include <Vsstyle.h>
2223

2324
// CExplorerBHO - a browser helper object that implements Alt+Enter for the folder tree
2425

@@ -593,6 +594,128 @@ LRESULT CALLBACK CExplorerBHO::SubclassStatusProc8( HWND hWnd, UINT uMsg, WPARAM
593594
return 0;
594595
}
595596
}
597+
598+
if (uMsg==WM_SETTINGCHANGE && IsColorSchemeChangeMessage(lParam))
599+
{
600+
SetWindowTheme(hWnd,ShouldAppsUseDarkMode()?L"DarkMode_Explorer":NULL,NULL);
601+
::RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_FRAME);
602+
}
603+
604+
if (ShouldAppsUseDarkMode())
605+
{
606+
if (uMsg==WM_NCPAINT)
607+
{
608+
LRESULT result=DefSubclassProc(hWnd,uMsg,wParam,lParam);
609+
HDC hdc=GetWindowDC(hWnd);
610+
if (hdc)
611+
{
612+
RECT rc;
613+
GetWindowRect(hWnd,&rc);
614+
OffsetRect(&rc,-rc.left,-rc.top);
615+
rc.bottom=rc.top+1;
616+
SetDCBrushColor(hdc,RGB(0x3A,0x3A,0x3A));
617+
FillRect(hdc,&rc,(HBRUSH)GetStockObject(DC_BRUSH));
618+
ReleaseDC(hWnd,hdc);
619+
}
620+
return result;
621+
}
622+
623+
if (uMsg==WM_ERASEBKGND)
624+
{
625+
HDC hdc=(HDC)wParam;
626+
RECT rc;
627+
if (GetClipBox(hdc,&rc)!=ERROR)
628+
{
629+
SetDCBrushColor(hdc,RGB(0x1C,0x1C,0x1C));
630+
FillRect(hdc,&rc,(HBRUSH)GetStockObject(DC_BRUSH));
631+
}
632+
return 1;
633+
}
634+
635+
if (uMsg==WM_PAINT)
636+
{
637+
PAINTSTRUCT ps;
638+
HDC hdc=BeginPaint(hWnd,&ps);
639+
if (!hdc)
640+
return 0;
641+
642+
int saved=SaveDC(hdc);
643+
RECT client;
644+
GetClientRect(hWnd,&client);
645+
SetDCBrushColor(hdc,RGB(0x1C,0x1C,0x1C));
646+
FillRect(hdc,&ps.rcPaint,(HBRUSH)GetStockObject(DC_BRUSH));
647+
648+
HFONT font=(HFONT)DefSubclassProc(hWnd,WM_GETFONT,0,0);
649+
if (font)
650+
SelectObject(hdc,font);
651+
SetBkMode(hdc,TRANSPARENT);
652+
SetTextColor(hdc,RGB(0xFF,0xFF,0xFF));
653+
654+
SIZE textSize={};
655+
GetTextExtentPoint32(hdc,L"T",1,&textSize);
656+
int smallIcon=GetSystemMetrics(SM_CXSMICON);
657+
658+
// Let the status theme draw a DPI-aware resize grip.
659+
HTHEME theme=OpenThemeData(hWnd,L"STATUS");
660+
if (theme)
661+
{
662+
RECT grip;
663+
grip.left=client.right-GetSystemMetrics(SM_CXHSCROLL)+(GetDpi(hWnd)<USER_DEFAULT_SCREEN_DPI*1.75?1:6);
664+
grip.top=client.bottom-GetSystemMetrics(SM_CYVSCROLL);
665+
grip.right=client.right;
666+
grip.bottom=client.bottom;
667+
DrawThemeBackground(theme,hdc,SP_GRIPPER,0,&grip,&grip);
668+
CloseThemeData(theme);
669+
}
670+
671+
SetDCBrushColor(hdc,RGB(0x5E,0x5E,0x5E));
672+
HBRUSH separatorBrush=(HBRUSH)GetStockObject(DC_BRUSH);
673+
int parts=(int)DefSubclassProc(hWnd,SB_GETPARTS,0,0);
674+
for (int i=0;i<parts;i++)
675+
{
676+
RECT rc;
677+
if (!DefSubclassProc(hWnd,SB_GETRECT,i,(LPARAM)&rc))
678+
continue;
679+
680+
RECT intersection;
681+
if (!IntersectRect(&intersection,&rc,&ps.rcPaint))
682+
continue;
683+
684+
RECT separator=rc;
685+
LRESULT length=DefSubclassProc(hWnd,SB_GETTEXTLENGTH,i,0);
686+
int textLength=LOWORD(length);
687+
int flags=HIWORD(length);
688+
689+
HICON icon=(HICON)DefSubclassProc(hWnd,SB_GETICON,i,0);
690+
if (icon)
691+
{
692+
DrawIconEx(hdc,rc.left+2,rc.top+((rc.bottom-rc.top)/2-smallIcon/2),icon,0,0,0,NULL,DI_NORMAL);
693+
rc.left+=smallIcon+4;
694+
}
695+
696+
WCHAR text[1024];
697+
if (textLength>0 && textLength<(int)_countof(text) && !(flags&SBT_OWNERDRAW))
698+
{
699+
DefSubclassProc(hWnd,SB_GETTEXT,i,(LPARAM)text);
700+
rc.left+=2;
701+
rc.top+=(rc.bottom-rc.top)/2-textSize.cy/2;
702+
DrawText(hdc,text,-1,&rc,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
703+
}
704+
705+
if (parts>1 && i!=parts-1 && !(flags&SBT_NOBORDERS) && separator.right<client.right-16)
706+
{
707+
separator.left=separator.right-1;
708+
separator.bottom--;
709+
FillRect(hdc,&separator,separatorBrush);
710+
}
711+
}
712+
713+
RestoreDC(hdc,saved);
714+
EndPaint(hWnd,&ps);
715+
return 0;
716+
}
717+
}
718+
596719
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
597720
}
598721

@@ -1303,6 +1426,8 @@ HRESULT STDMETHODCALLTYPE CExplorerBHO::SetSite( IUnknown *pUnkSite )
13031426

13041427
SendMessage(m_Status8,SB_SIMPLE,FALSE,0);
13051428
SetWindowSubclass(m_Status8,SubclassStatusProc8,(UINT_PTR)this,flags);
1429+
if (ShouldAppsUseDarkMode())
1430+
SetWindowTheme(m_Status8,L"DarkMode_Explorer",NULL);
13061431
}
13071432
else
13081433
{

‎Src/ClassicExplorer/ExplorerBand.cpp‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <shdeprecated.h>
2020
#include <propkey.h>
2121
#include <algorithm>
22+
#include <uxtheme.h>
2223

2324
///////////////////////////////////////////////////////////////////////////////
2425

@@ -229,6 +230,17 @@ LRESULT CALLBACK CBandWindow::ToolbarSubclassProc( HWND hWnd, UINT uMsg, WPARAM
229230
// Also refresh the buttons when the folder settings change
230231
if (pThis->HasFolderSettings())
231232
::PostMessage((HWND)dwRefData,CBandWindow::BWM_UPDATETOOLBAR,0,0);
233+
234+
if (IsColorSchemeChangeMessage(lParam))
235+
{
236+
HWND tooltips=(HWND)::SendMessage(pThis->GetToolbar(),TB_GETTOOLTIPS,0,0);
237+
if (tooltips)
238+
SetWindowTheme(tooltips,ShouldAppsUseDarkMode()?L"DarkMode_Explorer":NULL,NULL);
239+
240+
HWND rebar=::GetParent((HWND)dwRefData);
241+
if (rebar)
242+
::RedrawWindow(rebar,NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_FRAME|RDW_ALLCHILDREN);
243+
}
232244
}
233245
if (uMsg==WM_PAINT)
234246
{
@@ -239,8 +251,149 @@ LRESULT CALLBACK CBandWindow::ToolbarSubclassProc( HWND hWnd, UINT uMsg, WPARAM
239251
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
240252
}
241253

254+
// Custom-draw the Classic Explorer toolbar on Windows 10+ so it follows the
255+
// Windows application theme. Keeping this on the band window preserves the
256+
// existing toolbar message handling and right-click behavior.
257+
LRESULT CALLBACK CBandWindow::BandSubclassProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
258+
{
259+
CBandWindow *pThis=(CBandWindow*)uIdSubclass;
260+
if (uMsg!=WM_NOTIFY || !lParam || !pThis || GetWinVersion()<WIN_VER_WIN10)
261+
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
262+
263+
LPNMHDR header=(LPNMHDR)lParam;
264+
if (header->hwndFrom!=pThis->GetToolbar() || header->code!=NM_CUSTOMDRAW)
265+
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
266+
267+
LPNMTBCUSTOMDRAW draw=(LPNMTBCUSTOMDRAW)lParam;
268+
DWORD stage=draw->nmcd.dwDrawStage;
269+
if (!ShouldAppsUseDarkMode())
270+
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
271+
272+
if (stage==CDDS_PREPAINT)
273+
{
274+
FillRect(draw->nmcd.hdc,&draw->nmcd.rc,pThis->m_DarkBackgroundBrush);
275+
return CDRF_DODEFAULT|CDRF_NOTIFYITEMDRAW;
276+
}
277+
278+
if (stage==CDDS_ITEMPREPAINT)
279+
{
280+
TBBUTTONINFO info={sizeof(info),TBIF_STYLE|TBIF_STATE|TBIF_IMAGE};
281+
if (::SendMessage(pThis->GetToolbar(),TB_GETBUTTONINFO,(WPARAM)draw->nmcd.dwItemSpec,(LPARAM)&info)==-1)
282+
return CDRF_DODEFAULT;
283+
284+
bool dropDown=(info.fsStyle&(BTNS_DROPDOWN|BTNS_WHOLEDROPDOWN))!=0;
285+
bool checked=(info.fsState&TBSTATE_CHECKED)!=0;
286+
287+
// Checked toolbar buttons need explicit rendering. The standard toolbar
288+
// theme otherwise paints a light checked background in dark mode.
289+
if (checked && !dropDown && info.iImage!=I_IMAGENONE)
290+
{
291+
size_t itemIndex=(size_t)draw->nmcd.lItemlParam;
292+
HIMAGELIST images=(HIMAGELIST)::SendMessage(header->hwndFrom,TB_GETIMAGELIST,0,0);
293+
if (images && itemIndex<pThis->m_Items.size())
294+
{
295+
const wchar_t *label=pThis->m_Items[itemIndex].label;
296+
int iconSize=GetSettingInt(GetSettingBool(L"UseBigButtons")?L"LargeIconSize":L"SmallIconSize");
297+
SIZE textSize={};
298+
RECT itemRect=draw->nmcd.rc;
299+
RECT paintRect=itemRect;
300+
paintRect.right++;
301+
HDC hdc=draw->nmcd.hdc;
302+
303+
int saved=SaveDC(hdc);
304+
HBRUSH brush=(HBRUSH)GetStockObject(DC_BRUSH);
305+
SetDCBrushColor(hdc,RGB(0x4D,0x4D,0x4D));
306+
HRGN background=CreateRoundRectRgn(paintRect.left,paintRect.top,paintRect.right,paintRect.bottom,4,4);
307+
if (background)
308+
{
309+
FillRgn(hdc,background,brush);
310+
SetDCBrushColor(hdc,RGB(0x63,0x63,0x63));
311+
FrameRgn(hdc,background,brush,1,1);
312+
}
313+
314+
if (label)
315+
{
316+
SetBkMode(hdc,TRANSPARENT);
317+
SetTextColor(hdc,RGB(0xFF,0xFF,0xFF));
318+
SelectObject(hdc,(HGDIOBJ)::SendMessage(header->hwndFrom,WM_GETFONT,0,0));
319+
GetTextExtentPoint32(hdc,L"T",1,&textSize);
320+
}
321+
322+
if (!GetSettingBool(L"ToolbarListMode"))
323+
{
324+
int iconY=itemRect.top+((itemRect.bottom-itemRect.top)/2-(iconSize+(label?textSize.cy:0))/2)+1-(label?2:0);
325+
ImageList_Draw(images,info.iImage,hdc,itemRect.left+((itemRect.right-itemRect.left)/2-iconSize/2)+1,iconY,ILD_NORMAL);
326+
if (label)
327+
{
328+
RECT textRect=itemRect;
329+
textRect.left++;
330+
textRect.top=iconY+iconSize+1;
331+
DrawText(hdc,label,-1,&textRect,DT_SINGLELINE|DT_TOP|DT_CENTER);
332+
}
333+
}
334+
else
335+
{
336+
int iconX=itemRect.left+(label?4:5);
337+
ImageList_Draw(images,info.iImage,hdc,iconX,itemRect.top+((itemRect.bottom-itemRect.top)/2-iconSize/2)+1,ILD_NORMAL);
338+
if (label)
339+
{
340+
RECT textRect=itemRect;
341+
textRect.left=iconX+iconSize+4;
342+
textRect.top+=(textRect.bottom-textRect.top)/2-textSize.cy/2;
343+
DrawText(hdc,label,-1,&textRect,DT_SINGLELINE|DT_TOP|DT_LEFT);
344+
}
345+
}
346+
347+
if (background)
348+
DeleteObject(background);
349+
RestoreDC(hdc,saved);
350+
return CDRF_SKIPDEFAULT;
351+
}
352+
}
353+
354+
draw->clrText=RGB(0xFF,0xFF,0xFF);
355+
UINT flags=CDRF_DODEFAULT|TBCDRF_USECDCOLORS;
356+
if (dropDown)
357+
flags|=CDRF_NOTIFYPOSTPAINT;
358+
else
359+
{
360+
draw->clrHighlightHotTrack=RGB(0x47,0x47,0x47);
361+
flags|=TBCDRF_HILITEHOTTRACK;
362+
}
363+
return flags;
364+
}
365+
366+
if (stage==CDDS_ITEMPOSTPAINT)
367+
{
368+
RECT rc=draw->nmcd.rc;
369+
UINT dpi=GetDpi(hWnd);
370+
int triangleLeft=rc.right-(dpi>USER_DEFAULT_SCREEN_DPI?ScaleForDpi(hWnd,10):9);
371+
int triangleTop=(rc.bottom-rc.top)/2+MulDiv(2,USER_DEFAULT_SCREEN_DPI,dpi);
372+
int width=ScaleForDpi(hWnd,6);
373+
width-=width%2;
374+
POINT vertices[]={{triangleLeft,triangleTop},{triangleLeft+width,triangleTop},{triangleLeft+width/2,triangleTop+width/2}};
375+
376+
int saved=SaveDC(draw->nmcd.hdc);
377+
SetDCPenColor(draw->nmcd.hdc,RGB(0xDE,0xDE,0xDE));
378+
SetDCBrushColor(draw->nmcd.hdc,RGB(0xDE,0xDE,0xDE));
379+
SelectObject(draw->nmcd.hdc,GetStockObject(DC_PEN));
380+
SelectObject(draw->nmcd.hdc,GetStockObject(DC_BRUSH));
381+
Polygon(draw->nmcd.hdc,vertices,_countof(vertices));
382+
RestoreDC(draw->nmcd.hdc,saved);
383+
return CDRF_DODEFAULT;
384+
}
385+
386+
return CDRF_DODEFAULT;
387+
}
388+
242389
LRESULT CBandWindow::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
243390
{
391+
if (GetWinVersion()>=WIN_VER_WIN10)
392+
{
393+
m_DarkBackgroundBrush=CreateSolidBrush(RGB(0x19,0x19,0x19));
394+
m_DarkBorderBrush=CreateSolidBrush(RGB(0x3A,0x3A,0x3A));
395+
}
396+
244397
ParseToolbar();
245398

246399
bool bLabels=false;
@@ -261,6 +414,14 @@ LRESULT CBandWindow::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
261414
m_Toolbar.SendMessage(TB_SETMAXTEXTROWS,1);
262415
SetWindowSubclass(m_Toolbar,ToolbarSubclassProc,(UINT_PTR)this,(DWORD_PTR)m_hWnd);
263416

417+
if (GetWinVersion()>=WIN_VER_WIN10)
418+
{
419+
SetWindowSubclass(m_hWnd,BandSubclassProc,(UINT_PTR)this,(DWORD_PTR)m_hWnd);
420+
HWND tooltips=(HWND)m_Toolbar.SendMessage(TB_GETTOOLTIPS,0,0);
421+
if (tooltips && ShouldAppsUseDarkMode())
422+
SetWindowTheme(tooltips,L"DarkMode_Explorer",NULL);
423+
}
424+
264425
int iconSize=GetSettingInt(GetSettingBool(L"UseBigButtons")?L"LargeIconSize":L"SmallIconSize");
265426
if (iconSize<8) iconSize=8;
266427
else if (iconSize>128) iconSize=128;
@@ -397,6 +558,20 @@ LRESULT CBandWindow::OnDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& b
397558
if (it->menuIconD) DeleteObject(it->menuIconD);
398559
}
399560
m_Items.clear();
561+
562+
if (GetWinVersion()>=WIN_VER_WIN10)
563+
RemoveWindowSubclass(m_hWnd,BandSubclassProc,(UINT_PTR)this);
564+
if (m_DarkBackgroundBrush)
565+
{
566+
DeleteObject(m_DarkBackgroundBrush);
567+
m_DarkBackgroundBrush=NULL;
568+
}
569+
if (m_DarkBorderBrush)
570+
{
571+
DeleteObject(m_DarkBorderBrush);
572+
m_DarkBorderBrush=NULL;
573+
}
574+
400575
bHandled=FALSE;
401576
return 0;
402577
}
@@ -2031,6 +2206,34 @@ LRESULT CALLBACK CExplorerBand::RebarSubclassProc( HWND hWnd, UINT uMsg, WPARAM
20312206
}
20322207
}
20332208

2209+
if (GetWinVersion()>=WIN_VER_WIN10 && ShouldAppsUseDarkMode())
2210+
{
2211+
CExplorerBand *pThis=(CExplorerBand*)uIdSubclass;
2212+
if (uMsg==WM_ERASEBKGND)
2213+
{
2214+
HDC hdc=(HDC)wParam;
2215+
RECT rc;
2216+
if (GetClipBox(hdc,&rc)!=ERROR)
2217+
FillRect(hdc,&rc,pThis->m_BandWindow.GetDarkBackgroundBrush());
2218+
return 1;
2219+
}
2220+
if (uMsg==WM_NCPAINT)
2221+
{
2222+
LRESULT result=DefSubclassProc(hWnd,uMsg,wParam,lParam);
2223+
HDC hdc=GetWindowDC(hWnd);
2224+
if (hdc)
2225+
{
2226+
RECT rc;
2227+
GetWindowRect(hWnd,&rc);
2228+
OffsetRect(&rc,-rc.left,-rc.top);
2229+
rc.top=rc.bottom-1;
2230+
FillRect(hdc,&rc,pThis->m_BandWindow.GetDarkBorderBrush());
2231+
ReleaseDC(hWnd,hdc);
2232+
}
2233+
return result;
2234+
}
2235+
}
2236+
20342237
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
20352238
}
20362239

0 commit comments

Comments
 (0)