home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / cmnctrl2 / spinctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.0 KB  |  167 lines

  1. // SpinCtrl.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "CmnCtrl2.h"
  16. #include "SpinCtrl.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CSpinCtrlPage property page
  25.  
  26. IMPLEMENT_DYNCREATE(CSpinCtrlPage, CPropertyPage)
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #endif
  31.  
  32. CSpinCtrlPage::CSpinCtrlPage() : CPropertyPage(CSpinCtrlPage::IDD)
  33. {
  34.     //{{AFX_DATA_INIT(CSpinCtrlPage)
  35.     m_iAlignment = 0;
  36.     m_bArrowkeys = FALSE;
  37.     m_bAutobuddy = FALSE;
  38.     m_uiRangeFrom = 0;
  39.     m_bNothousands = FALSE;
  40.     m_iOrientation = 0;
  41.     m_bSetbuddyint = FALSE;
  42.     m_uiRangeTo = 100;
  43.     m_bWrap = FALSE;
  44.     //}}AFX_DATA_INIT
  45.     m_psp.dwFlags &= ~PSP_HASHELP;  // Lose the Help button
  46. }
  47.  
  48. CSpinCtrlPage::~CSpinCtrlPage()
  49. {
  50. }
  51.  
  52. void CSpinCtrlPage::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CPropertyPage::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CSpinCtrlPage)
  56.     DDX_CBIndex(pDX, IDC_SPIN_ALIGNMENT, m_iAlignment);
  57.     DDX_Check(pDX, IDC_SPIN_ARROWKEYS, m_bArrowkeys);
  58.     DDX_Check(pDX, IDC_SPIN_AUTOBUDDY, m_bAutobuddy);
  59.     DDX_Text(pDX, IDC_SPIN_FROM, m_uiRangeFrom);
  60.     DDV_MinMaxUInt(pDX, m_uiRangeFrom, 0, 65535);
  61.     DDX_Check(pDX, IDC_SPIN_NOTHOUSANDS, m_bNothousands);
  62.     DDX_CBIndex(pDX, IDC_SPIN_ORIENTATION, m_iOrientation);
  63.     DDX_Check(pDX, IDC_SPIN_SETBUDDYINT, m_bSetbuddyint);
  64.     DDX_Text(pDX, IDC_SPIN_TO, m_uiRangeTo);
  65.     DDV_MinMaxUInt(pDX, m_uiRangeTo, 0, 65535);
  66.     DDX_Check(pDX, IDC_SPIN_WRAP, m_bWrap);
  67.     //}}AFX_DATA_MAP
  68. }
  69.  
  70. BOOL CSpinCtrlPage::OnInitDialog()
  71. {
  72.     CPropertyPage::OnInitDialog();
  73.  
  74.     CWnd* pEdit = GetDlgItem( IDC_SPIN_EDIT );
  75.     pEdit->GetWindowRect( &m_EditRect );
  76.     ScreenToClient( &m_EditRect );
  77.  
  78.     CreateSpinCtrl();
  79.     SetModified(TRUE);  // allow the APPLY button to become active
  80.  
  81.     return TRUE;
  82. }
  83.  
  84. void CSpinCtrlPage::CreateSpinCtrl()
  85. {
  86. DWORD dwStyles=0;
  87.  
  88.     // Build styles mask
  89.     if ( 1 == m_iAlignment )
  90.         dwStyles |= UDS_ALIGNLEFT;      // Control is placed to the left of buddy, if set
  91.                                         // (default = unattached)
  92.     else if ( 2 == m_iAlignment )
  93.         dwStyles |= UDS_ALIGNRIGHT;     // Control is placed to the right of buddy, if set
  94.                                         // (default = unattached)
  95.     if ( m_bArrowkeys )
  96.         dwStyles |= UDS_ARROWKEYS;      // Up/Down arrow keys inc/decrement, if set
  97.  
  98.     if ( m_bAutobuddy )
  99.         dwStyles |= UDS_AUTOBUDDY;      // Previous (in Z-order) edit used as buddy, if set
  100.  
  101.     if ( m_bNothousands )
  102.         dwStyles |= UDS_NOTHOUSANDS;    // No thousands seperator used, if set
  103.  
  104.     if ( 1 == m_iOrientation )
  105.         dwStyles |= UDS_HORZ;           // Control is horizontal, if set (default = vert)
  106.  
  107.     if ( m_bSetbuddyint )
  108.         dwStyles |= UDS_SETBUDDYINT;    // Control updates buddy edit with position, if set
  109.  
  110.     if ( m_bWrap )
  111.         dwStyles |= UDS_WRAP;           // Position wraps when range exceeded, if set
  112.  
  113.     // Get edit control and change Z-order (created controls go at bottom of Z-order)
  114.     CWnd* pEdit = GetDlgItem( IDC_SPIN_EDIT );
  115.     pEdit->SetWindowPos( &wndBottom, m_EditRect.left, m_EditRect.top,
  116.                          m_EditRect.Width(), m_EditRect.Height(), SWP_SHOWWINDOW );
  117.  
  118.     // Create spin (up-down) control
  119.     CWnd* pWnd = GetDlgItem( IDC_SPIN_POS );
  120.     CRect rect;
  121.     pWnd->GetWindowRect( &rect );
  122.     ScreenToClient( &rect );
  123.  
  124.     m_Spin.Create( WS_VISIBLE|WS_CHILD|dwStyles, rect, this, IDC_SPIN );
  125.     m_Spin.SetRange( m_uiRangeFrom, m_uiRangeTo );  // Sends UDM_SETRANGE
  126.  
  127.     // Prime edit control with initial value
  128.     TCHAR buf[32];
  129.     int pos = m_Spin.GetPos();                      // Sends UDM_GETPOS
  130.     wsprintf( buf, _T("%d"), pos );
  131.     pWnd = m_Spin.GetBuddy();                       // Sends UDM_GETBUDDY
  132.     if (pWnd != NULL && HIWORD(pos) != 1)           // Check for error in high word
  133.         pWnd->SetWindowText( buf );
  134. }
  135.  
  136. BEGIN_MESSAGE_MAP(CSpinCtrlPage, CPropertyPage)
  137.     //{{AFX_MSG_MAP(CSpinCtrlPage)
  138.     ON_BN_CLICKED(IDC_SPIN_ARROWKEYS, OnAnyChange)
  139.     ON_BN_CLICKED(IDC_SPIN_AUTOBUDDY, OnAnyChange)
  140.     ON_BN_CLICKED(IDC_SPIN_NOTHOUSANDS, OnAnyChange)
  141.     ON_CBN_SELCHANGE(IDC_SPIN_ORIENTATION, OnAnyChange)
  142.     ON_BN_CLICKED(IDC_SPIN_SETBUDDYINT, OnAnyChange)
  143.     ON_EN_CHANGE(IDC_SPIN_TO, OnAnyChange)
  144.     ON_BN_CLICKED(IDC_SPIN_WRAP, OnAnyChange)
  145.     ON_EN_CHANGE(IDC_SPIN_EDIT, OnAnyChange)
  146.     ON_EN_CHANGE(IDC_SPIN_FROM, OnAnyChange)
  147.     ON_CBN_SELCHANGE(IDC_SPIN_ALIGNMENT, OnAnyChange)
  148.     //}}AFX_MSG_MAP
  149. END_MESSAGE_MAP()
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CSpinCtrlPage message handlers
  153.  
  154. BOOL CSpinCtrlPage::OnApply()
  155. {
  156.     UpdateData();
  157.     m_Spin.DestroyWindow();
  158.  
  159.     CreateSpinCtrl();
  160.     return TRUE;
  161. }
  162.  
  163. void CSpinCtrlPage::OnAnyChange()
  164. {
  165.   SetModified(TRUE);
  166. }
  167.