diff --git a/Src/Merge2.rc b/Src/Merge2.rc index 9842a22a0ab..4341f171801 100644 --- a/Src/Merge2.rc +++ b/Src/Merge2.rc @@ -12,7 +12,6 @@ // remains consistent on all systems. IDR_MAINFRAME ICON "res\\Merge.ico" -IDR_MAINFRAME_ICON_GRAY ICON "res\\Merge_gray.ico" IDR_MERGEPROJECT ICON "res\\MergeProject.ico" IDR_MERGEDOCTYPE ICON "res\\MergeDoc.ico" IDR_DIRDOCTYPE ICON "res\\MergeDir.ico" diff --git a/Src/TitleBarHelper.cpp b/Src/TitleBarHelper.cpp index cb67986a87e..495ccebc986 100644 --- a/Src/TitleBarHelper.cpp +++ b/Src/TitleBarHelper.cpp @@ -23,14 +23,14 @@ CTitleBarHelper::CTitleBarHelper() , m_bMouseTracking(false) , m_nTrackingButton(-1) , m_nHitTest(HTNOWHERE) + , m_icon(nullptr) + , m_icon_gray(nullptr) { } void CTitleBarHelper::Init(CWnd *pWnd) { m_pWnd = pWnd; - m_icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); - m_icon_gray = AfxGetApp()->LoadIcon(IDR_MAINFRAME_ICON_GRAY); } int CTitleBarHelper::GetTopMargin() const @@ -40,6 +40,7 @@ int CTitleBarHelper::GetTopMargin() const void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active) { + DelayLoadIcon(pWnd); HICON hIcon = active ? m_icon : m_icon_gray; if (hIcon == nullptr) return; @@ -334,3 +335,61 @@ void CTitleBarHelper::ReloadAccentColor() { CAccentColor::Get().Reload(); } + +HICON CTitleBarHelper::CreateGrayIcon(HICON hIcon) +{ + ICONINFO iconInfo; + GetIconInfo(hIcon, &iconInfo); + + BITMAP bitmap; + GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap); + const int width = bitmap.bmWidth; + const int height = bitmap.bmHeight; + const int pixsize = width * height; + + BITMAPINFO bmi; + ZeroMemory(&bmi, sizeof(BITMAPINFO)); + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biWidth = width; + bmi.bmiHeader.biHeight = height; + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 32; + bmi.bmiHeader.biCompression = BI_RGB; + + RGBQUAD* pixels = new RGBQUAD[pixsize]; + HDC hdc = GetDC(NULL); + GetDIBits(hdc, iconInfo.hbmColor, 0, height, pixels, &bmi, DIB_RGB_COLORS); + + for (int i = 0; i < pixsize; i++) + { + BYTE gray = (BYTE)(0.3 * pixels[i].rgbRed + 0.59 * pixels[i].rgbGreen + 0.11 * pixels[i].rgbBlue); + pixels[i].rgbRed = gray; + pixels[i].rgbGreen = gray; + pixels[i].rgbBlue = gray; + } + + HBITMAP hbmGray = CreateCompatibleBitmap(hdc, width, height); + SetDIBits(hdc, hbmGray, 0, height, pixels, &bmi, DIB_RGB_COLORS); + + ICONINFO grayIconInfo = iconInfo; + grayIconInfo.hbmColor = hbmGray; + HICON hGrayIcon = CreateIconIndirect(&grayIconInfo); + + DeleteObject(iconInfo.hbmColor); + DeleteObject(iconInfo.hbmMask); + DeleteObject(hbmGray); + ReleaseDC(NULL, hdc); + delete[] pixels; + + return hGrayIcon; +} + +void CTitleBarHelper::DelayLoadIcon(CWnd* pWnd) +{ + if (m_icon && m_icon_gray) + return; + m_icon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0); + if (m_icon == nullptr) + m_icon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM); + m_icon_gray = (m_icon == nullptr) ? nullptr : CreateGrayIcon(m_icon); +} diff --git a/Src/TitleBarHelper.h b/Src/TitleBarHelper.h index 845a4556418..f29ee4c94fb 100644 --- a/Src/TitleBarHelper.h +++ b/Src/TitleBarHelper.h @@ -41,6 +41,8 @@ class CTitleBarHelper { void ShowSysMenu(CPoint point); COLORREF GetIntermediateColor(COLORREF a, COLORREF b, float ratio); + HICON CreateGrayIcon(HICON hIcon); + void DelayLoadIcon(CWnd* pWnd); CWnd* m_pWnd; CSize m_size; diff --git a/Src/res/Merge_gray.ico b/Src/res/Merge_gray.ico deleted file mode 100644 index 5597ad0ebba..00000000000 Binary files a/Src/res/Merge_gray.ico and /dev/null differ diff --git a/Src/resource.h b/Src/resource.h index b0f9faeb289..5d52556a341 100644 --- a/Src/resource.h +++ b/Src/resource.h @@ -30,7 +30,6 @@ #define IDR_POPUP_PLUGIN_COMMAND_LINE_MENU 126 #define IDR_POPUP_PLUGIN_ADD_MENU 127 #define IDR_POPUP_DIRVIEW_COMPAREMETHOD 128 -#define IDR_MAINFRAME_ICON_GRAY 129 #define IDD_ABOUTBOX 200 #define IDD_OPEN 202 #define IDD_SAVECLOSING 203