我有一个关于TPopoupMenu和OwnerDraw的问题。我正在尝试进行一些自定义绘图,并将OwnerDraw设置为true并分配给事件处理程序OnDrawItem。我以编程方式调用MyPopup.Popup(X,Y),但OnDrawItem从未被调用过。我在这里做错什么了?
谢谢你的帮助。
编辑:
我的进一步发现表明,在使用VCL样式时存在一些问题。我分配了OnDrawItem和OnMeasureItem。现在这些处理程序被调用。实现OnDrawItem的常规方法不起作用,所以我尝试使用VCL样式,但弹出菜单没有显示任何文本。
我的事件处理程序代码(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;发布于 2014-10-27 20:08:24
必须将OnDrawItem分配给您希望绘制的PopupMenu中的每个项。
每个项目将通过调用OnDrawItem事件处理程序分别绘制-每项一个。OnDrawItem事件的发件人参数是对要为该调用绘制的特定项的引用。您可以有一个处理程序,它知道如何绘制每个项,或者,如果项有不同的绘图需求,则可以使用单独的处理程序。
如果这样做不起作用,那么我怀疑您已经为弹出菜单中的某个项创建了一个OnDrawItem事件处理程序,但没有将其分配给所有其他项目,并且已经删除了原始项,或者不知怎么没有从其中分配处理程序。
https://stackoverflow.com/questions/26591057
复制相似问题