Skip to content

Commit 6faf9ca

Browse files
committed
Make 16x16 .ico files possible for toolbar
1 parent 6713314 commit 6faf9ca

File tree

4 files changed

+44
-53
lines changed

4 files changed

+44
-53
lines changed

PythonScript/src/ConfigFile.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ConfigFile.h"
44
#include "resource.h"
55
#include "WcharMbcsConverter.h"
6+
#include "Utility.h"
67

78
ConfigFile* ConfigFile::s_instance;
89

@@ -98,32 +99,7 @@ void ConfigFile::readConfig()
9899
iconFullPath = expandPathIfNeeded(iconPath);
99100
hBitmap = static_cast<HBITMAP>(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
100101
if (hBitmap == NULL) {
101-
HICON hIcon = (HICON)LoadImage(NULL, iconFullPath.c_str(), IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE);
102-
if (hIcon) {
103-
ICONINFO iconInfo;
104-
if (GetIconInfo(hIcon, &iconInfo)) {
105-
HDC hdc = GetDC(NULL);
106-
if (hdc) {
107-
HDC hdcMem = CreateCompatibleDC(hdc);
108-
if (hdcMem) {
109-
BITMAP bm{};
110-
if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) {
111-
hBitmap = CreateCompatibleBitmap(hdc, 16, 16);
112-
if (hBitmap) {
113-
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
114-
DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL);
115-
SelectObject(hdcMem, hbmOld);
116-
}
117-
}
118-
DeleteDC(hdcMem);
119-
}
120-
ReleaseDC(NULL, hdc);
121-
}
122-
DeleteObject(iconInfo.hbmColor);
123-
DeleteObject(iconInfo.hbmMask);
124-
}
125-
DestroyIcon(hIcon);
126-
}
102+
hBitmap = ConvertIconToBitmap(const_cast<LPWSTR>(iconFullPath.c_str()));
127103
}
128104
}
129105
if (scriptFullPath != L"")

PythonScript/src/ShortcutDlg.cpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "resource.h"
66
#include "MenuManager.h"
77
#include "Notepad_plus_msgs.h"
8-
8+
#include "Utility.h"
99

1010
ShortcutDlg::ShortcutDlg(HINSTANCE hInst, NppData& nppData, const TCHAR *scriptDirAppend)
1111
: m_hTree(NULL),
@@ -604,32 +604,7 @@ void ShortcutDlg::toolbarSetIcon()
604604
ConfigFile::ToolbarItemsTD::iterator it = m_toolbarItems.begin() + index;
605605
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE);
606606
if (hBitmap == NULL) {
607-
HICON hIcon = (HICON)LoadImage(NULL, ofn.lpstrFile, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE);
608-
if (hIcon) {
609-
ICONINFO iconInfo;
610-
if (GetIconInfo(hIcon, &iconInfo)) {
611-
HDC hdc = GetDC(NULL);
612-
if (hdc) {
613-
HDC hdcMem = CreateCompatibleDC(hdc);
614-
if (hdcMem) {
615-
BITMAP bm{};
616-
if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) {
617-
hBitmap = CreateCompatibleBitmap(hdc, 16, 16);
618-
if (hBitmap) {
619-
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
620-
DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL);
621-
SelectObject(hdcMem, hbmOld);
622-
}
623-
}
624-
DeleteDC(hdcMem);
625-
}
626-
ReleaseDC(NULL, hdc);
627-
}
628-
DeleteObject(iconInfo.hbmColor);
629-
DeleteObject(iconInfo.hbmMask);
630-
}
631-
DestroyIcon(hIcon);
632-
}
607+
hBitmap = ConvertIconToBitmap(ofn.lpstrFile);
633608
}
634609
it->second.first = hBitmap;
635610
it->second.second = ofn.lpstrFile;

PythonScript/src/Utility.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "stdafx.h"
2+
#include "Utility.h"
3+
4+
HBITMAP ConvertIconToBitmap(LPCWSTR file_path) {
5+
HBITMAP hBitmap = nullptr;
6+
HICON hIcon = (HICON)LoadImage(nullptr, file_path, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE);
7+
if (hIcon) {
8+
ICONINFO iconInfo;
9+
if (GetIconInfo(hIcon, &iconInfo)) {
10+
HDC hdc = GetDC(nullptr);
11+
if (hdc) {
12+
HDC hdcMem = CreateCompatibleDC(hdc);
13+
if (hdcMem) {
14+
BITMAP bm{};
15+
if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) {
16+
hBitmap = CreateCompatibleBitmap(hdc, 16, 16);
17+
if (hBitmap) {
18+
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
19+
DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, nullptr, DI_NORMAL);
20+
SelectObject(hdcMem, hbmOld);
21+
}
22+
}
23+
DeleteDC(hdcMem);
24+
}
25+
ReleaseDC(nullptr, hdc);
26+
}
27+
DeleteObject(iconInfo.hbmColor);
28+
DeleteObject(iconInfo.hbmMask);
29+
}
30+
DestroyIcon(hIcon);
31+
}
32+
return hBitmap;
33+
}

PythonScript/src/Utility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _PYTHONSCRIPT_UTILITY_H
2+
#define _PYTHONSCRIPT_UTILITY_H
3+
#endif
4+
5+
#include <wtypes.h>
6+
7+
HBITMAP ConvertIconToBitmap(LPCWSTR file_path);

0 commit comments

Comments
 (0)