【发布时间】:2020-04-26 04:18:23
【问题描述】:
我正在尝试创建一个 wxPanel 来在其上绘制线条和点。
我的程序由以下窗口组成:
MyFrame : 默认框架类,继承自 wxFrame
Final_Layout : wxPanel 的一个实例,其父 MyFrame(即 this 指针),它包含我所有的控件
现在,我正在尝试创建一个新面板 Draw_Panel,我可以在上面进行绘图。我想使用 wxBoxSizer 在 Final_Layout 之外附加这个面板。
所以,我的第一步是初始化面板,我有以下代码:
void MyFrame::Initialize_Draw_Input() {
Draw_Panel = new wxPanel(this, -1); // QUESTION : Who is the parent here? Should it be Final_Layout or "this" pointer?
Draw_Panel ->Connect(wxEVT_PAINT, wxPaintEventHandler(MyFrame::OnPaint));
}
void MyFrame::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(Draw_Panel );
wxCoord x1 = 50, y1 = 60;
wxCoord x2 = 100, y2 = 60;
dc.DrawLine(x1, y1, x2, y2);
}
但是,这不起作用。我想我误解了 wxPanels 的工作原理......我将不胜感激任何帮助/指导
编辑:
根据下面的帮助(非常感谢),我对代码进行了以下更新。但是,现在我无法在面板上绘图:
void MyFrame::Initialize_Draw_Input() {
Draw_Panel = new wxPanel(Final_Panel, wxID_ANY,wxDefaultPosition,wxSize(1000,1000),wxTAB_TRAVERSAL,"");
Draw_StaticBox = new wxStaticBoxSizer(wxHORIZONTAL, Draw_Panel, _T("Cross Section"));
Draw_Sizer = new wxBoxSizer(wxHORIZONTAL);
// Draw_Panel->Connect(wxEVT_PAINT, wxPaintEventHandler(MyFrame::OnPaint), NULL, this);
wxStaticText* x = new wxStaticText(Draw_Panel,wxID_ANY,"");
Draw_Sizer->Add(x);
Draw_StaticBox->Add(Draw_Sizer,wxSizerFlags().Expand());
Draw_Panel->SetSizer(Draw_StaticBox);
PaintNow();
}
void MyFrame::PaintNow() {
wxClientDC dc(Draw_Panel);
Render(dc);
}
void MyFrame::Render(wxDC& dc) {
wxCoord x1 = 0, y1 = 60;
wxCoord x2 = 100, y2 = 60;
dc.DrawLine(x1, y1, x2, y2);
}
我的构造函数:
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "Cross Sectional Properties", wxDefaultPosition, wxSize(1500, 600), wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)) {
wxBoxSizer* Element_and_Node_hSizer = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* Info_and_Data_Input_vSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* Output_and_Draw_vSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* Main_hSizer = new wxBoxSizer(wxHORIZONTAL);
Final_Panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T(""));
Initialize_ProjectInfo_Input(); //Info_Panel, a wxFrame instance is defined here. It contains a wxGrid and other controls for project info input.
Initialize_Node_Input(); //Node_Panel, a wxFrame instance is defined here. It contains a wxGrid control for node data input.
Initialize_Element_Input(); //Element_Panel, a wxFrame instance is defined here. It contains a wxGrid control for element data input.
Initialize_Output(); //Out_Panel, a wxFrame instance defined here. It contians a wxGrid for cross sectional properties output.
Initialize_Draw_Input();
InitMenu(); //Initializes the menu.
//Combines the Node and element input wxGrid into one sizer - horizontally
Element_and_Node_hSizer->Add(Node_Panel, wxSizerFlags(1).Expand().Border(wxBOTTOM | wxLEFT |wxRIGHT));
Element_and_Node_hSizer->Add(Element_Panel, wxSizerFlags(1).Expand().Border(wxBOTTOM | wxRIGHT));
//Combines the project input and the (Node & Elemetn Input) into one sizer - vertically
Info_and_Data_Input_vSizer->Add(Info_Panel, wxSizerFlags(1).Expand().Border(wxALL));
Info_and_Data_Input_vSizer->Add(Element_and_Node_hSizer,wxSizerFlags(1).Expand());
//Combines output data and draw panel into one sizer - vertically
Output_and_Draw_vSizer->Add(Out_Panel,wxSizerFlags(1).Border(wxALL));
Output_and_Draw_vSizer->Add(Draw_Panel, wxSizerFlags(1).Expand().Border(wxLEFT| wxBOTTOM | wxRIGHT));
//Combines all into the final layout
Main_hSizer->Add(Info_and_Data_Input_vSizer);
Main_hSizer->Add(Output_and_Draw_vSizer);
SetSizer(Main_hSizer);
PaintNow();
}
同样不相关,有人知道如何将 wxGrid 居中吗? “节点坐标”部分看起来不太好。
【问题讨论】:
标签: wxwidgets