【问题标题】:How to do Alpha Blending at design time for a Compact Framework Control如何在设计时为紧凑型框架控件进行 Alpha 混合
【发布时间】:2009-08-26 18:25:51
【问题描述】:

我正在设计一个支持透明度的紧凑型框架控件,对于控件的运行时版本,一切正常,只需找到在此 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 设计器中显示相同的透明度。

我已经尝试过这个平台调用:

[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 时,结果是什么都没有被绘制到设计时视图。

有什么想法吗?

【问题讨论】:

    标签: c# visual-studio-2008 compact-framework


    【解决方案1】:

    这让我愚弄设计师执行 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);
    }
    

    【讨论】:

    • 太棒了!自从我试图解决这个问题以来已经很久了,我需要一段时间来验证,但它看起来是一个很好的解决方案。谢谢。
    【解决方案2】:

    很抱歉,这并不能完全回答您的问题...但是您应该查看 .Net Compact Framework 的 UI 框架,它已经进行了 alpha 混合。

    http://code.msdn.microsoft.com/uiframework

    【讨论】:

    • 这绝不是答案,甚至是试图回答他的问题。他已经在设备上进行了混合工作 - 他只是希望它在设计器中以相同的方式呈现。
    • UI 框架完成了他正在尝试做的事情(还有更多),并附带源代码。这里唯一没有帮助的是您的回复。
    • 我已经查看了 UI 框架,但它对我试图解决的问题没有帮助,正如 ctacke 提到的。 UI 框架没有解决设计器对使用 alpha 混合的紧凑框架控件的支持问题。也许我的问题可以更清楚?
    猜你喜欢
    • 2010-12-24
    • 2014-11-19
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2012-07-25
    相关资源
    最近更新 更多