This is pretty easy. First we need a routine that fades a given bitmap:
procedure FadeBitmap(ABitmap: TBitmap);
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[word] of TRGBTriple;
var
SL: PRGBTripleArray;
y: Integer;
x: Integer;
begin
ABitmap.PixelFormat := pf24bit;
for y := 0 to ABitmap.Height - 1 do
begin
SL := ABitmap.ScanLine[y];
for x := 0 to ABitmap.Width - 1 do
with SL[x] do
begin
rgbtRed := rgbtRed div 2;
rgbtGreen := rgbtGreen div 2;
rgbtBlue := rgbtBlue div 2;
end;
end;
end;
Then, when we want to display our modal message, we create a bitmap 'screenshot' of our current form, fade it, and place it on top of all controls of the form:
procedure TForm1.ButtonClick(Sender: TObject);
var
bm: TBitmap;
pn: TPanel;
img: TImage;
begin
bm := GetFormImage;
try
FadeBitmap(bm);
pn := TPanel.Create(nil);
try
img := TImage.Create(nil);
try
img.Parent := pn;
pn.BoundsRect := ClientRect;
pn.BevelOuter := bvNone;
img.Align := alClient;
img.Picture.Bitmap.Assign(bm);
pn.Parent := Self;
ShowMessage('Hello, Faded Background!');
finally
img.Free;
end;
finally
pn.Free;
end;
finally
bm.Free;
end;
end;
Hint: If you have more than one modal dialog to display in your application, you probably want to refactor this. To this end, have a look at TApplicationEvent
's OnModalBegin
and OnModalEnd
events.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…