我正在设计一个支持透明度的Compact Framework Control,对于控件的运行时版本,一切正常,只需在此API上执行平台调用:
[DllImport("coredll.dll")]
extern public static Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);
显然,对“coredll.dll”的调用不适用于桌面设计时间体验,而且现在,当绘画发生时,我只是检测到控件正在设计并且绘制它而没有任何透明度。我希望能够提供更好的设计时体验,并在Visual Studio Designer中显示相同的透明度。
我尝试过这个平台电话:
[DllImport("gdi32.dll", EntryPoint = "GdiAlphaBlend")]
public static extern bool AlphaBlendDesktop(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
BlendFunction blendFunction);
但是当它返回true时,结果是什么都没有画到设计时间视图。
有什么想法吗?
答案 0 :(得分:2)
这让我欺骗设计师执行AlphaBlend操作。
// PixelFormatIndexed = 0x00010000, // Indexes into a palette
// PixelFormatGDI = 0x00020000, // Is a GDI-supported format
// PixelFormatAlpha = 0x00040000, // Has an alpha component
// PixelFormatPAlpha = 0x00080000, // Pre-multiplied alpha
// PixelFormatExtended = 0x00100000, // Extended color 16 bits/channel
// PixelFormatCanonical = 0x00200000,
// PixelFormat32bppARGB = (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),
// cheat the design time to create a bitmap with an alpha channel
using (Bitmap bmp = new Bitmap(image.Width, image.Height, (PixelFormat)
PixelFormat32bppARGB))
{
// copy the original image
using(Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(image,0,0);
}
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
(PixelFormat)PixelFormat32bppARGB);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 32 bits per pixels.
int bytes = bmp.Width * bmp.Height * 4;
byte[] argbValues = new byte[bytes];
// Copy the ARGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, bytes);
// Set every alpha value to the given transparency.
for (int counter = 3; counter < argbValues.Length; counter += 4)
argbValues[counter] = transparency;
// Copy the ARGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
gx.DrawImage(bmp, x, y);
}
答案 1 :(得分:0)
很抱歉,这并不能完全回答你的问题...但你应该查看.Net Compact Framework的UI框架,它已经进行了alpha混合。