【问题标题】:New Window wxWidgets新窗口 wxWidgets
【发布时间】:2014-07-18 09:38:33
【问题描述】:

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

在我的 wxWidgets 应用程序中,我想从菜单选项中新建一个 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;
}

对不起。程序在新建 htmlwindow 后抛出分段错误。

我第一次尝试只是新的wxHtmlWindow(NULL),没有其他wxWindows,但似乎不起作用,-&gt;LoadPage 有 segfault 。然后我试了new wxHtmlWindow(this)htmlwindow显示在当前窗口,不是我想要的。

您可以忽略所有代码,告诉我如何新建一个 wxHtmlWindow(可能在一个新框架中?或者不是),并且可以实际执行 LoadPage 工作。

非常感谢!

顺便说一句,我使用的是 2.812 wxWidgets,带有 C++。

【问题讨论】:

    标签: c++ linux wxwidgets


    【解决方案1】:

    wxWindow 是所有其他小部件的基类,在极少数情况下您需要并且可以直接使用它。

    对于您的情况,您需要一个顶级窗口(即wxFramewxDialog)作为wxHtmlWindow 的父级。顺便说一句,只有顶级窗口才能拥有NULL 父级。

    要加载 html,您可能会使用 html_help-&gt;LoadFile(wxFileName("help.html")) 做得更好。这两个功能的文档都在线。

    【讨论】:

    • 非常感谢!以前不知道这些有什么区别。那我试试用 wxDialog。
    【解决方案2】:

    我在 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
    

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 2015-02-25
      • 2011-04-23
      • 2011-01-07
      • 1970-01-01
      相关资源
      最近更新 更多