wxwidgets 自定义控件大小无法设置。我错过了什么

发布于 2024-08-07 19:41:18 字数 518 浏览 4 评论 0原文

我有一个源自 wxControl 的自定义控件,位于带有垂直大小调整器的对话框内

wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
Knob* newKnob = new Knob(panel,wxID_ANY,wxDefaultPosition,wxSize(40,40));
topSizer->Add(newKnob,0,wxALL|wxALIGN_CENTER);
panel->SetSizer(topSizer);
topSizer->Fit(panel);

//panel is a wxPanel inside the dialog

,但由于某种原因,自定义控件的大小设置为 (80,100)。如果我将对话框的大小调整到超出该大小,则它会按照我指定的方式与中心对齐。

编辑: 我在 Windows Vista 上使用 wx 2.8.9 和 Visual Studio 2005。

可能缺少什么?

I have a Custom control derived from wxControl,inside a dialog with a vertical sizer

wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
Knob* newKnob = new Knob(panel,wxID_ANY,wxDefaultPosition,wxSize(40,40));
topSizer->Add(newKnob,0,wxALL|wxALIGN_CENTER);
panel->SetSizer(topSizer);
topSizer->Fit(panel);

//panel is a wxPanel inside the dialog

But for some reason ,the custom controls' size is set at (80,100).If i resize the dialog beyond that size its aligned to center as i specified.

EDIT:
Am using wx 2.8.9 on windows Vista with visual studio 2005.

What could be missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-08-14 19:41:18

您没有提供有关 wxWidgets 版本或平台/编译器的任何信息,因此我将回答 wxWidgets 2.8 和 MSW。

您指定的大小不一定用于控件,因为不同平台的最佳大小可能不同。即使在 Windows 上,大小通常也取决于默认的 GUI 字体。所以wxWidgets中有多种方法可以让窗口指定它们的最小、最佳或最大尺寸。如果您有特殊要求,您可能需要覆盖其中一些。

对于您的问题,您似乎希望控件的大小小于 wxControl 的默认最佳大小:(

wxSize wxControl::DoGetBestSize() const
{
    return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
}

取自 src/msw/control.cpp)值

#define DEFAULT_ITEM_WIDTH  100
#define DEFAULT_ITEM_HEIGHT 80

(取自 include/wx/msw/private.h)。

如果您重写 DoGetBestSize() 以返回您想要的大小,它应该可以工作。

编辑:

您评论说您无法重写DoGetBestSize(),因为它在wxControl中不是虚拟的。但确实如此,因为它是在 wxWindow 中引入的:(

// get the size which best suits the window: for a control, it would be
// the minimal size which doesn't truncate the control, for a panel - the
// same size as it would have after a call to Fit()
virtual wxSize DoGetBestSize() const;

取自 include/wx/window.h)。 wxWindowwxControl 的父类,因此该方法确实是虚拟的,您当然可以重写它。事实上,许多控件都是这样做的,正如源代码中的 grep 所示。

You didn't provide any information about the wxWidgets version or your platform / compiler, so I will answer for wxWidgets 2.8 and MSW.

The size you specify is not necessarily used for the control, since the best size for different platforms may well be different. Even on Windows the size will usually depend on the default GUI font. So there are various methods in wxWidgets that let windows specify their minimum, best or maximum size. If you have special requirements you may need to override some of them.

For your problem it looks like you want to have the control have a smaller size than the default best size of wxControl:

wxSize wxControl::DoGetBestSize() const
{
    return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
}

(taken from src/msw/control.cpp) with the values

#define DEFAULT_ITEM_WIDTH  100
#define DEFAULT_ITEM_HEIGHT 80

(taken from include/wx/msw/private.h).

If you override DoGetBestSize() to return your intended size it should work.

Edit:

You comment that you cannot override DoGetBestSize() since it is not virtual in wxControl. It is though, because it is introduced in wxWindow:

// get the size which best suits the window: for a control, it would be
// the minimal size which doesn't truncate the control, for a panel - the
// same size as it would have after a call to Fit()
virtual wxSize DoGetBestSize() const;

(taken from include/wx/window.h). wxWindow is a parent class of wxControl, so the method is indeed virtual and you can of course override it. Many controls do in fact, as a grep in the sources will show you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文