zwx_helper

zwx_helper

通过重载c++operator,实现一种轻松的wxWidgets界面编程风格,如html编写页面一样直观容易。

举一例,一个界面页有四块区,如果是开发html的话,是从头到脚一气书写

复制代码; "复制代码")

<div id='0'>

<div id\='1'\>
    <input type\="button" onclick\="handler()"\>
</div\>
<div id\='2'\>
    <input type\="button" onclick\="handler()"\>
</div\>
<div id\='3'\>
    <input type\="button" onclick\="handler()"\>
</div\>
<div id\='4'\>
    <input type\="button" onclick\="handler()"\>
</div\>

</div>

复制代码; "复制代码")

我的目标是,让c++开发的代码书写也一气呵成

复制代码; "复制代码")

Frame* frame = new Frame;
layout::begin(new layout)

(layout::begin(new layout)
    (new button) \[ onclick = \[\] (event&) {} \]
    (layout::end)
)
(layout::begin(new layout)
    (new button) \[ onclick = \[\] (event&) {} \]
    (layout::end)
)
(layout::begin(new layout)
    (new button) \[ onclick = \[\] (event&) {} \]
    (layout::end)
)
(layout::begin(new layout)
    (new button) \[ onclick = \[\] (event&) {} \]
    (layout::end)
)
(layout::end, \[\=\] (layout& layout) {
                            frame\->SetLayout(layout);
                        });

复制代码; "复制代码")

菜单布局编写直观自然,处理器函数绑定同时到位

复制代码; "复制代码")

Frame* frame = new Frame;
menu::begin(new MenuBar)

("File", 
     menu::begin(new Menu(ID\_OPEN, "open"),   \[=\] (event&) {  })
                (new Menu(ID\_NEW,   "new"),    \[=\] (event&) {  })
                (menu::end)
 )
 ("About",
      menu::begin(new Menu(ID\_HELP,   "help"),    \[=\] (event&)  {   })
                 (menu::end)
 )
 (menu::end,   \[\=\] (MenuBar\* mb) {
                                  frame\->SetMenuBar(mb);
                            });

复制代码; "复制代码")

理想总是美好的,现在回到现实中的界面开发,让人眼痛的c++代码,

1 必须在多处代码操作,步骤烦多,分散在多处代码。

2 布局编写不符合人的思维-将整体拆分再拆分,而是从枝节末端创建一层一层往上添加挂钩。

MFC,WTL与wxWidgets的开发步骤类似,这里只关注wxWidgets,

1 在头文件中,自定义窗口类声明事件(窗口消息)分派函数;

2 在源文件中,添加事件(窗口消息)分派函数的实现宏;

3 添加一个事件(窗口消息)处理器函数,

3-1 在头文件中,声明为自定义窗口类的成员函数

3-2 在源文件中,实现该函数

3-3 在源文件中,在那个TABLE宏里添加事件id,处理器函数

4 在某个窗口容器的初始化回调中,创建事件id对应的子窗口/控件,设置相关的id

5 过了一段时间后,维护某个控件的处理器函数

5-1 在布局代码中找到这个控件,看到它相关的id

5-2 在源代码找到分派事件的窗口类,在它的TABLE宏里众多条目中找出对应项

5-3 最后才定位到目标处理器函数

做一样事情,却要在多处代码文件中跳转切换,只要漏了一步又会浪费时间调试一翻,实在烦人烦心。

// MyFrame.h

enum

{

ID_SOME

};

class`MyFrame :publicwxFrame`

{

public`:`

void`hanlde_some_event(wxEvent&);`

private`:`

wxDECLARE_EVENT_TABLE()     

}

// MyFrame.cpp

wxBEGIN_EVENT_TABLE(MyFrame,  wxFrame)

EVT_XXXX(ID_SOME,    &MyFrame::handle_some_event)

wxEND_EVENT_TABLE()

void`MyFrame::handle_some_event(wxEvent&)`

{

// todo

}

MyFrame::MyFrame()

{

// ....

this`->Add(newwxSomeWindow(this, ID_SOME, ...);`

// ....

}

下面是用zwx_helper重写官方layout sample布局的代码的对比

先是官方代码

layout.h https://github.com/wxWidgets/wxWidgets/blob/master/samples/layout/layout.h

layout.cpp https://github.com/wxWidgets/wxWidgets/blob/master/samples/layout/layout.cpp

复制代码; "复制代码")

// ---------------------------------------------------------------------------- // MyFrame // ----------------------------------------------------------------------------
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)

EVT_MENU(LAYOUT_TEST_PROPORTIONS, MyFrame::TestProportions)
EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers)
EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers)
EVT_MENU(LAYOUT_TEST_GB_SIZER, MyFrame::TestGridBagSizer)
EVT_MENU(LAYOUT_TEST_SET_MINIMAL, MyFrame::TestSetMinimal)
EVT_MENU(LAYOUT_TEST_NESTED, MyFrame::TestNested)
EVT_MENU(LAYOUT_TEST_WRAP, MyFrame::TestWrap)
wxEND_EVENT_TABLE() // Define my frame constructor
MyFrame::MyFrame()

   : wxFrame(NULL, wxID\_ANY, "wxWidgets Layout Demo")

{

SetIcon(wxICON(sample)); // Make a menubar
wxMenu \*file\_menu = new wxMenu;

file\_menu\->Append(LAYOUT\_TEST\_PROPORTIONS, "&Proportions demo...\\tF1");
file\_menu\->Append(LAYOUT\_TEST\_SIZER, "Test wx&FlexSizer...\\tF2");
file\_menu\->Append(LAYOUT\_TEST\_NB\_SIZER, "Test &notebook sizers...\\tF3");
file\_menu\->Append(LAYOUT\_TEST\_GB\_SIZER, "Test &gridbag sizer...\\tF4");
file\_menu\->Append(LAYOUT\_TEST\_SET\_MINIMAL, "Test Set&ItemMinSize...\\tF5");
file\_menu\->Append(LAYOUT\_TEST\_NESTED, "Test nested sizer in a wxPanel...\\tF6");
file\_menu\->Append(LAYOUT\_TEST\_WRAP, "Test wrap sizers...\\tF7");

file\_menu\->AppendSeparator();
file\_menu\->Append(LAYOUT\_QUIT, "E&xit", "Quit program");

wxMenu \*help\_menu = new wxMenu;
help\_menu\->Append(LAYOUT\_ABOUT, "&About", "About layout demo...");

wxMenuBar \*menu\_bar = new wxMenuBar;

menu\_bar\->Append(file\_menu, "&File");
menu\_bar\->Append(help\_menu, "&Help"); // Associate the menu bar with the frame

SetMenuBar(menu_bar); #if wxUSE_STATUSBAR CreateStatusBar(2);

SetStatusText("wxWidgets layout demo"); #endif // wxUSE\_STATUSBAR wxPanel\* p = new wxPanel(this, wxID\_ANY); // we want to get a dialog that is stretchable because it // has a text ctrl in the middle. at the bottom, we have // two buttons which.

wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); // 1) top: create wxStaticText with minimum size equal to its default size

topsizer->Add( new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_RIGHT)." ),
    wxSizerFlags().Align(wxALIGN\_RIGHT).Border(wxALL & ~wxBOTTOM, 5));
topsizer\->Add( new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_LEFT)." ),
    wxSizerFlags().Align(wxALIGN\_LEFT).Border(wxALL & ~wxBOTTOM, 5));
topsizer\->Add( new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_CENTRE\_HORIZONTAL)." ),
    wxSizerFlags().Align(wxALIGN\_CENTRE\_HORIZONTAL).Border(wxALL & ~wxBOTTOM, 5)); // 2) top: create wxTextCtrl with minimum size (100x60)
topsizer->Add( new wxTextCtrl( p, wxID\_ANY, "My text (wxEXPAND).", wxDefaultPosition, wxSize(100,60), wxTE\_MULTILINE),
    wxSizerFlags(1).Expand().Border(wxALL, 5)); // 2.5) Gratuitous test of wxStaticBoxSizers
wxBoxSizer \*statsizer = new wxStaticBoxSizer( new wxStaticBox(p, wxID\_ANY, "A wxStaticBoxSizer"), wxVERTICAL );
statsizer\->Add( new wxStaticText(p, wxID\_ANY, "And some TEXT inside it"),
    wxSizerFlags().Border(wxALL, 30));
