自定义MessageBox象背景白色

时间:2011-02-14 06:18:57

标签: delphi user-interface

我正在使用自定义消息框的类。但我的问题是,图标背景总是白色的。下面的代码显示图标。有人能告诉我这段代码有什么问题吗?我希望图标背景透明。

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

祝福, evilone

2 个答案:

答案 0 :(得分:3)

我这样做的代码看起来像这样:

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));

无需在Image上设置任何属性,您只需忽略Transparent

答案 1 :(得分:0)

摘自TImage.Transparent的在线帮助:

  

设置透明设置   图片的透明属性。

     

注意:透明无效   除非Picture属性指定   一个TBitmap对象。

这对您来说意味着两件事:

  1. 仅在指定图片后设置透明属性
  2. 使用TBitmap为您的图片并将thtat分配给图片属性。
  3. 查看以下链接,该链接描述了将图标转换为位图的功能:Delph-Library: Convert icon to bitmap

    摘录:

    // 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;
    
相关问题