将双缓冲位图与 AlphaBlend 一起使用时出现的问题

The problem when using Double buffering Bitmap with AlphaBlend

本文关键字:问题 一起 缓冲 位图 AlphaBlend      更新时间:2023-10-16

我的文本在更改时重叠。

但是现在是时候文本了,所以总是在变化

我已经试过了


TranslateBlt(memhdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom,

hdc, TextBoxRect.right, TextBoxRect.bottom, RGB(0,0,0((;

案例WM_ERASEBKGND:返回真;

InvalidateRect(NULL, &TextBoxRect, TRUE(;


这是我的代码。


HDC hdc = GetDC(NULL);
HDC memhdc;
HBITMAP bgbitmap, oldbitmap;
stringstream stream;
int cur = delay / 10;
stream << "Ms : " << msec;
wchar_t str[120];
mbstowcs_s(NULL, str, 120, stream.str().c_str(), 120);
memhdc = CreateCompatibleDC(hdc);
bgbitmap = CreateCompatibleBitmap(hdc, TextBoxRect.right, TextBoxRect.bottom);
oldbitmap = (HBITMAP) SelectObject(memhdc, bgbitmap);
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 255;
bf.AlphaFormat = 0;
AlphaBlend(memhdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom, hdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom, bf);
SetBkMode(memhdc, TRANSPARENT);
SetTextColor(memhdc, RGB(255, 0, 0));
DrawText(memhdc, str, -1, &TextBoxRect, DT_LEFT | DT_NOCLIP);
BitBlt(hdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom, memhdc, TextBoxRect.left, TextBoxRect.top, SRCCOPY);
DeleteObject(SelectObject(memhdc,oldbitmap));
DeleteObject(bgbitmap);
DeleteDC(memhdc);
ReleaseDC(NULL, hdc);

我想你已经准备好绘制透明的数字变化了。

您可以尝试同时调用InvalidateRectUpdateWindow刷新本地区域,因为系统会将窗口中的绘制操作视为低优先级操作,可以使用 UpdateWindow 立即重绘。

你也可以尝试以下代码,正如乔纳森·波特所说

就像:

   AlphaBlend(memhdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right,       TextBoxRect.bottom, hdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom, bf);
   SetBkMode(hdc, TRANSPARENT);
   SetTextColor(hdc, RGB(255, 0, 0));       
   BitBlt(hdc, TextBoxRect.left, TextBoxRect.top, TextBoxRect.right, TextBoxRect.bottom, hdc, TextBoxRect.left, TextBoxRect.top, SRCCOPY);
   DrawText(hdc, str, -1, &TextBoxRect, DT_LEFT | DT_NOCLIP);