Recent

Author Topic: bitmap is not transparent on menuitem and form?  (Read 3603 times)

vkhoa

  • New Member
  • *
  • Posts: 47
bitmap is not transparent on menuitem and form?
« on: August 03, 2014, 01:46:17 pm »
I have a code like this to assign icon to menuitem
first place a menu with a menuitem named "menuitem1"
Code: [Select]
Unit Unit1;

{$mode objfpc}{$H+}

Interface

Uses
        Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus;

Type

{ TForm1 }

        TForm1 = Class(TForm)
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
                micon:tbitmap;
Procedure FormCreate(Sender: TObject);
Procedure FormPaint(Sender: TObject);
        Private
                { private declarations }
        Public
                { public declarations }
        End;

Var
        Form1: TForm1;

Implementation

{$R *.lfm}

{ TForm1 }

Procedure TForm1.FormCreate(Sender: TObject);
Begin
   micon:=tbitmap.Create;
   micon.Width:=22;micon.Height:=16;
   micon.TransparentColor:=clblack;
   micon.canvas.Brush.Color:=clyellow;
   micon.Canvas.Ellipse(1,1,21,16);
   menuitem1.Bitmap:=micon;
end;

Procedure TForm1.FormPaint(Sender: TObject);
Begin
   canvas.Draw(50,50,micon);
end;

End.

I want the icon to be transparent at the black color but this code didn't work.
Many thanks to anyone can show me how to do it!
« Last Edit: August 03, 2014, 01:53:58 pm by vkhoa »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: bitmap is not transparent on menuitem and form?
« Reply #1 on: August 03, 2014, 03:18:26 pm »
Remove the form OnPaint handler (you want the image painted on the menubar, not on the form), and add an ImageList to the form. Then change the form's OnCreate method to something like the following:

Code: [Select]
unit mainMenuIcons;

{$mode objfpc}{$H+}

interface

uses
  Forms, Controls, Graphics, menus;

type

  { TForm1 }

  TForm1 = class(TForm)
    ImageList1: TImageList;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  bmp: TBitmap;
begin
  mainmenu1.Images:=ImageList1;
  MenuItem1.ImageIndex:=0;
  bmp:=TBitmap.Create;
  try
    bmp.SetSize(24, 24);
    bmp.TransparentColor:=clBlack;
    bmp.Transparent:=True;
    bmp.Canvas.Brush.Color:=clYellow;
    bmp.Canvas.Ellipse(2, 2, 14, 14);
    ImageList1.Add(bmp, nil);
  finally
    bmp.Free;
  end;
end;

end.

Of course you can also set the mainmenu's Images property and the menuitem's ImageIndex property in the Object Inspector, rather than in code if you prefer.

« Last Edit: August 03, 2014, 03:27:43 pm by howardpc »

vkhoa

  • New Member
  • *
  • Posts: 47
Re: bitmap is not transparent on menuitem and form?
« Reply #2 on: August 03, 2014, 03:39:20 pm »
from your hint, I did it
simply add the code
Code: [Select]
micon.transparent:=true;thanks very much
« Last Edit: August 03, 2014, 03:51:47 pm by vkhoa »

 

TinyPortal © 2005-2018