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