我有一个 alpha 通道设置为 50% 不透明的图像(PNG 文件)。当我尝试在 TransparencyKey 设置为白色且背景色设置为白色的表单上绘制图像时,我希望图像绘制为 50% 透明。但是,它首先与表单背景色混合,因此它是完全不透明的。有没有办法解决?我不想设置表单的 Opaque 属性,因为表单上的某些图像需要是半透明的,而有些则需要是不透明的。
问问题
4383 次
3 回答
1
我最终使用了分层窗口,使用了 WS_EX_LAYERED 扩展窗口样式。
于 2009-09-02T18:09:50.987 回答
0
我不认为你可以。我们有一个启动屏幕,我们在其中做了类似的事情,但我们最终捕获了屏幕并将其设置为表单的背景图像。显然,这似乎只是工作,如果屏幕发生变化,表单的背景不会,事情看起来很奇怪。如果您找到更好的方法,我很想知道。
这是捕获屏幕的代码,只需将 ScreenRect 设置为表单屏幕坐标并调用 Process():
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace TourFactory.Core.Drawing
{
public class CaptureScreenCommand
{
#region Initialization and Destruction
public CaptureScreenCommand()
{
}
#endregion
#region Fields and Properties
// BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code
// that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e.
// do a simple copy from the source to the destination.
private const int cRasterOp_SrcCopy = 0xCC0020; // 13369376;
private Rectangle mScreenRect;
/// <summary>
/// Gets or sets the screen coordinates to capture.
/// </summary>
public Rectangle ScreenRect
{
get { return mScreenRect; }
set { mScreenRect = value; }
}
#endregion
#region Methods
public Image Process()
{
// use the GDI call and create a DC to the whole display
var dc1 = CreateDC("DISPLAY", null, 0, 0);
var g1 = Graphics.FromHdc(dc1);
// create a compatible bitmap the size of the form
var bmp = new Bitmap(mScreenRect.Width, mScreenRect.Height, g1);
var g2 = Graphics.FromImage(bmp);
// Now go retrace our steps and get the device contexts for both the bitmap and the screen
// Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
// when you try to release the dcs
dc1 = g1.GetHdc();
var dc2 = g2.GetHdc();
// Bit Blast the screen into the Bitmap
BitBlt(dc2, 0, 0, mScreenRect.Width, mScreenRect.Height, dc1, mScreenRect.Left, mScreenRect.Top, cRasterOp_SrcCopy);
// Remember to release the dc's, otherwise problems down the road
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
// return bitmap
return bmp;
}
#endregion
#region gdi32.dll
[DllImport("gdi32")]
private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, int lpOutput, int lpInitData);
[DllImport("gdi32")]
private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int width, int height, IntPtr hdcSrc, int xSrc, int ySrc, int dwRop);
#endregion
}
}
于 2009-04-26T18:56:50.733 回答
0
好的。不要忘记 Vista 有桌面窗口管理器来创建半透明窗口(又名 Areo) http://msdn.microsoft.com/en-us/magazine/cc163435.aspx
于 2009-09-02T18:20:15.523 回答