wxwidgets 自定义控件大小无法设置。我错过了什么
我有一个源自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供有关 wxWidgets 版本或平台/编译器的任何信息,因此我将回答 wxWidgets 2.8 和 MSW。
您指定的大小不一定用于控件,因为不同平台的最佳大小可能不同。即使在 Windows 上,大小通常也取决于默认的 GUI 字体。所以wxWidgets中有多种方法可以让窗口指定它们的最小、最佳或最大尺寸。如果您有特殊要求,您可能需要覆盖其中一些。
对于您的问题,您似乎希望控件的大小小于
wxControl
的默认最佳大小:(取自
src/msw/control.cpp
)值(取自
include/wx/msw/private.h
)。如果您重写
DoGetBestSize()
以返回您想要的大小,它应该可以工作。编辑:
您评论说您无法重写
DoGetBestSize()
,因为它在wxControl
中不是虚拟的。但确实如此,因为它是在wxWindow
中引入的:(取自
include/wx/window.h
)。wxWindow
是wxControl
的父类,因此该方法确实是虚拟的,您当然可以重写它。事实上,许多控件都是这样做的,正如源代码中的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
:(taken from
src/msw/control.cpp
) with the values(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 inwxControl
. It is though, because it is introduced inwxWindow
:(taken from
include/wx/window.h
).wxWindow
is a parent class ofwxControl
, so the method is indeed virtual and you can of course override it. Many controls do in fact, as agrep
in the sources will show you.