- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个完全透明的表单,在其上绘制具有 alpha 透明度的位图。问题是我不知道如何将位图的背景设置为 Alpha 0(完全看穿)。
这是表单现在的样子(注意右上角不透明)。
这是我想要的样子(右上角完全透明):
这是我的来源:
unit frmMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ActiveX,
GDIPObj, GDIPAPI, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm7 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
function CreateTranparentForm: TForm;
end;
var
Form7: TForm7;
implementation
{$R *.dfm}
// Thanks to Anders Melander for the transparent form tutorial
// (http://melander.dk/articles/alphasplash2/2/)
function CreateAlphaBlendForm(AOwner: TComponent; Bitmap: TBitmap; Alpha: Byte): TForm;
procedure PremultiplyBitmap(Bitmap: TBitmap);
var
Row, Col: integer;
p: PRGBQuad;
PreMult: array[byte, byte] of byte;
begin
// precalculate all possible values of a*b
for Row := 0 to 255 do
for Col := Row to 255 do
begin
PreMult[Row, Col] := Row*Col div 255;
if (Row <> Col) then
PreMult[Col, Row] := PreMult[Row, Col]; // a*b = b*a
end;
for Row := 0 to Bitmap.Height-1 do
begin
Col := Bitmap.Width;
p := Bitmap.ScanLine[Row];
while (Col > 0) do
begin
p.rgbBlue := PreMult[p.rgbReserved, p.rgbBlue];
p.rgbGreen := PreMult[p.rgbReserved, p.rgbGreen];
p.rgbRed := PreMult[p.rgbReserved, p.rgbRed];
inc(p);
dec(Col);
end;
end;
end;
var
BlendFunction: TBlendFunction;
BitmapPos: TPoint;
BitmapSize: TSize;
exStyle: DWORD;
PNGBitmap: TGPBitmap;
BitmapHandle: HBITMAP;
Stream: TMemoryStream;
StreamAdapter: IStream;
begin
Result := TForm.Create(AOwner);
// Enable window layering
exStyle := GetWindowLongA(Result.Handle, GWL_EXSTYLE);
if (exStyle and WS_EX_LAYERED = 0) then
SetWindowLong(Result.Handle, GWL_EXSTYLE, exStyle or WS_EX_LAYERED);
// Load the PNG from a resource
Stream := TMemoryStream.Create;
try
Bitmap.SaveToStream(Stream);
// Wrap the VCL stream in a COM IStream
StreamAdapter := TStreamAdapter.Create(Stream);
try
// Create and load a GDI+ bitmap from the stream
PNGBitmap := TGPBitmap.Create(StreamAdapter);
try
// Convert the PNG to a 32 bit bitmap
PNGBitmap.GetHBITMAP(MakeColor(0,0,0,0), BitmapHandle);
// Wrap the bitmap in a VCL TBitmap
Bitmap.Handle := BitmapHandle;
finally
FreeAndNil(PNGBitmap);
end;
finally
StreamAdapter := nil;
end;
finally
FreeAndNil(Stream);
end;
// Perform run-time premultiplication
PremultiplyBitmap(Bitmap);
// Resize form to fit bitmap
Result.ClientWidth := Bitmap.Width;
Result.ClientHeight := Bitmap.Height;
// Position bitmap on form
BitmapPos := Point(0, 0);
BitmapSize.cx := Bitmap.Width;
BitmapSize.cy := Bitmap.Height;
// Setup alpha blending parameters
BlendFunction.BlendOp := AC_SRC_OVER;
BlendFunction.BlendFlags := 0;
BlendFunction.SourceConstantAlpha := Alpha;
BlendFunction.AlphaFormat := AC_SRC_ALPHA;
UpdateLayeredWindow(Result.Handle, 0, nil, @BitmapSize, Bitmap.Canvas.Handle,
@BitmapPos, 0, @BlendFunction, ULW_ALPHA);
end;
procedure CopyControlToBitmap(AWinControl: TWinControl; Bitmap: TBitmap; X, Y: Integer);
var
SrcDC: HDC;
begin
SrcDC := GetDC(AWinControl.Handle);
try
BitBlt(Bitmap.Canvas.Handle, X, Y, AWinControl.ClientWidth, AWinControl.ClientHeight, SrcDC, 0, 0, SRCCOPY);
finally
ReleaseDC(AWinControl.Handle, SrcDC);
end;
end;
function MakeGDIPColor(C: TColor; Alpha: Byte): Cardinal;
var
tmpRGB : TColorRef;
begin
tmpRGB := ColorToRGB(C);
result := ((DWORD(GetBValue(tmpRGB)) shl BlueShift) or
(DWORD(GetGValue(tmpRGB)) shl GreenShift) or
(DWORD(GetRValue(tmpRGB)) shl RedShift) or
(DWORD(Alpha) shl AlphaShift));
end;
procedure TForm7.Button2Click(Sender: TObject);
begin
CreateTranparentForm.Show;
end;
function TForm7.CreateTranparentForm: TForm;
const
TabHeight = 50;
TabWidth = 150;
var
DragControl: TWinControl;
DragCanvas: TGPGraphics;
Bitmap: TBitmap;
ControlTop: Integer;
DragBrush: TGPSolidBrush;
begin
DragControl := Panel1;
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf32bit;
Bitmap.Height := TabHeight + DragControl.Height;
Bitmap.Width := DragControl.Width;
ControlTop := TabHeight;
// <<<< I need to clear the bitmap background here!!!
CopyControlToBitmap(DragControl, Bitmap, 0, ControlTop);
DragCanvas := TGPGraphics.Create(Bitmap.Canvas.Handle);
DragBrush := TGPSolidBrush.Create(MakeGDIPColor(clBlue, 255));
try
// Do the painting...
DragCanvas.FillRectangle(DragBrush, 0, 0, TabWidth, TabHeight);
finally
FreeAndNil(DragCanvas);
FreeAndNil(DragBrush);
end;
Result := CreateAlphaBlendForm(Self, Bitmap, 210);
Result.BorderStyle := bsNone;
finally
FreeAndNil(Bitmap);
end;
end;
end.
...和 DFM:
object Form7: TForm7
Left = 0
Top = 0
Caption = 'frmMain'
ClientHeight = 300
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 256
Top = 128
Width = 321
Height = 145
Caption = 'Panel1'
TabOrder = 0
object Edit1: TEdit
Left = 40
Top = 24
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object Button1: TButton
Left = 40
Top = 64
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
end
end
object Button2: TButton
Left = 16
Top = 16
Width = 75
Height = 25
Caption = 'Go'
TabOrder = 1
OnClick = Button2Click
end
end
谢谢。
最佳答案
您似乎对 UpdateLayeredWindow
/BLENDFUNCTION
的工作原理有误解。通过 UpdateLayeredWindow
,您可以使用每像素 alpha 或颜色键。您将 ULW_ALPHA
称为“dwFlags”,这意味着您打算使用每像素 alpha,并将完全不透明的位图传递给预乘例程(所有像素的 alpha 值为 255)。您的预乘例程不会修改 Alpha channel ,它所做的只是根据传递位图的 Alpha channel 计算红绿和蓝色值。最后,您得到的是一个完全不透明的位图,其中包含正确计算的 r、g、b(也未修改,因为 255/255 = 1)。您将获得的所有透明度都来自您分配给 BlendFunction
的 SourceConstantAlpha
的“210”。 UpdateLayeredWindow
提供的是一个半透明窗口,每个像素都具有相同的透明度。
填充位图区域(在问题的注释中提到)似乎有效,因为 FillRect
调用会覆盖 Alpha channel 。 alpha 为 255 的像素现在的 alpha 为 0。IMO,通常这应该被认为会导致未定义的行为,除非您完全理解它的工作原理/原因。
在当前状态下,该问题需要使用颜色键而不是每个像素 alpha 的答案,或者剪切表单的区域 (SetWindowRgn
)。如果要使用每像素 Alpha,则应以不同的方式应用于位图的各个部分。在问题的评论中,您提到位图将在某个时刻进行缩放。您还必须确保缩放代码保留 Alpha channel (如果使用)。
关于delphi - 如何在透明表单上创建具有清晰背景的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914710/
你好,我是 Cone。 首先,我们思考一个问题。 为什么会有操作系统? 在我们教科书上会提到分时系统、批处理系统等等现代操作系统前的中间产物,也会讲到管理硬件的功能,但似乎没有讲到为什么有
假设我只想清除顶部的所有条目,清除 Map 的 Map 的最合适方法是什么 -关卡 map ? Map> nestedMap; 方法A:仅清除顶层 map 。 nestedMap.clear();
我时不时遇到一个问题,我不确定解决方案是什么。 我有一个 2 列布局(左边是 strit,然后是主要区域)。在主要区域,我有时会有一个次要的 2 栏布局(例如 - 对于新闻部分,那里有一个图标,然后是
我有以下代码: Created Created .clearfix:after{ clear: both; bdy: "."; display
有没有办法CLS单行输出?我不相信 CLS 有任何开关,所以也许更好的问题是: 有什么办法吗 保留所有以前的输出以供重复使用? 或 捕获当前显示的输出(就像通过标记和复制一样)? 我只是想通过实时反馈
我有一个流式布局。当布局足够宽时,一些 div(.one 和 .two)可以全部水平排列在一条线上。 当布局最终被挤压时,右侧 float 的 div (.two) 最终会出现在多行上。有没有一种方法
我面临着一个我真的不知道从哪里开始解决的问题,所以我希望这个问题不要太宽泛。 我正在制作并在屏幕上应用它,我将有一个包含一些信息的矩形(假设它是一个 )并且我需要用另一个矩形覆盖那个矩形,所以当用户
我的 UITableView 跨越 iPhone View 的大小,并有一个 tableHeaderView 保存附加内容。想要标题清晰,表格的其余部分白色,我将表格的背景颜色设置为清晰,并在 Vie
我一直在使用 ImageMagick,但它产生的结果非常模糊。 convert -density 300 ../images/favicons/procensus.svg -background tr
我有手动数据的工作项目,但我想在我的项目中添加 json 解析。我认为我需要帮助。 (必须是实时解析,如果可能的话,新的item添加时会自动释放) 我的 TableView 代码 - (void)sc
我正在制作一个响应式网站。我有 3 个 div (.block),我需要将它们水平放置在一起。 当屏幕足够宽时,这很容易实现。但是,当我使浏览器更窄时,第三个 div (3) 换行到下一行,但我想要的
如何在 Swift 3 中使这个 UITableView 和它的单元格清晰。 我已经完成了前面的线程,但我仍然得到一个白色背景。 正如您从我的代码中看到的,我已经尝试了提到的各种方法: overrid
当我使用 为了显示图标,它在谷歌浏览器中看起来非常清晰锐利。然而,当我在 Firefox 或 Internet Explorer 中打开 svg 时,图标看起来很模糊。 这些浏览器似乎将图标呈现为半像
我是一名优秀的程序员,十分优秀!