Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / MFC

CPianoCtrl - A Display Piano Control

Rate me:
Please Sign up or sign in to vote.
4.80/5 (40 votes)
14 Mar 2008MIT5 min read 209.7K   5K   65  
An article about using the CPianoCtrl class
/*
    The MIT License

    Copyright (c) 2008 Leslie Sanford

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.

    Contact: Leslie Sanford (jabberdabber@hotmail.com)

    Last modified: 03/14/2008
*/


//---------------------------------------------------------------------
// Dependencies
//---------------------------------------------------------------------


#include "stdafx.h"
#include "PianoCtrl.h"


//---------------------------------------------------------------------
// CWhiteKeyFull class implementation
//---------------------------------------------------------------------


// Constructor
CPianoCtrl::CWhiteKeyFull::CWhiteKeyFull(Orientation Orient, 
                                         double UnitLength, 
                                         int Width, double Position)
{
    //
    // Initialize region area depending on orientation
    //

    if(Orient == HORIZONTAL)
    {
        InitHorRgn(UnitLength, Width, Position);
    }
    else if(Orient == VERTICAL_LEFT)
    {
        InitVerLeftRgn(UnitLength, Width, Position);
    }
    else
    {
        InitVerRightRgn(UnitLength, Width, Position);
    }

    // Create the polygon region
    m_Rgn.CreatePolygonRgn(Points, 4, WINDING); 
}


// Hit detection
bool CPianoCtrl::CWhiteKeyFull::IsPointInKey(const CPoint &pt) const
{
    return (m_Rgn.PtInRegion(pt)) ? true : false;
}


// Turns note on
CRgn *CPianoCtrl::CWhiteKeyFull::NoteOn(COLORREF NoteOnColor)
{
    m_NoteOnFlag = true;
    m_NoteOnColor = NoteOnColor;

    return &m_Rgn;
}


// Turns note off
CRgn *CPianoCtrl::CWhiteKeyFull::NoteOff()
{
    m_NoteOnFlag = false;

    return &m_Rgn;
}


// Paints this key
void CPianoCtrl::CWhiteKeyFull::Paint(CDC *dc)
{
    CPianoKey::Paint(dc, &m_Rgn, RGB(255, 255, 255));
}


// Initialize the horizontal white full key's region
void CPianoCtrl::CWhiteKeyFull::InitHorRgn(double UnitLength, 
                                           int Width, double Position)
{
    //   -->+--+
    //      |  |
    //      |  |
    //      +--+

    Points[0].x = static_cast<long>(Position);
    Points[0].y = 0;

    //      +--+
    //      |  |
    //      |  |
    //   -->+--+

    Points[1].x = Points[0].x;
    Points[1].y = Width;

    //      +--+
    //      |  |
    //      |  |
    //      +--+<--

    Points[2].x = 
        static_cast<long>(Position + UnitLength * UNIT_PER_NAT_KEY);
    Points[2].y = Points[1].y;

    //      +--+<--
    //      |  |
    //      |  |
    //      +--+

    Points[3].x = Points[2].x;
    Points[3].y = 0;
}


// Initialize the vertical left white full key's region
void CPianoCtrl::CWhiteKeyFull::InitVerLeftRgn(double UnitLength,
                                               int Width, 
                                               double Position)
{
    //      +-----+
    //      |     |
    //      |     |
    //   -->+-----+
    
    Points[0].x = 0;
    Points[0].y = static_cast<long>(Position);

    //      +-----+
    //      |     |
    //      |     |
    //      +-----+<--
    
    Points[1].x = Width;
    Points[1].y = Points[0].y;

    //      +-----+<--
    //      |     |
    //      |     |
    //      +-----+
    
    Points[2].x = Points[1].x;
    Points[2].y = static_cast<long>
        (Position - UnitLength * UNIT_PER_NAT_KEY);

    //   -->+-----+
    //      |     |
    //      |     |
    //      +-----+
    
    Points[3].x = 0;
    Points[3].y = Points[2].y;
}


// Initialize the vertical right white full key
void CPianoCtrl::CWhiteKeyFull::InitVerRightRgn(double UnitLength,
                                                int Width, 
                                                double Position)
{
    //   -->+-----+
    //      |     |
    //      |     |
    //      +-----+
    
    Points[0].x = 0;
    Points[0].y = static_cast<long>(Position);

    //      +-----+<--
    //      |     |
    //      |     |
    //      +-----+
    
    Points[1].x = Width;
    Points[1].y = Points[0].y;

    //      +-----+
    //      |     |
    //      |     |
    //      +-----+<--
    
    Points[2].x = Points[1].x;
    Points[2].y = static_cast<long>
        (Position + UnitLength * UNIT_PER_NAT_KEY);

    //   -->+-----+
    //      |     |
    //      |     |
    //      +-----+
    
    Points[3].x = 0;
    Points[3].y = Points[2].y;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

Comments and Discussions