代码之家  ›  专栏  ›  技术社区  ›  Tim Clem

如何在设计时为紧凑的框架控件进行Alpha混合

  •  1
  • Tim Clem  · 技术社区  · 15 年前

    我正在设计一个紧凑的框架控件,该控件支持透明性,对于该控件的运行时版本,一切正常,只要在这个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”是不起作用的,现在,当绘制发生时,我只是检测到控件正在被设计,并在没有任何透明度的情况下绘制它。我希望能够提供更好的设计时体验,并在VisualStudioDesigner中显示同样的透明度。

    我试过打这个平台电话:

    [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,结果是在设计时视图中根本没有绘制任何内容。

    有什么想法吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Saar Yahalom Saar Yahalom    15 年前

    这让我愚弄了设计师来执行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
  •  0
  •   Chris Brandsma    15 年前

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

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