【发布时间】:2020-11-25 13:00:29
【问题描述】:
我已经实现了一个自己的组合框,用作字体选择器。为了直接在组合框中显示所选字体的外观,我创建了一个继承自 wxOwnerDrawnComboBox 的新类 FontStyleComboBox。在显示完整类之后,仅缺少字体管理部分:
class FontStyleComboBox : public wxOwnerDrawnComboBox
{
private:
std::vector<wxFont> m_fontList;
public:
virtual void OnDrawItem(wxDC& dc,const wxRect& rect,int item,int flags) const
{
if (item == wxNOT_FOUND) return;
wxCoord w1,h1,w2,h2;
dc.GetTextExtent(GetString(item),&w1,&h1);
dc.DrawText(GetString(item),
rect.x + 3,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
dc.SetFont(m_fontList[item]);
dc.GetTextExtent(GetString(item),&w2,&h2);
if (w2<250)
{
if (rect.x+10+w1>135)
dc.DrawText(_T("AaBbCcDd 1234"),
rect.x+10+w1,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
else
dc.DrawText(_T("AaBbCcDd 1234"),
135,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
}
}
virtual void OnDrawBackground(wxDC& dc, const wxRect& rect,int item, int flags) const
{
// If item is selected or even, or we are painting the
// combo control itself, use the default rendering.
if ((flags & (wxODCB_PAINTING_CONTROL | wxODCB_PAINTING_SELECTED)) ||
(item & 1) == 0)
{
wxOwnerDrawnComboBox::OnDrawBackground(dc, rect, item, flags);
return;
}
// Otherwise, draw every other background with different colour.
wxColour bgCol(245, 245, 255);
dc.SetBrush(wxBrush(bgCol));
dc.SetPen(wxPen(bgCol));
dc.DrawRectangle(rect);
}
virtual wxCoord OnMeasureItem(size_t item) const
{
return 20;
}
virtual wxCoord OnMeasureItemWidth(size_t item) const
{
return 400;
}
};
不幸的是,这个字体组合框的行为不像普通的组合框:
- 在 GUI 中跳转元素时,只要 FontStyleComboBox 应该被聚焦,它就不会突出显示
- 在它应该聚焦的点上,当按下向上/向下箭头时,它不会滑过组合框的元素,而是将焦点更改为上一个/下一个 GUI 元素(就像有人会点击 shift-tab /tab)
所以...知道这里可能缺少什么吗?我必须在这个类中添加一些焦点/焦点丢失处理吗?
这发生在 wxWidgets 3.1 / Windows 上。
【问题讨论】:
-
您在哪个 OS/wxWidgets 版本上进行测试?
-
@Igor 如问题中所述,它是 Windows 上的 wxWidgets 3.1
标签: wxwidgets