Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions CPUCoreBars/CPUCoreBars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ using namespace Gdiplus;

// 静态成员变量定义
HFONT CCpuUsageItem::s_symbolFont = nullptr;
HFONT CCpuUsageItem::s_coreNumberFont = nullptr; // Font for core numbers
int CCpuUsageItem::s_fontRefCount = 0;

CCpuUsageItem::CCpuUsageItem(int core_index, bool is_e_core)
: m_core_index(core_index), m_is_e_core(is_e_core),
m_cachedBgBrush(nullptr), m_cachedBarBrush(nullptr),
m_lastBgColor(0), m_lastBarColor(0), m_lastDarkMode(false)
{
// 初始化静态字体(只创建一次)
if (s_symbolFont == nullptr) {
// Initialize static fonts (only create once)
if (s_fontRefCount == 0) {
s_symbolFont = CreateFontW(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI Symbol");
s_coreNumberFont = CreateFontW(10, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI");
}
s_fontRefCount++;

Expand All @@ -43,9 +47,15 @@ CCpuUsageItem::~CCpuUsageItem()

// 减少字体引用计数
s_fontRefCount--;
if (s_fontRefCount == 0 && s_symbolFont) {
DeleteObject(s_symbolFont);
s_symbolFont = nullptr;
if (s_fontRefCount == 0) {
if (s_symbolFont) {
DeleteObject(s_symbolFont);
s_symbolFont = nullptr;
}
if (s_coreNumberFont) {
DeleteObject(s_coreNumberFont);
s_coreNumberFont = nullptr;
}
}
}

Expand Down Expand Up @@ -154,6 +164,22 @@ void CCpuUsageItem::DrawItem(void* hDC, int x, int y, int w, int h, bool dark_mo
if (m_is_e_core) {
DrawECoreSymbol(dc, rect, dark_mode);
}
else {
// For non-E-cores, draw the core number at the top
wchar_t core_num_str[4]; // Buffer for core number string
swprintf_s(core_num_str, L"%d", m_core_index);

COLORREF text_color = dark_mode ? RGB(255, 255, 255) : RGB(0, 0, 0);
SetTextColor(dc, text_color);
SetBkMode(dc, TRANSPARENT);

// Use the cached core number font
if (s_coreNumberFont) {
HGDIOBJ hOldFont = SelectObject(dc, s_coreNumberFont);
DrawTextW(dc, core_num_str, -1, &rect, DT_CENTER | DT_TOP | DT_SINGLELINE);
SelectObject(dc, hOldFont);
}
}
}


Expand Down
1 change: 1 addition & 0 deletions CPUCoreBars/CPUCoreBars.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CCpuUsageItem : public IPluginItem

// 新增:静态字体缓存
static HFONT s_symbolFont;
static HFONT s_coreNumberFont; // Added missing declaration
static int s_fontRefCount;

// 新增:GDI对象缓存
Expand Down
Loading