diff --git a/PythonScript.Tests/PythonScript.Tests.vcxproj b/PythonScript.Tests/PythonScript.Tests.vcxproj index 796297d3..4b79c7df 100644 --- a/PythonScript.Tests/PythonScript.Tests.vcxproj +++ b/PythonScript.Tests/PythonScript.Tests.vcxproj @@ -296,6 +296,7 @@ + Create @@ -321,4 +322,4 @@ - \ No newline at end of file + diff --git a/PythonScript/project/PythonScript.vcxproj b/PythonScript/project/PythonScript.vcxproj index f38a9722..8b246e41 100644 --- a/PythonScript/project/PythonScript.vcxproj +++ b/PythonScript/project/PythonScript.vcxproj @@ -481,6 +481,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py + @@ -535,6 +536,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py + @@ -553,4 +555,4 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py - \ No newline at end of file + diff --git a/PythonScript/src/ConfigFile.cpp b/PythonScript/src/ConfigFile.cpp index 2f6b7f70..45c32446 100644 --- a/PythonScript/src/ConfigFile.cpp +++ b/PythonScript/src/ConfigFile.cpp @@ -3,6 +3,7 @@ #include "ConfigFile.h" #include "resource.h" #include "WcharMbcsConverter.h" +#include "Utility.h" ConfigFile* ConfigFile::s_instance; @@ -58,7 +59,7 @@ void ConfigFile::readConfig() char buffer[500]; - HBITMAP hIcon; + HBITMAP hBitmap; while (startupFile.good()) { @@ -90,17 +91,20 @@ void ConfigFile::readConfig() char *iconPath = strtok_s(NULL, "/", &context); if (!iconPath || !(*iconPath)) { - hIcon = static_cast(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE)); + hBitmap = static_cast(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE)); iconPath = NULL; } else { iconFullPath = expandPathIfNeeded(iconPath); - hIcon = static_cast(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE)); + hBitmap = static_cast(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE)); + if (hBitmap == NULL) { + hBitmap = ConvertIconToBitmap(const_cast(iconFullPath.c_str())); + } } if (scriptFullPath != L"") { - m_toolbarItems.push_back(std::pair >(scriptFullPath, std::pair(hIcon, iconPath ? iconFullPath : tstring()))); + m_toolbarItems.push_back(std::pair >(scriptFullPath, std::pair(hBitmap, iconPath ? iconFullPath : tstring()))); } } else if (0 == strcmp(element, "SETTING")) diff --git a/PythonScript/src/ShortcutDlg.cpp b/PythonScript/src/ShortcutDlg.cpp index babb3450..89474416 100644 --- a/PythonScript/src/ShortcutDlg.cpp +++ b/PythonScript/src/ShortcutDlg.cpp @@ -5,7 +5,7 @@ #include "resource.h" #include "MenuManager.h" #include "Notepad_plus_msgs.h" - +#include "Utility.h" ShortcutDlg::ShortcutDlg(HINSTANCE hInst, NppData& nppData, const TCHAR *scriptDirAppend) : m_hTree(NULL), @@ -602,8 +602,13 @@ void ShortcutDlg::toolbarSetIcon() if (GetOpenFileName(&ofn)) { ConfigFile::ToolbarItemsTD::iterator it = m_toolbarItems.begin() + index; - it->second.first = static_cast(LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE)); + HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hBitmap == NULL) { + hBitmap = ConvertIconToBitmap(ofn.lpstrFile); + } + it->second.first = hBitmap; it->second.second = ofn.lpstrFile; + int imageIndex = ImageList_Add(m_hImageList, it->second.first, NULL); LVITEM lvItem{}; lvItem.mask = LVIF_IMAGE; diff --git a/PythonScript/src/Utility.cpp b/PythonScript/src/Utility.cpp new file mode 100644 index 00000000..4c97ce06 --- /dev/null +++ b/PythonScript/src/Utility.cpp @@ -0,0 +1,33 @@ +#include "stdafx.h" +#include "Utility.h" + +HBITMAP ConvertIconToBitmap(LPCWSTR file_path) { + HBITMAP hBitmap = nullptr; + HICON hIcon = (HICON)LoadImage(nullptr, file_path, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hIcon) { + ICONINFO iconInfo; + if (GetIconInfo(hIcon, &iconInfo)) { + HDC hdc = GetDC(nullptr); + if (hdc) { + HDC hdcMem = CreateCompatibleDC(hdc); + if (hdcMem) { + BITMAP bm{}; + if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { + hBitmap = CreateCompatibleBitmap(hdc, 16, 16); + if (hBitmap) { + HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); + DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, nullptr, DI_NORMAL); + SelectObject(hdcMem, hbmOld); + } + } + DeleteDC(hdcMem); + } + ReleaseDC(nullptr, hdc); + } + DeleteObject(iconInfo.hbmColor); + DeleteObject(iconInfo.hbmMask); + } + DestroyIcon(hIcon); + } + return hBitmap; +} diff --git a/PythonScript/src/Utility.h b/PythonScript/src/Utility.h new file mode 100644 index 00000000..41eae045 --- /dev/null +++ b/PythonScript/src/Utility.h @@ -0,0 +1,7 @@ +#ifndef _PYTHONSCRIPT_UTILITY_H +#define _PYTHONSCRIPT_UTILITY_H +#endif + +#include + +HBITMAP ConvertIconToBitmap(LPCWSTR file_path);