新窗口wxWidgets

在问这个问题之前,我确实search了很多东西。 对不起,如果这是一个简单的问题。

在我的wxWidgets应用程序中,我想从菜单选项中新build一个wxHtmlWindow 。 (我已经有我的主要应用程序窗口正确显示)。

我设置了EVENT_TABLE

 EVT_MENU(wxID_HELP, MyFrame::OnHelp) 

然后函数(我有一个指针html_help类私有):

 void MyFrame::OnHelp(wxCommandEvent& event) // Callback for the Help menu item { cout << "Enter OnHelp" << endl; wxWindow* help_win = new wxWindow(NULL, -1, wxDefaultPosition, wxSize(400, 600)); cout << "New window" << endl; help_win->Show(true); cout << "Show" << endl; html_help = new wxHtmlWindow(help_win, -1); cout << "After new" << endl; wxString m_Name = wxT("help.html"); //SetTopWindow(html_help); bool hel = html_help->LoadPage(m_Name); cout << "After Loadpage" << endl; } 

对不起,couts。 程序在新的html窗口后抛出分段错误。

我第一次尝试只是新的wxHtmlWindow(NULL)没有其他的wxWindows,但似乎不工作, ->LoadPage有段错误。 然后我尝试new wxHtmlWindow(this) ,在当前窗口显示的htmlwindow ,不是我想要的。

你可能会忽略所有的代码,并告诉我如何新build一个wxHtmlWindow(也许在一个新的框架?或不),可以实际上做LoadPage的工作。

非常感谢你!

顺便说一句,我正在使用2.812 wxWidgets,与C + +。

wxWindow是所有其他小部件的基类,在需要时可以直接使用。

对于你的情况,你需要一个顶层窗口(这是一个wxFramewxDialog )作为你的wxHtmlWindow的父wxHtmlWindow 。 顺便说一句,只有顶级窗口可以有NULL父。

要加载html,你可能会用html_help->LoadFile(wxFileName("help.html")) 。 该文档在线功能。

我在Windows的纸牌游戏程序中使用了以下内容。

不知道这个代码是多么便携或一般。

程序处于原理示范的状态,所以这个代码也不是很干净或一般化,只是我所需要的。

 #pragma once // Copyright © 2014 Alf P. Steinbach. // DEBUG: #include <thisApp/cppx/trace_stream.h> #include <wx/msgdlg.h> //#include <thisApp/ie/css3_support.h> // ie::Css3_support #include <thisApp/wxx/underscore_macro.h> // _ #include <wx/filesys.h> // wxFileSystem #include <wx/fs_mem.h> // wxMemoryFSHandler #include <wx/dialog.h> // wxDialog #include <wx/sizer.h> // wxBoxSizer //#include <wx/html/htmlwin.h> // wxHtmlWindow #include <wx/webview.h> // wxWebView, wxWebViewEvent #include <wx/webviewfshandler.h> // wxWebViewFSHandler #include <wx/statline.h> // wxStaticLine #include <wx/button.h> // wxButton #include <wx/utils.h> // wxMilliSleep, wxLaunchDefaultBrowser #include <thisApp/wxx/Named_bitmap.h> namespace wxx { using cppx::trace_stream; using std::endl; class Html_dialog : public wxDialog { public: struct Args { wxWindow* parent = nullptr; wxWindowID id = wxID_ANY; wxString title = ""; wxPoint pos = wxDefaultPosition; wxSize size = wxDefaultSize; long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER; wxString name = wxDialogNameStr; Args() {} }; Html_dialog( Args const& args = Args() ) : wxDialog( args.parent, args.id, args.title, args.pos, args.size, args.style, args.name ) {} }; inline void show_dialog( wxWindow* const parent, wxString const& title, wxString const& html, wxSize const& size = wxSize(380, 160), Named_bitmap const* const p_first_bitmap = nullptr, int const n_bitmaps = 0 ) { wxFileSystem::AddHandler( new wxMemoryFSHandler ); for( auto p = p_first_bitmap; p != p_first_bitmap + n_bitmaps; ++p ) { wxMemoryFSHandler::AddFile( p->filename, p->bitmap, wxBITMAP_TYPE_PNG ); } static char const* const utf8_bom = "\xEF\xBB\xBF"; char const* const html_cstr = ( html.StartsWith( utf8_bom )? html.c_str() + 3 : html.c_str() ); trace_stream << "------------------------------------" << endl; trace_stream << html_cstr << endl; trace_stream << "------------------------------------" << endl; wxMemoryFSHandler::AddFile( "dialog.htm", html_cstr ); //wxMemoryFSHandler::AddFile( "dialog.htm", "<html><body>Bah</body></html>" ); Html_dialog::Args args; args.parent = parent; args.title = "About"; Html_dialog dlg( args ); wxWebView* browser = wxWebView::New( &dlg, wxID_ANY, wxWebViewDefaultURLStr, wxDefaultPosition, size ); browser->RegisterHandler( wxSharedPtr<wxWebViewHandler>( new wxWebViewFSHandler( "memory" ) ) ); browser->LoadURL( "memory:dialog.htm" ); //browser->SetPage( "<html><body>Bah</body></html>", "memory:dialog.htm" ); dlg.SetLabel( title ); struct WcEvent { static void handler (wxWebViewEvent& event) { wxString const url = event.GetURL(); //wxMessageBox( url, "Navigation to:" ); if( !url.StartsWith( "memory:" ) ) { event.Veto(); wxLaunchDefaultBrowser( url, wxBROWSER_NEW_WINDOW ); } } }; browser->Bind( wxEVT_WEBVIEW_NAVIGATING, &WcEvent::handler ); auto* const sizer = new wxGridSizer( 0 ) ; sizer->Add( browser, 0, wxEXPAND | wxALL, 0); // sizer->Add( browser, 1, wxALL, 10); // sizer->Add( new wxStaticLine( &dlg, -1 ), 0, wxEXPAND | wxLEFT | wxRIGHT, 10 ); // sizer->Add( new wxButton( &dlg, wxID_OK, "Ok" ), 0, wxALL | wxALIGN_RIGHT, 15 ); dlg.SetAutoLayout( true ); dlg.SetSizer( sizer ); sizer->Fit( &dlg ); //dlg.Centre(); dlg.SetLayoutAdaptationMode( wxDIALOG_ADAPTATION_MODE_DISABLED ); dlg.ShowModal(); wxMemoryFSHandler::RemoveFile( "dialog.htm" ); for( auto p = p_first_bitmap; p != p_first_bitmap + n_bitmaps; ++p ) { wxMemoryFSHandler::RemoveFile( p->filename ); } } } // namespace wxx