- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

Small bitmaps in a TPopUpMenu

Question:

How can I place small bitmaps in a TPopUpMenu component?

Answer:

The following example demonstrates adding a bitmap to a PopUpMenu

item using the Windows API function SetMenuItemBitmaps(). This

function takes a handle to the popup menu, the zero based position

of the menu item you wish to add the bitmaps to, and two bitmap

handles, (one bitmap specifying the image to be shown in the menus

unchecked state, and another specifying the image to be shown in the

menus checked state).



type

  TForm1 = class(TForm)

    PopupMenu1: TPopupMenu;

    Pop11: TMenuItem;

    Pop21: TMenuItem;

    Pop31: TMenuItem;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;

      Shift: TShiftState; X, Y: Integer);

  private

    { Private declarations }

    bmUnChecked : TBitmap;

    bmChecked : TBitmap;

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.FormCreate(Sender: TObject);

begin

  bmUnChecked := TBitmap.Create;

  bmUnChecked.LoadFromFile(

    'C:\Program Files\Borland\BitBtns\ALARMRNG.BMP');

  bmChecked := TBitmap.Create;

  bmChecked.LoadFromFile(

    'C:\Program Files\Borland\BitBtns\CHECK.BMP');

{Add the bitmaps to the item at index 1 in PopUpMenu}

  SetMenuItemBitmaps(PopUpMenu1.Handle,

                     1,

                     MF_BYPOSITION,

                     BmUnChecked.Handle,

                     BmChecked.Handle);

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

  bmUnChecked.Free;

  bmChecked.Free;

end;



procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);

var

  pt : TPoint;

begin

  pt := ClientToScreen(Point(x, y));

  PopUpMenu1.Popup(pt.x, pt.y);

end;






* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile