一:JUCE程序框架
1.1 使用 PRojucer 新建 JUCE 項目
1.2 新建的項目包含三個文件:
MainComponent.h: 主 Component 頭文件;
MainComponent.cpp:主Component 源文件;
Main.cpp :application應用和窗口文件。
這里需要明白三個概念:Application , Windows 和 Component:
Appilication :應用程序實例,一般一個程序只有一個,繼承 JUCEApplication ;Windows :窗口 ,一個應用程序可以有多個窗口,一般繼承 DocumentWindow ;Component :組件,及各種控件,一般Windows無法自主顯示,常常為其設置一個主Componet,之后其他的子Component加入此主Component 。二:修改程序框架
原始的JUCE程序將Window作為Application的內部類定義,為了更清晰劃分出各部分,對生成的JUCE GUI Project進行改寫。改寫為:MainWindow.h,MainWindow.cpp和Main.cpp。
MainWindow.h:
/* ============================================================================== MainWindow.h Created: 6 Feb 2017 2:48:03pm Author: magewell ==============================================================================*/#ifndef MAINWINDOW_H_INCLUDED#define MAINWINDOW_H_INCLUDED#include "../JuceLibraryCode/JuceHeader.h"class MainContentComponent;//==============================================================================//This class implements the desktop window that contains an instance of our MainContentComponent class.class MainWindow : public DocumentWindow{public: MainWindow(String name); ~MainWindow(); //==================================override void closeButtonPressed() override;private: ScopedPointer<MainContentComponent> contentComponent; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainWindow)};#endif // MAINWINDOW_H_INCLUDEDMainWindow.cpp:
/* ============================================================================== MainWindow.cpp Created: 6 Feb 2017 2:48:03pm Author: magewell ==============================================================================*/#include "MainWindow.h"/********************************************************************* ContentComponent Class********************************************************************/class MainContentComponent : public Component{public: MainContentComponent() { } ~MainContentComponent() { } //=================================================override void paint(Graphics &g) override { g.fillAll(Colour(0xff001F36)); //g.setFont(Font(16.0f)); //g.setColour(Colours::white); //g.drawText("Hello World!", getLocalBounds(), Justification::centred, true); } void resized() override { //Rectangle<int> area(getLocalBounds()); }private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainContentComponent);};/************************************************************ Main Window Class*************************************************************///Construct/DeConstructMainWindow::MainWindow(String name) : DocumentWindow(name, Colours::lightgrey, DocumentWindow::allButtons){ setUsingNativeTitleBar(true); //resizable contentComponent = new MainContentComponent(); setContentOwned(contentComponent, false); setResizable(true, false); setResizeLimits(600, 600, 10000, 10000); centreWithSize(getWidth(), getHeight()); setVisible(true);}MainWindow::~MainWindow(){ contentComponent = nullptr;}void MainWindow::closeButtonPressed(){ JUCEApplication::getInstance()->systemRequestedQuit();}Main.cpp:/* ============================================================================== This file was auto-generated! It contains the basic startup code for a Juce application. ==============================================================================*/#include "MainWindow.h"//==============================================================================class ArchtureApplication : public JUCEApplication{public: //============================================================================== ArchtureApplication() {} const String getApplicationName() override { return ProjectInfo::projectName; } const String getApplicationVersion() override { return ProjectInfo::versionString; } bool moreThanOneInstanceAllowed() override { return true; } //============================================================================== void initialise (const String& commandLine) override { // This method is where you should put your application's initialisation code.. mainWindow = new MainWindow (getApplicationName()); } void shutdown() override { // Add your application's shutdown code here.. mainWindow = nullptr; // (deletes our window) } //============================================================================== void systemRequestedQuit() override { // This is called when the app is being asked to quit: you can ignore this // request and let the app carry on running, or call quit() to allow the app to close. quit(); } void anotherInstanceStarted (const String& commandLine) override { // When another instance of the app is launched while this one is running, // this method is invoked, and the commandLine parameter tells you what // the other instance's command-line arguments were. }private: ScopedPointer<MainWindow> mainWindow;};//==============================================================================// This macro generates the main() routine that launches the app.START_JUCE_APPLICATION (ArchtureApplication)以后程序基于此框架。
新聞熱點
疑難解答