I'm trying to do this:
- Load a PNG image that contains alpha channel
- Assign it to a TImage
- Set the form transparent
- Be able to move the form with the mouse over the image
- Be able to click throgh the transparent areas
I used to do this by using a bitmap and setting up regions, and now I've moved on to using Layered window styles for this.
I have accomplished this by using pngimage. This works if I use WHITE as the form's color and the transparent color, but using any other color fails. It still makes the form transparent, but if you click on a transparent area the click doesn't go through and the form takes the click instead.
I guess the problem lies in the timage setting the transparent areas as white when it gets the png object assigned, but even so, the transparency works with any color.
This is the code. I have included a rar with the project, pngimage files that are needed and a test PNG image. Please rename the file to RAR (can't upload .rar to EE)
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, pngimage, ExtCtrls, StdCtrls;
type
TForm4 = class(TForm)
Button1: TButton;
Image1: TImage;
Button2: TButton;
Button3: TButton;
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
procedure TForm4.Button1Click(Sender
: TObject);
var p: TPNGObject;
begin
p:= TPNGObject.Create;
p.LoadFromFile(ExtractFile
Path(Appli
cation.Exe
Name)+'Ima
ge6.png');
Image1.Picture.Assign(p);
p.Free;
BorderStyle:= bsNone;
// if we pick white instead ($ffffff) it works
// Color:= clRed;
// TransparentColorValue:= clRed;
TransparentColor:= True;
button1.Enabled:= False;
end;
procedure TForm4.Button3Click(Sender
: TObject);
begin
Close;
end;
procedure TForm4.Image1MouseMove(Sen
der: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssleft in shift then
begin
releasecapture;
Form4.perform(WM_syscomman
d, $F012, 0);
end;
end;
end.
object Form4: TForm4
Left = 0
Top = 0
Caption = 'Form4'
ClientHeight = 305
ClientWidth = 393
Color = clBtnFace
TransparentColorValue = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 0
Top = 0
Width = 393
Height = 305
OnMouseMove = Image1MouseMove
end
object Button1: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Load PNG'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 208
Top = 160
Width = 145
Height = 49
Caption = 'This must be clickable too'
TabOrder = 1
end
object Button3: TButton
Left = 8
Top = 56
Width = 75
Height = 25
Caption = 'Close'
TabOrder = 2
OnClick = Button3Click
end
end
Start Free Trial