topsizer\->Add(
    statsizer,
    wxSizerFlags(1).Expand().Border(wxALL, 10)); // 2.7) And a test of wxGridSizer
wxGridSizer \*gridsizer = new wxGridSizer(2, 5, 5);
gridsizer\->Add(new wxStaticText(p, wxID\_ANY, "Label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL));
gridsizer\->Add(new wxTextCtrl(p, wxID\_ANY, "Grid sizer demo"),
            wxSizerFlags(1).Align(wxGROW | wxALIGN\_CENTER\_VERTICAL));
gridsizer\->Add(new wxStaticText(p, wxID\_ANY, "Another label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL));
gridsizer\->Add(new wxTextCtrl(p, wxID\_ANY, "More text"),
            wxSizerFlags(1).Align(wxGROW | wxALIGN\_CENTER\_VERTICAL));
gridsizer\->Add(new wxStaticText(p, wxID\_ANY, "Final label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL));
gridsizer\->Add(new wxTextCtrl(p, wxID\_ANY, "And yet more text"),
            wxSizerFlags().Align(wxGROW | wxALIGN\_CENTER\_VERTICAL));
topsizer\->Add(
    gridsizer,
    wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10)); #if wxUSE\_STATLINE
// 3) middle: create wxStaticLine with minimum size (3x3)
topsizer->Add( new wxStaticLine( p, wxID\_ANY, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
    wxSizerFlags().Expand()); #endif // wxUSE\_STATLINE

// 4) bottom: create two centred wxButtons
wxBoxSizer \*button\_box = new wxBoxSizer( wxHORIZONTAL );
button\_box\->Add( new wxButton( p, wxID\_ANY, "Two buttons in a box" ),
    wxSizerFlags().Border(wxALL, 7));
button\_box\->Add( new wxButton( p, wxID\_ANY, "(wxCENTER)" ),
    wxSizerFlags().Border(wxALL, 7));

topsizer\->Add(button\_box, wxSizerFlags().Center());

p\->SetSizer( topsizer ); // don't allow frame to get smaller than what the sizers tell it and also set // the initial size as calculated by the sizers
topsizer->SetSizeHints( this );

}

复制代码; "复制代码")

最后是我的代码

复制代码; "复制代码")

std::shared_ptr<MyFrameDelegate> sptr_delegate; bool MyApp::OnInit()
{ if (!wxApp::OnInit()) return false;


MyFrame\* frame = new MyFrame;
sptr\_delegate \= std::shared\_ptr<MyFrameDelegate>(new MyFrameDelegate(frame));
MyFrameDelegate\* delegate = sptr\_delegate.get();
wxPanel\* p = new wxPanel((wxWindow\*)frame, wxID\_ANY);
wxMenuBar\* mb = menu::begin(new wxMenuBar)
            ("&File",
             menu::begin(new wxMenu)
                (LAYOUT\_TEST\_PROPORTIONS, "&Proportions demo...\\tF1", &MyFrame::TestMenuCommand, frame)
                (LAYOUT\_TEST\_SIZER, "Test wx&FlexSizer...\\tF2", &MyFrame::TestMenuCommand, frame)
                (LAYOUT\_TEST\_NB\_SIZER, "Test &notebook sizers...\\tF3", &MyFrame::TestMenuCommand, frame)
                (LAYOUT\_TEST\_GB\_SIZER, "Test &gridbag sizer...\\tF4", frame, \[=\](wxCommandEvent& event) { frame->TestMenuCommand(event); })
                (LAYOUT\_TEST\_SET\_MINIMAL, "Test Set&ItemMinSize...\\tF5")
                (LAYOUT\_TEST\_NESTED, "Test nested sizer in a wxPanel...\\tF6", frame, \[=\](wxCommandEvent& event) { sptr\_delegate->TestMenuCommand(event); })(LAYOUT\_TEST\_WRAP, "Test wrap sizers...\\tF7")
                (menu::end))
            ("&Help",
             menu::begin(new wxMenu)
                (LAYOUT\_ABOUT, "&About", "About layout demo...")
                (menu::end))
            ("level-tree",
             menu::begin(new wxMenu)
                (wxID\_ANY, "level-1.1", 
                    menu::begin(new wxMenu)
                            (wxID\_ANY, "level-1.1-level-2.1")
                            (wxID\_ANY, "level-1.1-level-2.2")
                            (menu::end))
                (wxID\_ANY, "level-1.2", 
                    menu::begin(new wxMenu)
                            (wxID\_ANY, "level-1.2-level-2.1")
                            (wxID\_ANY, "level-1.2-level-2.2")
                            (menu::end))
                (menu::end))
            (menu::end, 
                \[\=\](wxMenuBar\* mb) {
                    frame\->SetMenuBar(mb);
                });

layout::begin(new wxBoxSizer(wxVERTICAL)) // 1) top: create wxStaticText with minimum size equal to its default size
    (new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_RIGHT)." ),
    wxSizerFlags().Align(wxALIGN\_RIGHT).Border(wxALL & ~wxBOTTOM, 5))
    (new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_LEFT)." ),
    wxSizerFlags().Align(wxALIGN\_LEFT).Border(wxALL & ~wxBOTTOM, 5))
    (new wxStaticText( p, wxID\_ANY, "An explanation (wxALIGN\_CENTRE\_HORIZONTAL)." ),
    wxSizerFlags().Align(wxALIGN\_CENTRE\_HORIZONTAL).Border(wxALL & ~wxBOTTOM, 5)) // 2) top: create wxTextCtrl with minimum size (100x60)
    (new wxTextCtrl( p, wxID\_ANY, "My text (wxEXPAND).", wxDefaultPosition, wxSize(100,60), wxTE\_MULTILINE),
    wxSizerFlags(1).Expand().Border(wxALL, 5)) // 2.5) Gratuitous test of wxStaticBoxSizers
    (layout::begin(new wxStaticBoxSizer(new wxStaticBox(p, wxID\_ANY, "A wxStaticBoxSizer"), wxVERTICAL))
        (new wxStaticText(p, wxID\_ANY, "And some TEXT inside it"), wxSizerFlags().Border(wxALL, 30))
        (layout::end), wxSizerFlags(1).Expand().Border(wxALL, 10)) // 2.7) And a test of wxGridSizer
    (layout::begin(new wxGridSizer(2, 5, 5))
            (new wxStaticText(p, wxID\_ANY, "Label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL))
            (new wxTextCtrl(p, wxID\_ANY, "Grid sizer demo"),
            wxSizerFlags(1).Align(wxGROW | wxALIGN\_CENTER\_VERTICAL))
            (new wxStaticText(p, wxID\_ANY, "Another label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL))
            (new wxTextCtrl(p, wxID\_ANY, "More text"),
            wxSizerFlags(1).Align(wxGROW | wxALIGN\_CENTER\_VERTICAL))
            (new wxStaticText(p, wxID\_ANY, "Final label"),
            wxSizerFlags().Align(wxALIGN\_RIGHT | wxALIGN\_CENTER\_VERTICAL))
            (new wxTextCtrl(p, wxID\_ANY, "And yet more text"),
            wxSizerFlags().Align(wxGROW | wxALIGN\_CENTER\_VERTICAL))
            (layout::end), wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10)) #if wxUSE\_STATLINE
// 3) middle: create wxStaticLine with minimum size (3x3)
    (new wxStaticLine( p, wxID\_ANY, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
        wxSizerFlags().Expand()) #endif // wxUSE\_STATLINE

// 4) bottom: create two centred wxButtons
    (layout::begin(new wxBoxSizer( wxHORIZONTAL ))
        (new wxButton( p, wxID\_ANY, "Two buttons in a box" ),
        wxSizerFlags().Border(wxALL, 7))\[onclick = \[=\](wxCommandEvent& e) { delegate\->OnClick(e); } \]
        (new wxButton( p, wxID\_ANY, "(wxCENTER)" ),
        wxSizerFlags().Border(wxALL, 7))
        (layout::end), wxSizerFlags().Center())
    (layout::end, 
        \[\=\](wxSizer\* s) { 
            p\->SetSizer(s); 
            s\->SetSizeHints(frame); 
        });

frame\->Show(true); return true;

}

复制代码; "复制代码")

zwx_helper将会在https://github.com/bbqz007/zhelper-wxWidgets/发布。

逆向深入objc,c++ windows下仿objc动画层UI引擎

评论

暂无评论