Bitmap,ID = IDC_LED。IDB_LED_GREEN、IDB_LED_RED(建議 16x16 或 32x32 像素)。void CSerialDlg::OnBnClickedBtnToggleLed()
{
static bool bGreen = true;
CStatic* pLed = (CStatic*)GetDlgItem(IDC_LED);
if (pLed) {
pLed->SetBitmap(bGreen ? IDB_LED_RED : IDB_LED_GREEN);
bGreen = !bGreen;
}
}
void CSerialDlg::DrawLed(CDC* pDC, CRect rect, bool bGreen)
{
// 背景清理
pDC->FillSolidRect(rect, RGB(240,240,240));
// 畫圓
int r = min(rect.Width(), rect.Height()) / 2 - 2;
CPoint center(rect.CenterPoint());
CBrush brush(bGreen ? RGB(0, 200, 0) : RGB(200, 0, 0));
CBrush* pOld = pDC->SelectObject(&brush);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
// 高光效果
CBrush highlight(RGB(255,255,255));
pDC->SelectObject(&highlight);
pDC->Ellipse(center.x - r/3, center.y - r/3, center.x - r/6, center.y - r/6);
pDC->SelectObject(pOld);
}
// 在 OnPaint 或按鈕處理中調用
void CSerialDlg::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetDlgItem(IDC_LED)->GetWindowRect(&rect);
ScreenToClient(&rect);
DrawLed(&dc, rect, m_bLedGreen);
}
原帖最佳回答鏈接:
https://blog.csdn.net/lujianfeiccie2009/article/details/7281996
https://download.csdn.net/detail/wyx20750/8727117
https://download.csdn.net/detail/a964153622/4922120
https://www.pudn.com/downloads522/sourcecode/windows/dialog/detail2166436.html
enum LedState { OFF, GREEN, YELLOW, RED, BLINK }。SetTimer(1, 500, NULL) 實現閃爍,OnTimer 中切換狀態並 InvalidateRect 重繪。SetTimer(2, 100, NULL),OnTimer 熄滅。掌握 Static 控件位圖切換或自繪圓形,即可在 MFC 串口調試工具中實現專業的雙色/多色指示燈效果。