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 に答える