[英]Making an application bundle with Xcode, C++ and wxWidgets
我是 macOS 的新手,我正在嘗試弄清楚如何制作一個應用程序包,這樣我的代碼就不僅僅是一個可執行文件。
我正在使用 xcode 12.5 版並使用 c++ 語言和wxWidgets庫編寫測試 gui 應用程序。
現在我嘗試用一個按鈕和一個菜單欄(屏幕頂部的欄有不同的菜單,如 File 或 About )制作一個簡單的 gui。 我遇到了2個問題:
代碼:
應用程序的 cpp 文件(繼承自 wxApp)
bool instaStalkApp::OnInit(){
instaFrame = new instaStalkFrame("InstaStalk", wxPoint(50,50), wxSize(P_WIDTH,P_HEIGHT));
instaFrame->Show(true);
SetTopWindow(instaFrame);
mainPanel = new instaStalkPanel(instaFrame, ID_PANEL_MAIN, wxPoint(-1,-1), wxSize(P_WIDTH, P_HEIGHT), wxTAB_TRAVERSAL, "panel");
testButton = new instaButton(mainPanel, ID_TEST_BUTTON_1, "test1" , wxPoint(P_WIDTH / 2 ,P_HEIGHT / 2), wxSize(100,20), "test1but");
return true;
}
按鈕 class 的 Header 文件:
#ifndef instaButton_hpp
#define instaButton_hpp
#include <stdio.h>
#include <wx/wx.h>
class instaButton : public wxButton {
private:
public:
instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name);
void On_Button1Click(wxCommandEvent &event);
wxDECLARE_EVENT_TABLE();
};
#endif /* instaButton_hpp */
按鈕事件表:
wxBEGIN_EVENT_TABLE(instaButton, wxButton)
EVT_BUTTON(ID_TEST_BUTTON_1, instaButton::On_Button1Click)
wxEND_EVENT_TABLE()
自定義按鈕的cpp文件
#include <wx/wx.h>
#include "instaButton.hpp"
instaButton::instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name) : wxButton(parent, id, label, pos, size, 0, wxDefaultValidator, name){
}
void instaButton::On_Button1Click(wxCommandEvent &event){
wxLogMessage("hi");
}
帶有 menuBar 的框架的 cpp 文件:
#include <wx/wx.h>
#include "instaStalkFrame.hpp"
#include "instaStalkPanel.hpp"
#include "consts.hpp"
instaStalkFrame::instaStalkFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size){
menuFile = new wxMenu;
menuFile->Append(ID_TEST, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar(menuBar);
}
void instaStalkFrame::OnExit(wxCommandEvent& event){
Close(true);
}
void instaStalkFrame::OnAbout(wxCommandEvent& event){
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void instaStalkFrame::OnTest(wxCommandEvent& event){
wxLogMessage("Test!");
}
為了制作應用程序包:
您應該已經在 Xcode 中創建了一個 OSX 應用程序項目。 檢查 wxWiki(我知道它適用於舊版本的 Xcode,但它仍然適用,您可以嘗試在新版本中匹配它)。
如果您想稍后再做 - 您可以嘗試從 wxWidgets 發行版構建minimal
樣本,查看它使用什么來創建捆綁包並在您的測試程序中復制它。 但是你絕對應該在 Xcode 中創建一個應用程序包項目來簡化你的生活。
當您創建一個 Bundle 時,該菜單將開始工作。 正如你應該的那樣。
我認為沒有實現懸停效果。 你可以在 wx-users ML 上詢問它。
如果您需要更多指導,請告訴我們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.