手把手有限元编程系列,给大家讲解如何利用c++语言,借助wxWidgets和opengl构建有限元程序.... 第一篇, wxWidgets初步 开发交互软件首先必须有界面, wxWidgets是很不错的界面库,由于wxWidgets的教材很少,所以本篇先介绍如何分离wxWidgets基本框架的几个类, 首先,MVApp类是主应用程序类, /***********************MVApp.h************************/#ifndef MV_APP_H#define MV_APP_H#pragma warning (disable: 4018)#include "wx_pch.h"#include "MVFrame.h"class MVApp: public wxApp{public: bool OnInit(); MVFrame * GetFrame(void);private: MVFrame * frame;};DECLARE_APP(MVApp);#endif // #ifndef MV_APP_H /*************************************MVApp.cpp**************************************/#include "MVApp.h"IMPLEMENT_APP(MVApp)//Main program (equivalent)bool MVApp::OnInit(){ // Create the main frame window frame = new MVFrame(NULL, wxT("MV Post"),wxDefaultPosition, wxSize(650, 600)); // Show the frame frame->Show(true); return true;}MVFrame * MVApp::GetFrame(void){ return frame;} MVFrame是主框架类,负责界面的构建, /*************************************MVFrame.h**************************************/#ifndef MV_FRAME_H#define MV_FRAME_H#pragma warning (disable: 4018)#include "wx_pch.h"class MVFrame: public wxFrame{public: MVFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long style = wxDEFAULT_FRAME_STYLE); void OnExit (wxCommandEvent& WXUNUSED(event)) { Close(true); } void OnAbout (wxCommandEvent& WXUNUSED(event)); private: DECLARE_EVENT_TABLE()};#endif // #ifndef MV_FRAME_H /*****************************************MVFrame.cpp******************************************/#include "MVFrame.h"BEGIN_EVENT_TABLE(MVFrame, wxFrame) EVT_MENU(wxID_EXIT, MVFrame::OnExit) EVT_MENU(ID_ABOUT, MVFrame::OnAbout)END_EVENT_TABLE()/* My frame constructor */MVFrame::MVFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(frame, wxID_ANY, title, pos, size, style){ CreateMenubar(); // Create a statubar CreateStatusBar(2); SetStatusText(wxT("MyFEM"),0); } void MVFrame::CreateMenubar(){ wxMenuBar *menuBar = new wxMenuBar; wxMenu *winMenu = new wxMenu; winMenu->Append(wxID_EXIT, wxT("E&xit")); menuBar->Append(winMenu, wxT("&File")); winMenu = new wxMenu; winMenu->Append(ID_ABOUT, _T("About...")); menuBar->Append(winMenu, _T("&Help")); SetMenuBar(menuBar);} /* Intercept menu commands */ void MVFrame::OnAbout(wxCommandEvent& WXUNUSED(event)){ (void)wxMessageBox(_T("About MVPost"));} 另外,头文件wx_pch.h里面设置基本的wxWidgets链接, /***************************************wx_pch.h****************************************/#ifndef WX_PCH_H_INCLUDED#define WX_PCH_H_INCLUDED #if ( defined(USE_PCH) && !defined(WX_PRECOMP) )#define WX_PRECOMP#endif // USE_PCH // basic wxWidgets headers#include <wx/wxprec.h> // for use xrc files#include <wx/xrc/xmlres.h> #ifdef __BORLANDC__#pragma hdrstop#endif #ifndef WX_PRECOMP#include <wx/wx.h>#endif #if !wxUSE_GLCANVAS #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"#endif #ifdef USE_PCH// put here all your rarely-changing header files #endif // USE_PCH #endif // WX_PCH_H_INCLUDED 将以上5个文件添加到设置好wxWidgets环境的VC工程中,即可生成漂亮的程序界面, 在此基础上就可以开始添加有限元类了, 生成程序如下:

评论