Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CBTAngleWnd: A cool rotation angle custom control with full source code!

0.00/5 (No votes)
27 Oct 2003 1  
It implements the angle custom control, a circle with a line indicating a rotation angle. Give your application a professional look now!

Sample Image - testangle.gif

Introduction

This article shows how to implements the angle custom control, a circle with a line indicating a rotation angle. This control is a CWnd based control, it includes three classes CBTAngleWnd, CBTComboBox, CBTEditBox. We can use this control to attach with another CWnd based control, such as we can attach to staticbox, button, editbox, combobox etc. The core class is CBTAngleWnd. All source code is included within the zip files.

How to use it

Step1:First off, to use the CBTAngleWnd class, you will need to include the following files in your project:

  • BTAngleWnd.h
  • BTAngleWnd.cpp
  • BTComboBox.h
  • BTComboBox.cpp
  • BTEditBox.h
  • BTEditBox.cpp

Step2: Once you have the class at your disposal, using it is simple. Open dialog resource editor, drag a new picture control from the control toolbar panel to the dialog, and change it's ID to IDC_ANGLE1. Then you should check the Notify property value. As the blow shows:

Step3: Add two members to your dialog class like this:

class CTestAngleDlg : public CDialog
{
// Construction

public:
    CTestAngleDlg(CWnd* pParent = NULL);    // standard constructor


// Dialog Data

    //{{AFX_DATA(CTestAngleDlg)

    enum { IDD = IDD_TESTANGLE_DIALOG };
    CFOHyperLink    m_Home;
    //}}AFX_DATA

    CStatic    m_Static;
    CBTAngleWnd m_AngleCtrl1;

Step4: Now add the following bolded code below to your OnInitDialog() function:

BOOL CTestAngleDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.


    // IDM_ABOUTBOX must be in the system command range.

    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically

    //  when the application's main window is not a dialog

    SetIcon(m_hIcon, TRUE);            // Set big icon

    SetIcon(m_hIcon, FALSE);        // Set small icon


    m_Home.SetColors(RGB(0,0,255), RGB(0,0,255), RGB(51,102,153) );
    m_Home.SetURL
      (_T("http://www.ucancode.net/Products/free%20product/colorpicker.htm"));


    /////////////////////////////////////////////////

    m_AngleCtrl1.SubclassDlgItem(IDC_ANGLE1, this);
    m_Static.SubclassDlgItem(IDC_STATIC_ANGLE, this);

    m_AngleCtrl1.AttachToEditBox(&m_Static);

    m_AngleCtrl1.TurnOnNow(TRUE);
    m_Static.SetWindowText("225");
    m_AngleCtrl1.SetAngleValue(225, TRUE);

    return TRUE;  // return TRUE  unless you set the focus to a control

}

That's all you need to do. If you want to attach the angle control to an edit box or a combo box, you should use the following code instead:

CBTComboBox m_AngleCombo;
CBTEditBox m_AngleEdit; 
CBTAngleWnd m_AngleCtrl2;
CBTAngleWnd m_AngleCtrl3;
// TODO: Add extra initialization here

m_AngleCtrl3.SubclassDlgItem(IDC_ANGLE3, this);
m_AngleCombo.SubclassDlgItem(IDC_COMBO_ANGLE, this);

m_AngleCtrl3.AttachToEditBox(&m_AngleCombo);
m_AngleCombo.AttachToAngleBox(&m_AngleCtrl3);

m_AngleCtrl3.TurnOnNow(TRUE);
m_AngleCombo.SetWindowText("45");
m_AngleCtrl3.SetAngleValue(45, TRUE);

/////////////////////////////////////////////////

m_AngleCtrl2.SubclassDlgItem(IDC_ANGLE2, this);
m_AngleEdit.SubclassDlgItem(IDC_EDIT_ANGLE, this);

m_AngleCtrl2.AttachToEditBox(&m_AngleEdit);
m_AngleEdit.AttachToAngleBox(&m_AngleCtrl2);

m_AngleCtrl2.TurnOnNow(TRUE);
m_AngleEdit.SetWindowText("90");
m_AngleCtrl2.SetAngleValue(90, TRUE);

m_AngleCtrl2.SetBackColor(::GetSysColor(COLOR_ACTIVECAPTION));

That's all. If you have any good ideas about this control, feel free to let me know.

Click here to view my profile and report bugs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here