简体   繁体   English

wxPanel 和 wxFrame 之间的交互

[英]Interaction between wxPanel and wxFrame

I am trying to understand how wxFrame and wxPanel work together.我试图了解 wxFrame 和 wxPanel 如何一起工作。 I am having a hard time explaining the following code behavior:我很难解释以下代码行为:

I am trying to set up the GUI for my application.我正在尝试为我的应用程序设置 GUI。 I have a function that creates a form for data entry:我有一个 function 用于创建数据输入表单:

void MyFrame::Initialize_Project_Info() {


//Info_Panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(600, 1000), wxTAB_TRAVERSAL, _T(""));

//Info_Panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T(""));

Info_Box = new wxBoxSizer(wxHORIZONTAL);
Info_Staticbox = new wxStaticBoxSizer(wxHORIZONTAL, Info_Panel, _T("Project Information"));
Info_Grid = new wxFlexGridSizer(4, 2, 0, 0);


//Note wsSize(width,height).
Project_Name = new wxStaticText(Info_Panel, wxID_ANY, _T("Project:"));
Engineer_Name = new wxStaticText(Info_Panel, wxID_ANY, _T("Engineer:"));
CrossSection_Name = new wxStaticText(Info_Panel, wxID_ANY, _T("Cross Section ID:"));
Additional_Notes = new wxStaticText(Info_Panel, wxID_ANY, _T("Additional Notes:"));


Enter_PN = new wxTextCtrl(Info_Panel, ENTER_PN, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB, wxDefaultValidator, _T(""));
Enter_EN = new wxTextCtrl(Info_Panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB, wxDefaultValidator, _T(""));
Enter_CSN = new wxTextCtrl(Info_Panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB, wxDefaultValidator, _T(""));
Enter_AN = new wxTextCtrl(Info_Panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(250, 100), wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxTE_MULTILINE, wxDefaultValidator, _T(""));

Info_Grid->Add(Project_Name,
    wxSizerFlags().Align(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL)
    .Border(wxTOP | wxBOTTOM | wxLEFT));
Info_Grid->Add(Enter_PN, wxSizerFlags().Expand().Border(wxALL));
Info_Grid->Add(Engineer_Name,
    wxSizerFlags().Align(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL)
    .Border(wxBOTTOM | wxLEFT));
Info_Grid->Add(Enter_EN, wxSizerFlags().Expand().Border(wxBOTTOM | wxLEFT | wxRIGHT));
Info_Grid->Add(CrossSection_Name,
    wxSizerFlags().Align(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL)
    .Border(wxBOTTOM | wxLEFT));
Info_Grid->Add(Enter_CSN,
    wxSizerFlags().Expand().Border(wxBOTTOM | wxLEFT | wxRIGHT));
Info_Grid->Add(Additional_Notes,
    wxSizerFlags().Align(wxALIGN_LEFT).Border(wxBOTTOM | wxLEFT));
Info_Grid->Add(Enter_AN, wxSizerFlags().Expand().Border(wxBOTTOM | wxLEFT | wxRIGHT));

Info_Staticbox->Add(Info_Grid);
Info_Box->Add(Info_Staticbox, wxSizerFlags().Border(wxALL));
Info_Panel->SetSizer(Info_Box);

} }

This code works if I append all the objects to a wxPanel when only defined in the scope of the function.如果仅在 function 的 scope 中定义时,我将 append 所有对象都放到 wxPanel 中,则此代码有效。 If I define the wxPanel in the scope of the constructor of the wxFrame derived class, it does not work.如果我在 wxFrame 派生的 class 的构造函数的 scope 中定义 wxPanel,它不起作用。

For example, doing this does not generate the form as intended:例如,这样做不会按预期生成表单:

MyFrame::MyFrame() : wxFrame(NULL,wxID_ANY,"Cross Sectional Beam Properties",wxDefaultPosition,wxSize(1200,600),wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)) {


Info_Panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T(""));


Initialize_Project_Info();

} }

So, my question is how does wxPanel interface with wxFrame.所以,我的问题是 wxPanel 如何与 wxFrame 交互。 Why would one moving one line of code (ie wxPanel initialization) change the outcome?为什么移动一行代码(即 wxPanel 初始化)会改变结果?

From the wx docs :来自wx 文档

A frame is a window whose size and position can (usually) be changed by the user. frame是 window,其大小和 position(通常)可以由用户更改。
It usually has thick borders and a title bar, and can optionally contain a menu bar, toolbar and status bar.它通常有粗边框和标题栏,并且可以选择包含菜单栏、工具栏和状态栏。

A panel is a window on which controls are placed. panel是一个 window,上面放置了控件。
It is usually placed within a frame.它通常放置在一个框架内。 Its main feature over its parent class wxWindow is code for handling child windows and TAB traversal.其父 class wxWindow 的主要特征是用于处理子 windows 和 TAB 遍历的代码。

wxWindow is a generic class used by any type of window. wxWindow是一个通用的 class 被任何类型的 window 使用。 wxDialog is mainly used as a form where the user enters data. wxDialog主要用作用户输入数据的表单。

Usually an application shows a frame as the main window of the app.通常应用程序显示一个框架作为应用程序的主要 window。

==> While you can place controls directly in a wxFrame, because of 'TAB traversal' and a OS's typical background features you better use a wxPanel. ==> 虽然您可以将控件直接放置在 wxFrame 中,但由于“TAB 遍历”和操作系统的典型背景功能,您最好使用 wxPanel。

This panel is a child of the main wxFrame and you can buid it at wxFrame's ctor.此面板是主 wxFrame 的子面板,您可以在 wxFrame 的 ctor 处构建它。 Because in wxWidgets if a wxFrame has an unique child then this child is expanded to fit the whole client size of the wxFrame, then you don't use wxsizers for this panel-to-frame fitting.因为在 wxWidgets 中,如果一个 wxFrame 有一个唯一的子级,那么这个子级会被扩展以适应 wxFrame 的整个客户端大小,那么您就不要使用 wxsizers 来进行这种面板到框架的拟合。

When you create the controls (eg at wxPanel ctor) you set this wxPanel as the parent of the controls .当您创建控件时(例如在 wxPanel ctor 处),您将此wxPanel 设置为控件的父级 And use sizers to layout them into the panel.并使用 sizers 将它们布局到面板中。

So each control has your panel as parent;所以每个控件都有你的面板作为父级; an your panel has the frame as parent.您的面板将框架作为父级。 Don't forget that 'this' pointers may be used as parents, or pass the parent as a parameter in the function that creates the windows (panel, controls).不要忘记“this”指针可以用作父级,或者将父级作为参数传递给 function 以创建 windows(面板,控件)。

Finally, the pointers to any wxWindow-derived class are created with "new", but you don't need to delete them, wxWigets internals takes care of their deletion.最后,指向任何 wxWindow 派生的 class 的指针都是用“new”创建的,但您不需要删除它们,wxWigets 内部会处理它们的删除。

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

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