Главная страница | назад





Article #16806: Small bitmaps in a TPopUpMenu

 Question and Answer Database
FAQ1806D.txt Small bitmaps in a TPopUpMenu
Category :VCL
Platform :All
Product :All 32 bit
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;
7/16/98 4:31:28 PM

Last Modified: 01-SEP-99