Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - How draw a shadow effect in a complete Bitmap image?

I want know if is possible draw a shadow effect in a complete Bitmap image already existent and after have a effect similar to this example below, where all area behind modal Form is my new Bitmap image already with the shadow effect? =>

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

A VCL form with a faded background when a modal window is shown.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...