Skip to content

Commit 4ef0022

Browse files
committed
修正主窗口使用png背景时,插件项目自绘使用GDI绘制的文本变成透明的问题
1 parent 0c5e86e commit 4ef0022

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

TrafficMonitor/DrawCommon.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,57 @@ void DrawCommonHelper::ImageDrawAreaConvert(CSize image_size, CPoint& start_poin
298298
}
299299
}
300300
}
301+
302+
void DrawCommonHelper::FixBitmapTextAlpha(HBITMAP hBitmap, BYTE alpha, const std::vector<CRect>& rects)
303+
{
304+
if (rects.empty())
305+
return;
306+
BITMAP bm;
307+
GetObject(hBitmap, sizeof(BITMAP), &bm);
308+
309+
int width = bm.bmWidth;
310+
int height = bm.bmHeight;
311+
312+
// 获取位图的像素数据
313+
BITMAPINFO bmpInfo = { 0 };
314+
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
315+
bmpInfo.bmiHeader.biWidth = width;
316+
bmpInfo.bmiHeader.biHeight = -height; // top-down DIB
317+
bmpInfo.bmiHeader.biPlanes = 1;
318+
bmpInfo.bmiHeader.biBitCount = 32;
319+
bmpInfo.bmiHeader.biCompression = BI_RGB;
320+
321+
HDC hdc = CreateCompatibleDC(NULL);
322+
SelectObject(hdc, hBitmap);
323+
324+
// 分配内存存储位图像素
325+
RGBQUAD* pPixels = new RGBQUAD[width * height];
326+
GetDIBits(hdc, hBitmap, 0, height, pPixels, &bmpInfo, DIB_RGB_COLORS);
327+
328+
// 遍历所有矩形区域
329+
for (const auto& rect : rects)
330+
{
331+
int startX = max(0, rect.left);
332+
int startY = max(0, rect.top);
333+
int endX = min(width, rect.right);
334+
int endY = min(height, rect.bottom);
335+
336+
// 遍历当前矩形内的像素
337+
for (int y = startY; y < endY; ++y)
338+
{
339+
for (int x = startX; x < endX; ++x)
340+
{
341+
int index = y * width + x;
342+
//如果检测到alpha值为0,则可能是被错误地设置成透明的文本部分,将其修正为正确的alpha值
343+
if (pPixels[index].rgbReserved == 0)
344+
pPixels[index].rgbReserved = alpha; // 设置Alpha通道
345+
}
346+
}
347+
}
348+
349+
// 将修改后的像素数据写回位图
350+
SetDIBits(hdc, hBitmap, 0, height, pPixels, &bmpInfo, DIB_RGB_COLORS);
351+
352+
delete[] pPixels;
353+
DeleteDC(hdc);
354+
}

TrafficMonitor/DrawCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ namespace DrawCommonHelper
111111
//stretch_mode[int]:拉伸模式
112112
void ImageDrawAreaConvert(CSize image_size, CPoint& start_point, CSize& size, IDrawCommon::StretchMode stretch_mode);
113113

114+
//修正位图中文本部分的Alpha通道
115+
//使用了UpdateLayeredWindow后,使用GDI绘制的文本也会变得透明,此函数会遍历bitmap中alpha值为0的部分,将其修正为正确的alpha值
116+
void FixBitmapTextAlpha(HBITMAP hBitmap, BYTE alpha, const std::vector<CRect>& rects);
114117
};

TrafficMonitor/SkinFile.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,24 @@ void CSkinFile::DrawInfo(CDC* pDC, bool show_more_info, CFont& font)
527527
//绘制显示项目
528528
DrawItemsInfo(gdiplus_drawer, layout, font);
529529

530+
//重新设置自绘插件区域的alpha值。
531+
//插件自绘时可能会使用GDI绘制文本,由于使用了UpdateLayeredWindow函数,使用GDI的绘图函数绘制文本时会导致文本变得透明。
532+
//这里遍历所有插件自绘区域,将alpha值为0的部分修正为正确的alpha值。
533+
std::vector<CRect> rects;
534+
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
535+
{
536+
const auto& layout_item = layout.GetItem(plugin_item);
537+
if (plugin_item->IsCustomDraw() && layout_item.show)
538+
{
539+
CRect rect(CPoint(layout_item.x, layout_item.y), CSize(layout_item.width, m_layout_info.text_height));
540+
rects.push_back(rect);
541+
}
542+
}
543+
DrawCommonHelper::FixBitmapTextAlpha(hBitMap, m_alpha, rects);
544+
530545
SIZE sizeWindow = rect.Size();
531546
POINT ptSrc = { 0,0 };
532-
::UpdateLayeredWindow(hWnd, pDC->GetSafeHdc(), nullptr, &sizeWindow, hdcMemory, &ptSrc, RGB(255, 0, 255), &bf, ULW_ALPHA);
547+
::UpdateLayeredWindow(hWnd, pDC->GetSafeHdc(), nullptr, &sizeWindow, hdcMemory, &ptSrc, 0, &bf, ULW_ALPHA);
533548

534549
gdiplus_drawer.GetGraphics()->ReleaseHDC(hdcMemory);
535550
DeleteObject(hBitMap);
@@ -672,7 +687,8 @@ void CSkinFile::DrawItemsInfo(IDrawCommon& drawer, Layout& layout, CFont& font)
672687
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(cl).c_str());
673688
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"0");
674689
}
675-
drawer.SetTextColor(cl);
690+
drawer.GetDC()->SetTextColor(cl);
691+
drawer.GetDC()->SetBkMode(TRANSPARENT);
676692
plugin_item->DrawItem(drawer.GetDC()->GetSafeHdc(), layout_item.x, layout_item.y, layout_item.width, m_layout_info.text_height, brightness >= 128);
677693
}
678694
else

0 commit comments

Comments
 (0)