Nix
Nix

Reputation: 581

TMenuItem ownerdrawing when calling PopUp

I have a question regarding TPopoupMenu and OwnerDraw. I am trying to do some custom drawing and I set OwnerDraw to true and assigned event handler OnDrawItem. I programatically call MyPopup.Popup(X, Y) but OnDrawItem never gets called. What am I doing wrong here?

Thanks for your help.

EDIT:

My further discoveries showed out that there is some issue when using VCL styles. I assigned OnDrawItem and OnMeasureItem. Now those handlers are called. The regular way of implementing OnDrawItem is not working so I tried with using VCL styles but my popup menu does not show any text.

My event handler code (OnDrawItem):

procedure TMyDisplay.EngineMenuDraw(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
var LStyles: TCustomStyleServices;
    Text: string;
const
  ColorStates: array[Boolean] of TStyleColor = (scComboBoxDisabled, scComboBox);
  FontColorStates: array[Boolean] of TStyleFont = (sfPopupMenuItemTextDisabled, sfPopupMenuItemTextNormal);


begin

 LStyles := StyleServices;

 Text := (Sender as TMenuItem).Caption;

 ACanvas.Brush.Color := LStyles.GetStyleColor(ColorStates[(Sender as TMenuItem).Enabled]);
 ACanvas.Font.Color  := LStyles.GetStyleFontColor(FontColorStates[(Sender as TMenuItem).Enabled]);

 if Selected then
 begin
   ACanvas.Brush.Color := LStyles.GetSystemColor(clHighlight);
   ACanvas.Font.Color  := LStyles.GetSystemColor(clHighlightText);
 end;

 ACanvas.FillRect(ARect);
 ACanvas.TextOut(ARect.Left + 2, ARect.Top, Text);

end;

Upvotes: 1

Views: 1551

Answers (1)

Deltics
Deltics

Reputation: 23036

OnDrawItem has to be assigned to each item in the PopupMenu that you wish to draw.

Each item will be drawn separately by a call to your OnDrawItem event handler - one per item. The Sender parameter to the OnDrawItem event is a reference to the specific item to be drawn for that call. You can have one handler which knows how to draw every item, or use separate handlers if items have different drawing needs.

If this is not working then I suspect that you have created an OnDrawItem event handler for an item in your popup menu but have not assigned it to all the others and have since deleted the original item or somehow unassigned the handler from it.

Upvotes: 1

Related Questions