2011-02-14 131 views
0

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

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

+0

这是相当多的代码负载...你可能想尝试减少到​​相关的部分。 – jpfollenius 2011-02-14 07:42:24

+0

@Smasher缩小了代码 – evilone 2011-02-14 07:46:11

回答

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

0

摘自在线帮助TImage.Transparent

设置透明套图片的 透明特性。

注意:除非图片属性指定了TBitmap对象的 ,否则透明无效 。

这意味着你两件事情:

  1. 只设置透明的属性之后,照片就被分配
  2. 使用TBitmap您的图像和分配thtat Picture属性。

看看下面的链接,它描述了一个将图标转换为位图的函数: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;