Delphi 与 DirectX 之 DelphiX(9): 透明色与是否透明

  • A+
所属分类:[开发技巧]

这一节的目的是利用TPictureCollectionItem的透明色~达到加载的图片透明的效果.

在例子中 图片的背景颜色 是白色.

原理是 指定透明色(要使之透明的颜色) 即例子中的白色 .传给TPictureCollectionItem的 TransparentColor的属性

并用Transparent属性控制 是否需要透明

代码如下:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
DXDraw1: TDXDraw;
DXImageList1: TDXImageList;
CheckBox1: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

var
PicItem: TPictureCollectionItem;

procedure TForm1.CheckBox1Click(Sender: TObject);
const
arr: array[Boolean] of string = ('不透明','透明');
begin
DXDraw1.Surface.Fill($FF0000);
PicItem.Restore;
PicItem.Transparent := CheckBox1.Checked;
PicItem.Draw(DXDraw1.Surface,10,10,0);
DXDraw1.Flip;
CheckBox1.Caption:=arr[CheckBox1.Checked] ;
end;

procedure TForm1.FormCreate(Sender: TObject);
const
ImgPath1 = 'C:\Temp\Transparent.bmp';
begin
DXImageList1.DXDraw := DXDraw1;
PicItem := TPictureCollectionItem(DXImageList1.Items.Add);
PicItem.Picture.LoadFromFile(ImgPath1);

PicItem.TransparentColor := $FFFFFF;
end;

end.
窗口代码:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 260
ClientWidth = 339
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object DXDraw1: TDXDraw
Left = 8
Top = 8
Width = 305
Height = 209
AutoInitialize = True
AutoSize = True
Color = clBlack
Display.FixedBitCount = False
Display.FixedRatio = True
Display.FixedSize = True
Options = [doAllowReboot, doWaitVBlank, doCenter, do3D, doDirectX7Mode, doHardware, doSelectDriver]
SurfaceHeight = 209
SurfaceWidth = 305
TabOrder = 0
Traces = <>
end
object CheckBox1: TCheckBox
Left = 208
Top = 235
Width = 49
Height = 17
Caption = 'CheckBox1'
TabOrder = 1
OnClick = CheckBox1Click
end
object DXImageList1: TDXImageList
Items.ColorTable = {
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000}
Items = <>
Left = 280
Top = 168
end
end

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: