evilone
evilone

Reputation: 22750

Custom MessageBox icon background white

I'm using a class for custom messageboxes. But my problem is that, icon background is always white. Code below displays the icons. Can somebody tell me what is wrong in this code? I want icon background to be transparent.

try
   if not custb then
   case i of
      MB_ICONINFORMATION:ico.Handle := LoadIcon( 0, IDI_INFORMATION);
      MB_ICONEXCLAMATION:ico.Handle := LoadIcon( 0, IDI_EXCLAMATION);
      MB_ICONQUESTION:ico.Handle := LoadIcon( 0, IDI_QUESTION);
      MB_ICONERROR:ico.Handle := LoadIcon( 0, IDI_ERROR);
   end;

   with timage.Create( frm) do
   begin
      parent := frm;
      transparent := True;

      if custb then
      begin
       height := glyph.Height;
       width := Glyph.Width;
      end 
      else
      begin
       height := ico.Height;
       width := ico.Width;
      end;

      ih := height;
      top := Height div 2 + 2;
      it := Top;
      left := Width div 2 + 2;
      il := Left + width + width div 2;

      if width <= 16 then
      begin
       il := il + 16;
       left := left + 8;
      end;
      if height <= 16 then
      begin
       it := it + 8;
       top := top + 8;
      end;
      if custb then picture := Glyph else canvas.Draw( 0, 0, ico);
     end;
    finally
    end;
    if not custb then ico.Free;
   end

Best wishes, evilone

Upvotes: 0

Views: 1045

Answers (2)

David Heffernan
David Heffernan

Reputation: 613521

My code to do this very thing looks like this:

function StandardDialogIcon(DlgType: TMsgDlgType): PChar;
begin
  case DlgType of
  mtWarning:
    Result := IDI_WARNING;
  mtError:
    Result := IDI_ERROR;
  mtInformation:
    Result := IDI_INFORMATION;
  mtConfirmation:
    Result := IDI_QUESTION;
  else
    Result := nil;
  end;
end;

...

Image.Picture.Icon.Handle := LoadIcon(0, StandardDialogIcon(DlgType));

There's no need to set any properties on Image, you can simply ignore Transparent.

Upvotes: 3

jpfollenius
jpfollenius

Reputation: 16620

Extract from online help for TImage.Transparent:

Setting Transparent sets the Transparent property of the Picture.

Note: Transparent has no effect unless the Picture property specifies a TBitmap object.

This means two things for you:

  1. only set transparent property after the picture has been assigned
  2. Use TBitmap for your image and assign thtat to the picture property.

Have a look at the following link, that describes a function that converts an icon to a bitmap: Delph-Library: Convert icon to bitmap.

Excerpt:

// Konvertiert Ico zu Bitmap
procedure IcoToBmpA(Ico: TIcon; Bmp: TBitmap; SmallIcon: Boolean);
var
  WH: Byte; // Width and Height
begin
  with Bmp do begin
    Canvas.Brush.Color := clFuchsia;
    TransparentColor := clFuchsia;

    Width := 32; Height := 32;
    Canvas.Draw(0, 0, Ico);

    if SmallIcon then WH := 16 else WH := 32;
    Canvas.StretchDraw(Rect(0, 0, WH, WH), Bmp);
    Width := WH; Height := WH;

    Transparent :=  True;
  end;
end;

Upvotes: 0

Related Questions