10

我有一个由 a 组成的对象TFrame,在它上面 aTPanel和在那个 a 上TImage。一个位图被分配给TImage包含一个钢琴卷。这个框架对象放在一个TImage,包含一个包含网格的图像。有关示例,请参见图像。

在此处输入图像描述

问题:是否可以使框架部分透明,从而使包含网格(在主窗体上)的背景图像隐约可见?理想情况下,透明度的数量可以由用户设置。位图是 32 位深,但尝试 alpha 通道并没有帮助。面板不是绝对必要的。它用于在对象周围快速设置边框。我可以把它画在图像上。

更新 1添加了一个小代码示例。主机用垂直线绘制背景。第二个单元包含一个 TFrame 和一个在其上绘制水平线的 TImage。我想看到的是垂直线部分通过 TFrame 图像发光。

更新 2我在原始问题中没有指定的内容:TFrame 是更大应用程序的一部分并且独立运行。如果透明度问题可以由 TFrame 本身处理,那将有所帮助。

///////////////// Main unit, on mouse click draw lines and plot TFrame
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls,
  Unit2;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var background: TBitmap;
    f: TFrame2;
    i, c: Int32;
begin
   background := TBitmap.Create;
   background.Height := Image1.Height;
   background.Width  := Image1.Width;
   background.Canvas.Pen.Color := clBlack;

   for i := 0 to 10 do
   begin
      c := i * background.Width div 10;
      background.Canvas.MoveTo (c, 0);
      background.Canvas.LineTo (c, background.Height);
   end;
   Image1.Picture.Assign (background);
   Application.ProcessMessages;

   f := TFrame2.Create (Self);
   f.Parent := Self;
   f.Top    := 10;
   f.Left   := 10;
   f.plot;
end;

end.

///////////////////Unit containing the TFrame
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls;

type
  TFrame2 = class(TFrame)
    Image1: TImage;

    procedure plot;
  end;

implementation

{$R *.dfm}

procedure TFrame2.plot;
var bitmap: TBitmap;
begin
   bitmap := TBitmap.Create;
   bitmap.Height := Image1.Height;
   bitmap.Width  := Image1.Width;
   bitmap.PixelFormat := pf32Bit;
   bitmap.Canvas.MoveTo (0, bitmap.Height div 2);
   bitmap.Canvas.LineTo (bitmap.Width, bitmap.Height div 2);
   Image1.Picture.Assign (bitmap);
end;

end.

更新 3我曾希望会有一些消息或 API 调用会产生一个解决方案,即控件可以使其自身部分透明,就像 WMEraseBkGnd 消息为完全透明所做的那样。在他们的解决方案中,Sertac 和 NGLN 都指向使用 AlphaBlend 函数模拟透明度。此功能合并两个位图,因此需要了解背景图像。现在我的 TFrame 有一个额外的属性:BackGround: TImage由父控件分配。这给出了预期的结果(看到它工作真是太专业了:-)

RRUZ 指向 Graphics32 库。我所看到的它产生了惊人的结果,对我来说学习曲线太陡峭了。

感谢大家的帮助!

4

3 回答 3

8

隐藏框架并使用Frame.PaintTo. 例如,如下所示:

unit Unit1;

interface

uses
  Windows, Classes, Graphics, Controls, Forms, Unit2, JPEG, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage; //Align = alClient, Visible = False
    Frame21: TFrame2; //Visible = False
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    FBlendFunc: TBlendFunction;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.FormPaint(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.Width := Frame21.Width;
    Bmp.Height := Frame21.Height;
    Frame21.PaintTo(Bmp.Canvas, 0, 0);
    Canvas.StretchDraw(ClientRect, Image1.Picture.Graphic);
    with Frame21 do
      Windows.AlphaBlend(Canvas.Handle, Left, Top, Left + Width, Top + Height,
        Bmp.Canvas.Handle, 0, 0, Width, Height, FBlendFunc);
  finally
    Bmp.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FBlendFunc.BlendOp := AC_SRC_OVER;
  FBlendFunc.BlendFlags := 0;
  FBlendFunc.SourceConstantAlpha := 255 div 2;
  FBlendFunc.AlphaFormat := 0;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Invalidate;
end;

end.

框架单元:

unit Unit2;

interface

uses
  Windows, Classes, Controls, Forms, JPEG, ExtCtrls;

type
  TFrame2 = class(TFrame)
    Image1: TImage;  //Align = alClient
    Panel1: TPanel;  //Align = alClient, BevelWidth = 5
  end;

implementation

{$R *.dfm}

end.

结果:

部分透明框

根据您的具体情况重写上述内容,理想情况下TPaintBox在主窗体上删除图像组件。但是当框架中唯一重要的元素是图像时,我也会停止使用它,并开始自己绘制所有内容。

于 2012-05-22T21:43:31.313 回答
8

这是另一种解决方案,它将背景图像复制到顶部图像并AlphaBlend在其上放置位图,同时保留黑点的不透明度:

单元1:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit2, ExtCtrls, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Clip_View1: TClip_View;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    procedure TrackBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TrackBar1.Min := 0;
  TrackBar1.Max := 255;
  TrackBar1.Position := 255;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  Label1.Caption := IntToStr(TrackBar1.Position);
  Clip_View1.Transparency := TrackBar1.Position;
end;

end.

单元2:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TClip_View = class(TFrame)
    Image1: TImage;
    Panel1: TPanel;
    Image2: TImage;
  protected
    procedure SetTransparency(Value: Byte);
  private
    FTopBmp: TBitmap;
    FTransparency: Byte;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Transparency: Byte read FTransparency write SetTransparency;
  end;

implementation

{$R *.dfm}

{ TClip_View }

constructor TClip_View.Create(AOwner: TComponent);
begin
  inherited;
  Image1.Left := 0;
  Image1.Top := 0;
  Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName) + '..\..\back.bmp');
  Image1.Picture.Bitmap.PixelFormat := pf32bit;
  Image1.Width := Image1.Picture.Bitmap.Width;
  Image1.Height := Image1.Picture.Bitmap.Height;

  FTopBmp := TBitmap.Create;
  FTopBmp.LoadFromFile(ExtractFilePath(Application.ExeName) + '..\..\top.bmp');
  FTopBmp.PixelFormat := pf32bit;
  Image2.SetBounds(1, 1, FTopBmp.Width, FTopBmp.Height);
  Panel1.SetBounds(20, 20, Image2.Width + 2, Image2.Height + 2);
  Image2.Picture.Bitmap.SetSize(Image2.Width, Image2.Height);
  Image2.Picture.Bitmap.Canvas.Draw(0, 0, FTopBmp);
end;

destructor TClip_View.Destroy;
begin
  FTopBmp.Free;
  inherited;
end;

procedure TClip_View.SetTransparency(Value: Byte);
var
  Bmp: TBitmap;
  R: TRect;
  X, Y: Integer;
  Pixel: PRGBQuad;
  BlendFunction: TBlendFunction;
begin
  if Value <> FTransparency then begin
    FTransparency := Value;
    R := Image2.BoundsRect;
    OffsetRect(R, Panel1.Left, + Panel1.Top);
    Image2.Picture.Bitmap.Canvas.CopyRect(Image2.ClientRect,
                                          Image1.Picture.Bitmap.Canvas, R);

    Bmp := TBitmap.Create;
    Bmp.SetSize(FTopBmp.Width, FTopBmp.Height);
    Bmp.PixelFormat := pf32bit;
    Bmp.Assign(FTopBmp);
    try
      for Y := 0 to Bmp.Height - 1 do begin
        Pixel := Bmp.ScanLine[Y];
        for X := 0 to Bmp.Width - 1 do begin
          if (Pixel.rgbBlue <> 0) and (Pixel.rgbGreen <> 0) and
              (Pixel.rgbRed <> 0) then begin
            Pixel.rgbBlue := MulDiv(Pixel.rgbBlue, Value, $FF);
            Pixel.rgbGreen := MulDiv(Pixel.rgbGreen, Value, $FF);
            Pixel.rgbRed := MulDiv(Pixel.rgbRed, Value, $FF);
            Pixel.rgbReserved := Value;
          end else                      // don't touch black pixels
            Pixel.rgbReserved := $FF;
          Inc(Pixel);
        end;
      end;

      BlendFunction.BlendOp := AC_SRC_OVER;
      BlendFunction.BlendFlags := 0;
      BlendFunction.SourceConstantAlpha := 255;
      BlendFunction.AlphaFormat := AC_SRC_ALPHA;
      AlphaBlend(Image2.Picture.Bitmap.Canvas.Handle,
          0, 0, Image2.Picture.Bitmap.Width, Image2.Picture.Bitmap.Height,
          Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
          BlendFunction);
    finally
      Bmp.Free;
    end;
  end;
end;

end.


在启动时:
在此处输入图像描述
应用透明度:
在此处输入图像描述

于 2012-05-22T22:23:12.013 回答
2

我会用 aTPaintBox代替。在这种OnPaint情况下,首先绘制您的网格,然后在顶部混合您的滚动图像。根本不需要使用任何TImage, TPanel, 或TFrame组件。

于 2012-05-22T23:20:31.517 回答