简体   繁体   English

使用 C++ wx-widgets 为程序创建固定大小的主框架

[英]Create fixed size main frame for a program using C++ wx-widgets

I was watching a quick basic tutorial about wx-widgets and creationg of simple GUI using C++.我正在观看有关 wx-widgets 和使用 C++ 创建简单 GUI 的快速基础教程。 I wanted to make my window a fixed size one (impossible to maximize or drag to expand/contract).我想让我的窗口固定大小(不可能最大化或拖动以展开/收缩)。 Currently the code I have on the constructor of my main class is the one below:目前,我在主类的构造函数上的代码如下:

main::main() : wxFrame(nullptr, wxID_ANY, "Usage Statistics Viewing Console", wxDefaultPosition, wxSize(1008, 567), wxDEFAULT_FRAME_STYLE)
{       
    m_btn1 = new wxButton(this, 1, "Connect Database", wxPoint(10, 10), wxSize(150, 50));
    m_txt1 = new wxTextCtrl(this, wxID_ANY, " ", wxPoint(10, 70), wxSize(300, 30));
    m_list1 = new wxListBox(this, wxID_ANY, wxPoint(10, 110), wxSize(300, 300));
}

I believe I have to make changes to the wxFrame parameters, but I searched on the wx database about the function and to be honest I don't have enough knowledge to understand half of what is there.我相信我必须对 wxFrame 参数进行更改,但是我在 wx 数据库上搜索了有关该函数的信息,老实说我没有足够的知识来了解其中的一半内容。 I suppose I could change the way the window behaves with the "styles" param.我想我可以使用“样式”参数更改窗口的行为方式。 but I tried them all and none produce the effect I'm looking for.但我都试过了,没有一个能产生我想要的效果。

If you look at the documentation for wxFrame , under the styles section it states that wxDEFAULT_FRAME_STYLE is defined as如果您查看wxFrame文档,在样式部分下,它指出wxDEFAULT_FRAME_STYLE被定义为

wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN. 

To make it so that the user can not resize the window, you just need to remove the wxRESIZE_BORDER from this set.为了让用户不能调整窗口大小,你只需要从这个集合中删除wxRESIZE_BORDER Like so:像这样:

main::main() : wxFrame(nullptr, wxID_ANY, "Usage Statistics Viewing Console",
                       wxDefaultPosition, wxSize(1008, 567),
                       wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxSYSTEM_MENU
                       | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN)
{...

or if your familiar with C's bitwise operations, you can write this more compactly as:或者,如果您熟悉 C 的按位运算,则可以更简洁地将其编写为:

main::main() : wxFrame(nullptr, wxID_ANY, "Usage Statistics Viewing Console",
                       wxDefaultPosition, wxSize(1008, 567),
                       wxDEFAULT_FRAME_STYLE & ~wxRESIZE_BORDER)

I'd like to offer 2 other suggestions.我想提供另外 2 个建议。 First you can improve the appearance of your frames by using a panel as the only direct child of the frame and then having all other controls be children of the panel.首先,您可以通过使用面板作为框架的唯一直接子项,然后让所有其他控件成为面板的子项来改善框架的外观。

wxPanel* panel = new wxPanel(this, wxID_ANY);

m_btn1 = new wxButton(panel, wxID_ANY, "Connect Database", wxPoint(10, 10), wxSize(150, 50));
m_txt1 = new wxTextCtrl(panel, wxID_ANY, " ", wxPoint(10, 70), wxSize(300, 30));
m_list1 = new wxListBox(panel, wxID_ANY, wxPoint(10, 110), wxSize(300, 300));

Second, at some point you should look into using sizers to manage the size and position of your controls instead of using the size and position parameters in the controls constructor.其次,在某些时候,您应该考虑使用 sizers 来管理控件的大小和位置,而不是使用控件构造函数中的大小和位置参数。 Sizers can be a little tricky to learn but are a great benefit once you get the hang of them. Sizer 学习起来可能有点棘手,但一旦掌握了它们,就会大有裨益。 There are also tools like wxFormbuilder or wxCrafter that can help with laying out your forms using sizers.还有像 wxFormbuilder 或 wxCrafter 这样的工具可以帮助您使用 sizer 来布置表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM