简体   繁体   English

使用 Xcode、C++ 和 wxWidgets 制作应用程序包

[英]Making an application bundle with Xcode, C++ and wxWidgets

I'm new to macos, I'm trying to figure out how to make an application bundle so my code won't be just an executable file.我是 macOS 的新手,我正在尝试弄清楚如何制作一个应用程序包,这样我的代码就不仅仅是一个可执行文件。

I'm working with xcode version 12.5 and writing a test gui application using c++ language and the wxWidgets library.我正在使用 xcode 12.5 版并使用 c++ 语言和wxWidgets库编写测试 gui 应用程序。

Now I tried to make a simple gui with just a button and a menu bar (The bar on top of the screen with different menus like File or About ).现在我尝试用一个按钮和一个菜单栏(屏幕顶部的栏有不同的菜单,如 File 或 About )制作一个简单的 gui。 I encountered 2 issues:我遇到了2个问题:

  1. The first and the major one is that the menubar doesn't respond when the executable starts, it only works when focused on another program and then focuses back to the executable, then the menu is clickable and working.第一个也是主要的一个是可执行文件启动时菜单栏没有响应,它只有在关注另一个程序时才有效,然后再关注可执行文件,然后菜单是可点击的并且可以工作。 I read that this happens because I build the program as an executable and not as an application bundle.我读到这是因为我将程序构建为可执行文件而不是应用程序包。 Although, couldn't figure out how to do so in xcode, and the internet doesn't quite answers how to do so.虽然,无法在 xcode 中弄清楚如何做到这一点,并且互联网并没有完全回答如何做到这一点。
  2. The button doesn't have a click effect when you only touch the mousepad ( you have to click it to make a blue click effect ), you have to click and hold the button to see the button change color.仅触摸鼠标垫时按钮没有点击效果(您必须点击它才能产生蓝色点击效果),您必须单击并按住按钮才能看到按钮改变颜色。

CODE:代码:

cpp file of the app ( inherits from wxApp )应用程序的 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;
}

Header file of the button class:按钮 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 */

event table of the button:按钮事件表:

wxBEGIN_EVENT_TABLE(instaButton, wxButton)
    EVT_BUTTON(ID_TEST_BUTTON_1, instaButton::On_Button1Click)
wxEND_EVENT_TABLE()

cpp file of custom button自定义按钮的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");
}

cpp file of the frame with the menuBar:带有 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!");
}

In order to make an Application Bundle:为了制作应用程序包:

You should have been creating an OSX Application project inside Xcode.您应该已经在 Xcode 中创建了一个 OSX 应用程序项目。 Check wxWiki (I know it is for older version of Xcode, but it still applies and you can try to match it in the newer version).检查 wxWiki(我知道它适用于旧版本的 Xcode,但它仍然适用,您可以尝试在新版本中匹配它)。

If you want to do that later - you can try to build minimal sample from thr wxWidgets distribution, see what it uses in order to create a bundle and replicate that in you test program.如果您想稍后再做 - 您可以尝试从 wxWidgets 发行版构建minimal样本,查看它使用什么来创建捆绑包并在您的测试程序中复制它。 But you absolutely should make an Application Bundle project in Xcode to simplify your life.但是你绝对应该在 Xcode 中创建一个应用程序包项目来简化你的生活。

The menu will start working when you create a Bundle.当您创建一个 Bundle 时,该菜单将开始工作。 As you should.正如你应该的那样。

I don't think hovering effect is implemented.我认为没有实现悬停效果。 You can ask about it on wx-users ML.你可以在 wx-users ML 上询问它。

Let us know if you need any more guidance.如果您需要更多指导,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM