【发布时间】:2014-10-27 15:29:38
【问题描述】:
我有一个关于 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;
【问题讨论】:
-
应该可以。你确定你的处理程序分配了
OnDrawItem事件吗? -
是的,我确定。我检查出每个项目都设置了事件处理程序。
标签: delphi