我正在使用Win32 GDI本机API绘制线条。现在,我想画一条透明的线。我已经在颜色中设置了alpha通道属性。但是,将Alpha通道设置为彩色不会将线绘制为透明。我读到有关Alpha Blend Api的信息,但找不到解决方案。
var hdc = g.GdiDeviceContext;
var srcHdc = CreateCompatibleDC(hdc);
var clipRegion = CreateRectRgn(x, y, x + width, y + height);
SelectClipRgn(hdc, clipRegion);
var pen = CreatePen(PenStyle.Solid, LineWidth, (uint)ColorTranslator.ToWin32(colour));
if (pen != IntPtr.Zero)
{
var oldPen = SelectObject(hdc, pen);
Polyline(hdc, points, points.Length);
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
SelectClipRgn(hdc, IntPtr.Zero);
AlphaBlend(hdc, x, y, width, height, srcHdc, x, y, width, height, new BlendFunction(0x00, 0, 0x7f, 0x00));
DeleteObject(clipRegion);
我正试图将线条画得透明。
答案 0 :(得分:1)
var srcHdc = CreateCompatibleDC(hdc);
这将创建一个存储设备上下文。这是正确的第一步。但是内存dc尚未准备好。它也需要一个内存位图。
SelectObject(hdc, pen); Polyline(hdc, points, points.Length);
这将在Windows设备上下文中进行绘制。但是我们想利用内存设备上下文,然后使用HDC
AlphaBlend
请参见以下示例:
int w = 100;
int h = 100;
//create memory device context
var memdc = CreateCompatibleDC(hdc);
//create bitmap
var hbitmap = CreateCompatibleBitmap(hdc, w, h);
//select bitmap in to memory device context
var holdbmp = SelectObject(memdc, hbitmap);
//begine drawing:
var hpen = CreatePen(0, 4, 255);
var holdpen = SelectObject(memdc, hpen);
Rectangle(memdc, 10, 10, 90, 90);
//draw memory device (memdc) context on to windows device context (hdc)
AlphaBlend(hdc, 0, 0, w, h, memdc, 0, 0, w, h, new BLENDFUNCTION(0, 0, 128, 0));
//clean up:
SelectObject(memdc, holdbmp);
SelectObject(memdc, holdpen);
DeleteObject(hbitmap);
DeleteObject(hpen);
DeleteDC(memdc);