接着【GUI编程之准备--1】下一步操作:
/Users/shelley/Downloads/wxWidgets-2.9.5/samples 创建一个文件夹
$mkdir helloworld
$cd helloworld
编写一个helloworld.cpp文件
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( "this is my first wxWidget demo ", wxPoint(50, 50), wxSize(450, 340) );
frame->Show( true );
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT,"&About...\tF1","show about dialog");
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar );
CreateStatusBar(2);
SetStatusText( wxT("Welcome to my first wxWidgets!") );
}
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
Close( true );
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( wxT("This is a wxWidgets' Hello world sample"),
wxT("About Hello World"), wxOK | wxICON_INFORMATION,this );
}
void MyFrame::OnHello(wxCommandEvent& WXUNUSED(event))
{
wxLogMessage("Hello world from wxWidgets!");
}
在/Users/shelley/Downloads/wxWidgets-2.9.5/cocoabuildwx/samples/目录下创建helloworld文件夹
$mkdir helloworld
$cd helloworld
创建makefile文件
$vi makefile
此文件的内容,我在/Users/shelley/Downloads/wxWidgets-2.9.5/cocoabuildwx/samples/目录下随便一个例子的 makefile复制到helloworld下
$cd ../minimal
$cp Makefile ./helloworld/makefile
然后替换文件中minimal为helloworld 及 MINIMAL 为HELLOWORLD手动在检查下是否存在没有替换的内容。
$make clean (第一次编译就不需要执行此命令了)
$make
$ls -lrt
-rw-r--r--@ 1 shelley staff 7537 10 19 13:13 Makefile
-rw-r--r-- 1 shelley staff 89408 10 21 11:14 helloworld_helloworld.o
drwxr-xr-x 3 shelley staff 102 10 21 11:14 helloworld.app
-rwxr-xr-x@ 2 shelley staff 6018256 10 21 11:14 helloworld
已经生成可执行文件helloworld.app
在图形界面中运行。
注意:
1- 你会发现源文件路径为:
/Users/shelley/Downloads/wxWidgets-2.9.5/samples/helloworld/helloworld.cpp
makefile文件路径为:
/Users/shelley/Downloads/wxWidgets-2.9.5/cocoabuildwx/samples/helloworld/makefile
因为makefile是wxwidgets开发者已经写好编译工程的,所以只能按照它得模式处理
2- 如果发现编译的时候发生问题,一定是没有替换干净makefile内容导致,app编译没有出现,请find .app与源文件比对