首页 > 学院 > 开发设计 > 正文

2.JUCE——基本程序框架

2019-11-11 05:22:13
字体:
来源:转载
供稿:网友

一:JUCE程序框架

1.1 使用 PRojucer 新建 JUCE 项目

1.2 新建的项目包含三个文件:

MainComponent.h: 主 Component 头文件;

MainComponent.cpp:主Component 源文件;

Main.cppapplication应用和窗口文件。

这里需要明白三个概念: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_INCLUDED

MainWindow.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)以后程序基于此框架。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表