📄 nokiacomposerview.cpp
字号:
// NokiaComposerView.cpp : implementation of the CNokiaComposerView class
//
#include "stdafx.h"
#include "NokiaComposer.h"
#include "NokiaComposerDoc.h"
#include "NokiaComposerView.h"
#include "MelodyWindow.h"
#include "Melody.h"
#include "MIDIWriter.h"
#include "KeySequence.h"
#include <mmsystem.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNokiaComposerView
IMPLEMENT_DYNCREATE(CNokiaComposerView, CFormView)
BEGIN_MESSAGE_MAP(CNokiaComposerView, CFormView)
//{{AFX_MSG_MAP(CNokiaComposerView)
ON_EN_CHANGE(IDC_MELODY, OnChangeMelody)
ON_EN_CHANGE(IDC_TEMPO, OnChangeTempo)
ON_EN_CHANGE(IDC_TITLE, OnChangeTitle)
ON_BN_CLICKED(IDC_PARSE, OnParse)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_WM_CLOSE()
ON_BN_CLICKED(DIC_STOP, OnStop)
ON_BN_CLICKED(IDC_GENERATE_KEY_SEQUENCE, OnGenerateKeySequence)
ON_BN_CLICKED(IDC_GENERATE_NOTES, OnGenerateNotes)
ON_COMMAND(ID_OCTAVE_DOWN, OnOctaveDown)
ON_COMMAND(ID_OCTAVE_UP, OnOctaveUp)
ON_COMMAND(ID_SEMITONE_DOWN, OnSemitoneDown)
ON_COMMAND(ID_SEMITONE_UP, OnSemitoneUp)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_UPDATE_COMMAND_UI(ID_OCTAVE_DOWN, OnUpdateOctaveDown)
ON_UPDATE_COMMAND_UI(ID_OCTAVE_UP, OnUpdateOctaveUp)
ON_UPDATE_COMMAND_UI(ID_SEMITONE_DOWN, OnUpdateSemitoneDown)
ON_UPDATE_COMMAND_UI(ID_SEMITONE_UP, OnUpdateSemitoneUp)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
ON_MESSAGE(MM_MCINOTIFY, OnMCINotify)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNokiaComposerView construction/destruction
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::CNokiaComposerView
// | Description :
// |
// +-------------------------------------------------------------
CNokiaComposerView::CNokiaComposerView()
: CFormView(CNokiaComposerView::IDD),
playingMIDI_(false)
{
//{{AFX_DATA_INIT(CNokiaComposerView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::~CNokiaComposerView
// | Description :
// |
// +-------------------------------------------------------------
CNokiaComposerView::~CNokiaComposerView()
{
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::DoDataExchange
// | Description :
// |
// | pDX :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNokiaComposerView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::PreCreateWindow
// | Description :
// |
// | cs :
// |
// +-------------------------------------------------------------
BOOL CNokiaComposerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::OnInitialUpdate
// | Description :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
ResizeParentToFit(FALSE);
SetMelodyTextFormat();
// event mask
CRichEditCtrl* pEdit;
pEdit = (CRichEditCtrl*) GetDlgItem(IDC_MELODY);
pEdit->SetEventMask(ENM_CHANGE);
pEdit = (CRichEditCtrl* ) GetDlgItem(IDC_KEY_SEQUENCE);
pEdit->SetEventMask(ENM_CHANGE);
}
/////////////////////////////////////////////////////////////////////////////
// CNokiaComposerView printing
BOOL CNokiaComposerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CNokiaComposerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CNokiaComposerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CNokiaComposerView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CNokiaComposerView diagnostics
#ifdef _DEBUG
void CNokiaComposerView::AssertValid() const
{
CFormView::AssertValid();
}
void CNokiaComposerView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CNokiaComposerDoc* CNokiaComposerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNokiaComposerDoc)));
return (CNokiaComposerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNokiaComposerView message handlers
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::OnChangeMelody
// | Description :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnChangeMelody()
{
OnParse();
}
// +-------------------------------------------------------------
// |
// | Funci髇 : CNokiaComposerView::OnChangeTempo
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnChangeTempo()
{
ModifyDoc md( GetDocument() );
Melody& melody = md.GetMelody();
melody.SetTempo( GetDlgItemInt( IDC_TEMPO )) ;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : CNokiaComposerView::OnChangeTitle
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnChangeTitle()
{
ModifyDoc md( GetDocument() );
Melody& melody = md.GetMelody();
CString title;
GetDlgItemText( IDC_TITLE, title );
melody.SetTitle( title );
}
// +-------------------------------------------------------------
// |
// | Funci髇 : CNokiaComposerView::OnUpdate
// | Descripci髇 :
// |
// | pSender :
// | lHint :
// | pHint :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
CNokiaComposerDoc* pDoc = GetDocument();
const Melody &melody = pDoc->GetMelody();
int modified = pDoc->IsModified();
SetDlgItemInt( IDC_TEMPO, melody.GetTempo() );
SetDlgItemText( IDC_TITLE, melody.GetTitle() );
SetDlgItemText( IDC_MELODY, melody.GetString() );
SetDlgItemInt( IDC_NOTE_COUNT, melody.GetNoteCount() );
pDoc->SetModifiedFlag(modified);
}
// +-------------------------------------------------------------
// |
// | Funci髇 : CNokiaComposerView::UpdateDocument
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::UpdateDocument()
{
ModifyDoc md( GetDocument() );
Melody& melody = md.GetMelody();
CString title;
GetDlgItemText( IDC_TITLE, title );
melody.SetTitle( title );
MelodyWindow melodyWindow( (CRichEditCtrl*) GetDlgItem(IDC_MELODY) );
melody.ParseString( melodyWindow );
melody.SetTempo( GetDlgItemInt( IDC_TEMPO )) ;
SetDlgItemInt( IDC_NOTE_COUNT, melody.GetNoteCount() );
}
// +-------------------------------------------------------------
// |
// | Funci髇 : CNokiaComposerView::OnParse
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnParse()
{
ModifyDoc md( GetDocument() );
Melody& melody = md.GetMelody();
MelodyWindow melodyWindow( (CRichEditCtrl*) GetDlgItem( IDC_MELODY ) );
melody.ParseString( melodyWindow );
SetDlgItemInt( IDC_NOTE_COUNT, melody.GetNoteCount() );
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::OnEditPaste
// | Description :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnEditPaste()
{
CWnd* pFocusWnd = GetFocus();
int focusId = pFocusWnd->GetDlgCtrlID();
pFocusWnd->SendMessage(WM_PASTE,0,0);
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::OnPlay
// | Description :
// |
// +-------------------------------------------------------------
void CNokiaComposerView::OnPlay()
{
int modifiedFlag = GetDocument()->IsModified();
UpdateDocument();
GetDocument()->SetModifiedFlag(modifiedFlag);
const Melody& melody = GetDocument()->GetMelody();
CString tempMidiPath;
GetTempPath( 100, tempMidiPath.GetBuffer(100) );
tempMidiPath.ReleaseBuffer();
tempMidiPath += "tmp.mid";
{
std::ofstream midiFile(tempMidiPath, std::ios::binary);
MIDIwriter writer( melody, midiFile );
writer.WriteMIDIFile();
}
playingMIDI_ = true;
PlayMIDIFile(tempMidiPath );
}
// +-------------------------------------------------------------
// |
// | Function : CNokiaComposerView::PlayMIDIFile
// | Description :
// |
// | hWndNotify :
// | lpszMIDIFileName :
// |
// +-------------------------------------------------------------
DWORD CNokiaComposerView::PlayMIDIFile(LPCSTR lpszMIDIFileName)
{
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_PLAY_PARMS mciPlayParms;
MCI_STATUS_PARMS mciStatusParms;
// Open the device by specifying the device and filename.
// MCI will attempt to choose the MIDI mapper as the output port.
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.lpstrElementName = lpszMIDIFileName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -