Skip to content

Commit

Permalink
Fixed issue with toolbar icon appearing black if the bitmap is incomp…
Browse files Browse the repository at this point in the history
…atible with the screen format.
  • Loading branch information
cmrazek committed Mar 9, 2013
1 parent b1004ba commit 969dab0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@
*.lib
*.ncb
*.obj
*.opensdf
*.pch
*.pdb
*.sdf
Expand Down
32 changes: 29 additions & 3 deletions NppSharp/NppInterface.cpp
Expand Up @@ -287,12 +287,38 @@ namespace NppSharp
Bitmap^ bm = cmd->ToolbarIcon;
if (bm != nullptr)
{
tbi.hToolbarBmp = (HBITMAP)bm->GetHbitmap().ToInt32();

::SendMessageW(_nppHandle, NPPM_ADDTOOLBARICON, GetPluginCommandId(cmd), (LPARAM)&tbi);
HBITMAP hUserBitmap = (HBITMAP)bm->GetHbitmap().ToInt32();
HBITMAP hBitmap = MakeCompatibleBitmap(hUserBitmap);
if (hBitmap)
{
tbi.hToolbarBmp = hBitmap;
::SendMessageW(_nppHandle, NPPM_ADDTOOLBARICON, GetPluginCommandId(cmd), (LPARAM)&tbi);
::DeleteObject(hUserBitmap);
}
}
}

HBITMAP NppInterface::MakeCompatibleBitmap(HBITMAP hUserBitmap)
{
BITMAP bmp;
if (!::GetObject(hUserBitmap, sizeof(bmp), &bmp)) return hUserBitmap;

HDC hSrcDC = ::CreateCompatibleDC(NULL);
::SelectObject(hSrcDC, hUserBitmap);

HDC hNppDC = ::GetDC(_nppHandle);
HDC hDstDC = ::CreateCompatibleDC(hNppDC);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hNppDC, bmp.bmWidth, bmp.bmHeight);
::SelectObject(hDstDC, hBitmap);

::BitBlt(hDstDC, 0, 0, bmp.bmWidth, bmp.bmHeight, hSrcDC, 0, 0, SRCCOPY);

::DeleteDC(hSrcDC);
::DeleteDC(hDstDC);
::ReleaseDC(_nppHandle, hNppDC);
return hBitmap;
}

void NppInterface::OnTbModification()
{
RegisterToolbarIcons(this, gcnew EventArgs());
Expand Down
1 change: 1 addition & 0 deletions NppSharp/NppInterface.h
Expand Up @@ -203,6 +203,7 @@ namespace NppSharp
private:
String^ GetFileNameByBufferId(unsigned int bufferId);
void DockWindow_Shutdown();
HBITMAP MakeCompatibleBitmap(HBITMAP hUserBitmap);

HWND _nppHandle;
HWND _scHandle1;
Expand Down

0 comments on commit 969dab0

Please sign in to comment.