home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch09 / speed / mydlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-17  |  8.4 KB  |  344 lines

  1. // mydlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "speed.h"
  6. #include "mydlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CMyDlg dialog
  15.  
  16.  
  17. CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CMyDlg::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CMyDlg)
  21.         // NOTE: the ClassWizard will add member initialization here
  22.     //}}AFX_DATA_INIT
  23.     
  24.      //////////////////////
  25.      // MY CODE STARTS HERE
  26.      //////////////////////
  27.  
  28.     // Initialize the data members
  29.     m_ScrollPosition = 50;
  30.     m_ScrollMin = 0;
  31.     m_ScrollMax = 100;
  32.  
  33.      ////////////////////
  34.      // MY CODE ENDS HERE
  35.      ////////////////////
  36.     
  37. }
  38.  
  39. void CMyDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41.     CDialog::DoDataExchange(pDX);
  42.     //{{AFX_DATA_MAP(CMyDlg)
  43.         // NOTE: the ClassWizard will add DDX and DDV calls here
  44.     //}}AFX_DATA_MAP
  45. }
  46.  
  47. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
  48.     //{{AFX_MSG_MAP(CMyDlg)
  49.     ON_WM_VSCROLL()
  50.     ON_BN_CLICKED(IDC_ENABLE, OnEnable)
  51.     ON_BN_CLICKED(IDC_DISABLE, OnDisable)
  52.     ON_BN_CLICKED(IDC_HIDE, OnHide)
  53.     ON_BN_CLICKED(IDC_SHOW, OnShow)
  54.     //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56.  
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CMyDlg message handlers
  60.  
  61. BOOL CMyDlg::OnInitDialog()
  62. {
  63.     CDialog::OnInitDialog();
  64.     
  65.     // TODO: Add extra initialization here
  66.  
  67.  
  68.      //////////////////////
  69.      // MY CODE STARTS HERE
  70.      //////////////////////
  71.  
  72.     // Extract the pointer of the scroll bar.
  73.     CScrollBar* pSB =
  74.           (CScrollBar*) GetDlgItem ( IDC_SCROLL_SPEED);
  75.  
  76.     // Set the range of the scroll bar.
  77.     pSB -> SetScrollRange (m_ScrollMin,
  78.                            m_ScrollMax );
  79.  
  80.     // Set the position of the scroll bar.
  81.     pSB -> SetScrollPos ( m_ScrollPosition );
  82.  
  83.  
  84.     // Declare local variables
  85.      CString LabelContents;
  86.      char sPos[25];
  87.  
  88.     // Convert the current position from an integer to a string.
  89.     itoa ( m_ScrollPosition, sPos, 10 );
  90.  
  91.     // Update the variable of the label.
  92.     LabelContents = (CString) "Speed:" + sPos ;
  93.  
  94.      // Update the caption of the label
  95.      SetDlgItemText(IDC_SPEED_LABEL, LabelContents);
  96.  
  97.  
  98.      ////////////////////
  99.      // MY CODE ENDS HERE
  100.      ////////////////////
  101.  
  102.     
  103.     return TRUE;  // return TRUE  unless you set the focus to a control
  104. }
  105.  
  106. void CMyDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  107. {
  108.     // TODO: Add your message handler code here and/or call default
  109.  
  110.      //////////////////////
  111.      // MY CODE STARTS HERE
  112.      //////////////////////
  113.  
  114.      CString LabelContents;
  115.      char sPos[25];
  116.      int iCurrent;
  117.  
  118.      switch (nSBCode)
  119.           {
  120.           case SB_THUMBPOSITION:
  121.  
  122.                 // Set the scroll bar to its new position.
  123.                 pScrollBar->SetScrollPos ( nPos );
  124.  
  125.  
  126.                 // Update the caption of the label.
  127.                 itoa ( nPos, sPos, 10 );
  128.                 LabelContents = (CString) "Speed:" + sPos ;
  129.  
  130.                 SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  131.                 break;
  132.  
  133.           case SB_LINEDOWN:
  134.  
  135.                 // Get the current position
  136.                 iCurrent = pScrollBar->GetScrollPos();
  137.  
  138.  
  139.                 // Increase current position by 1 (if possible)
  140.                 iCurrent = iCurrent + 1;
  141.  
  142.                 if ( iCurrent >= m_ScrollMax )
  143.                      iCurrent = m_ScrollMax;
  144.  
  145.                 // Set scroll bar to its new position.
  146.                 pScrollBar->SetScrollPos ( iCurrent );
  147.  
  148.  
  149.                 // Update the caption of the label.
  150.                 itoa ( iCurrent, sPos, 10 );
  151.  
  152.                 LabelContents = (CString) "Speed:" + sPos ;
  153.  
  154.                 SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  155.                 break;
  156.  
  157.           case SB_LINEUP:
  158.  
  159.                 // Get the current position
  160.                 iCurrent = pScrollBar->GetScrollPos();
  161.  
  162.  
  163.                 // Decrease current position by 1 (if possible)
  164.                 iCurrent = iCurrent - 1;
  165.  
  166.                 if ( iCurrent <= m_ScrollMin )
  167.                      iCurrent = m_ScrollMin;
  168.  
  169.                 // Set scroll bar to its new position.
  170.                 pScrollBar->SetScrollPos ( iCurrent );
  171.  
  172.  
  173.                 // Update the caption of the label.
  174.                 itoa ( iCurrent, sPos, 10 );
  175.                 LabelContents = (CString) "Speed:" + sPos ;
  176.  
  177.                 SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  178.                 break;
  179.  
  180.           case SB_PAGEDOWN:
  181.  
  182.                 // Get the current position.
  183.                 iCurrent = pScrollBar->GetScrollPos();
  184.  
  185.                 // Increase scroll bar (if possible).
  186.                 iCurrent = iCurrent + 1;
  187.  
  188.                 if ( iCurrent >= m_ScrollMax )
  189.                      iCurrent = m_ScrollMax;
  190.  
  191.  
  192.                 // Set scroll bar to its new position.
  193.                 pScrollBar->SetScrollPos ( iCurrent );
  194.  
  195.  
  196.                 // Update the caption of the label.
  197.                 itoa ( iCurrent, sPos, 10 );
  198.                 LabelContents = (CString) "Speed:" + sPos ;
  199.  
  200.                 SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  201.                 break;
  202.  
  203.           case SB_PAGEUP:
  204.  
  205.                 // Get the current position.
  206.                 iCurrent = pScrollBar->GetScrollPos();
  207.  
  208.                 // Decrease scroll bar (if possible).
  209.                 iCurrent = iCurrent - 1;
  210.  
  211.                 if ( iCurrent <= m_ScrollMin )
  212.                      iCurrent = m_ScrollMin;
  213.  
  214.  
  215.                 // Set scroll bar to its new position.
  216.                 pScrollBar->SetScrollPos ( iCurrent );
  217.  
  218.  
  219.                 // Update the caption of the label.
  220.                 itoa ( iCurrent, sPos, 10 );
  221.                 LabelContents = (CString) "Speed:" + sPos ;
  222.  
  223.                 SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  224.                 break;
  225.  
  226.           case SB_THUMBTRACK:
  227.  
  228.                // Set scroll bar to its new position.
  229.                pScrollBar->SetScrollPos ( nPos );
  230.  
  231.                //  Update the caption of the label.
  232.                itoa ( nPos, sPos, 10 );
  233.                LabelContents = (CString) "Speed:" + sPos ;
  234.                SetDlgItemText(IDC_SPEED_LABEL,LabelContents);
  235.                break;
  236.                 
  237.  
  238.           }
  239.  
  240.  
  241.      ////////////////////
  242.      // MY CODE ENDS HERE
  243.      ////////////////////
  244.  
  245.     
  246.     CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  247. }
  248.  
  249. void CMyDlg::OnOK()
  250. {
  251.     // TODO: Add extra validation here
  252.  
  253.      //////////////////////
  254.      // MY CODE STARTS HERE
  255.      //////////////////////
  256.  
  257.      // Extract the pointer of the scroll bar whose
  258.      // ID is IDC_SCROLL_SPEED
  259.      CScrollBar* pSB =
  260.          (CScrollBar*) GetDlgItem ( IDC_SCROLL_SPEED);
  261.  
  262.      // Extract the thumb position of the scroll bar, and
  263.      // assign it to the m_ScrollPosition data member.
  264.      m_ScrollPosition = pSB -> GetScrollPos ();
  265.  
  266.      ////////////////////
  267.      // MY CODE ENDS HERE
  268.      ////////////////////
  269.  
  270.  
  271.     
  272.     CDialog::OnOK();
  273. }
  274.  
  275. void CMyDlg::OnEnable()
  276. {
  277.     // TODO: Add your control notification handler code here
  278.  
  279.     //////////////////////
  280.     // MY CODE STARTS HERE
  281.     //////////////////////
  282.  
  283.     // Enable the scroll bar
  284.     GetDlgItem(IDC_SCROLL_SPEED)->EnableWindow(TRUE);
  285.  
  286.     ////////////////////
  287.     // MY CODE ENDS HERE
  288.     ////////////////////
  289.     
  290. }
  291.  
  292. void CMyDlg::OnDisable()
  293. {
  294.     // TODO: Add your control notification handler code here
  295.     
  296.      //////////////////////
  297.      // MY CODE STARTS HERE
  298.      //////////////////////
  299.  
  300.      // Disable the scroll bar
  301.      GetDlgItem(IDC_SCROLL_SPEED)->EnableWindow(FALSE);
  302.  
  303.      ////////////////////
  304.      // MY CODE ENDS HERE
  305.      ////////////////////
  306.  
  307.  
  308. }
  309.  
  310. void CMyDlg::OnHide()
  311. {
  312.     // TODO: Add your control notification handler code here
  313.  
  314.      //////////////////////
  315.      // MY CODE STARTS HERE
  316.      //////////////////////
  317.  
  318.      // Hide the scroll bar
  319.      GetDlgItem(IDC_SCROLL_SPEED)->ShowWindow(SW_HIDE);
  320.  
  321.      ////////////////////
  322.      // MY CODE ENDS HERE
  323.      ////////////////////
  324.     
  325. }
  326.  
  327. void CMyDlg::OnShow()
  328. {
  329.     // TODO: Add your control notification handler code here
  330.  
  331.      //////////////////////
  332.      // MY CODE STARTS HERE
  333.      //////////////////////
  334.   
  335.      // Un-hide the scroll bar
  336.      GetDlgItem(IDC_SCROLL_SPEED)->ShowWindow(SW_SHOW);
  337.  
  338.      ////////////////////
  339.      // MY CODE ENDS HERE
  340.      ////////////////////
  341.  
  342.     
  343. }
  344.