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 / slidctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  8.3 KB  |  305 lines

  1. // SlidCtrl.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 "SlidCtrl.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CSliderCtrlPage property page
  25.  
  26. IMPLEMENT_DYNCREATE(CSliderCtrlPage, CPropertyPage)
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #endif
  31.  
  32. CSliderCtrlPage::CSliderCtrlPage() : CPropertyPage(CSliderCtrlPage::IDD)
  33. {
  34.     //{{AFX_DATA_INIT(CSliderCtrlPage)
  35.     m_bAutoticks = TRUE;
  36.     m_bEnablesel = FALSE;
  37.     m_bFixed = FALSE;
  38.     m_uiLine = 10;
  39.     m_bNothumb = FALSE;
  40.     m_bNoticks = FALSE;
  41.     m_iOrientation = 0;
  42.     m_uiPage = 50;
  43.     m_iTickpos = 0;
  44.     m_uiRangeFrom = 0;
  45.     m_uiRangeTo = 100;
  46.     m_uiSelRangeFrom = 20;
  47.     m_uiSelRangeTo = 80;
  48.     m_uiTickFreq = 20;
  49.     //}}AFX_DATA_INIT
  50.     m_psp.dwFlags &= ~PSP_HASHELP;  // Lose the Help button
  51. }
  52.  
  53. CSliderCtrlPage::~CSliderCtrlPage()
  54. {
  55. }
  56.  
  57. void CSliderCtrlPage::DoDataExchange(CDataExchange* pDX)
  58. {
  59.     CPropertyPage::DoDataExchange(pDX);
  60.     //{{AFX_DATA_MAP(CSliderCtrlPage)
  61.     DDX_Control(pDX, IDC_NOTIFICATIONS, m_Notifications);
  62.     DDX_Check(pDX, IDC_SLIDER_AUTOTICKS, m_bAutoticks);
  63.     DDX_Check(pDX, IDC_SLIDER_ENABLESEL, m_bEnablesel);
  64.     DDX_Check(pDX, IDC_SLIDER_FIXED, m_bFixed);
  65.     DDX_Text(pDX, IDC_SLIDER_LINE, m_uiLine);
  66.     DDV_MinMaxUInt(pDX, m_uiLine, 0, 65535);
  67.     DDX_Check(pDX, IDC_SLIDER_NOTHUMB, m_bNothumb);
  68.     DDX_Check(pDX, IDC_SLIDER_NOTICKS, m_bNoticks);
  69.     DDX_CBIndex(pDX, IDC_SLIDER_ORIENTATION, m_iOrientation);
  70.     DDX_Text(pDX, IDC_SLIDER_PAGE, m_uiPage);
  71.     DDV_MinMaxUInt(pDX, m_uiPage, 0, 65535);
  72.     DDX_CBIndex(pDX, IDC_SLIDER_POINT, m_iTickpos);
  73.     DDX_Text(pDX, IDC_SLIDER_RANGEFROM, m_uiRangeFrom);
  74.     DDV_MinMaxUInt(pDX, m_uiRangeFrom, 0, 65535);
  75.     DDX_Text(pDX, IDC_SLIDER_RANGETO, m_uiRangeTo);
  76.     DDV_MinMaxUInt(pDX, m_uiRangeTo, 0, 65535);
  77.     DDX_Text(pDX, IDC_SLIDER_SELRANGEFROM, m_uiSelRangeFrom);
  78.     DDV_MinMaxUInt(pDX, m_uiSelRangeFrom, 0, 65535);
  79.     DDX_Text(pDX, IDC_SLIDER_SELRANGETO, m_uiSelRangeTo);
  80.     DDV_MinMaxUInt(pDX, m_uiSelRangeTo, 0, 65535);
  81.     DDX_Text(pDX, IDC_SLIDER_TICKFREQ, m_uiTickFreq);
  82.     DDV_MinMaxUInt(pDX, m_uiTickFreq, 0, 65535);
  83.     //}}AFX_DATA_MAP
  84. }
  85.  
  86. BOOL CSliderCtrlPage::OnInitDialog()
  87. {
  88.     CPropertyPage::OnInitDialog();
  89.  
  90.     // Initially create slider control in horizontal position
  91.     CWnd* pWnd =
  92.         GetDlgItem( IDC_SLIDER_HORZPOS );
  93.     CRect rect;
  94.     pWnd->GetWindowRect( &rect );
  95.     ScreenToClient( &rect );
  96.  
  97.     // Initialise controls
  98.     m_Slider.Create( WS_VISIBLE|WS_CHILD|TBS_HORZ|TBS_BOTH|TBS_AUTOTICKS,
  99.                      rect, this, IDC_SLIDER );
  100.     m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ
  101.     m_Slider.SetLineSize( m_uiLine );       // Send TBM_SETLINESIZE
  102.     m_Slider.SetPageSize( m_uiPage );       // Send TBM_SETPAGESIZE
  103.     m_Slider.SetRange( m_uiRangeFrom, m_uiRangeTo, TRUE );
  104.                                             // Send TBM_SETRANGE
  105.     return TRUE;
  106. }
  107.  
  108. void CSliderCtrlPage::ChangeCtrlStyle( long lStyle, BOOL bSetBit)
  109. {
  110.     long    lStyleOld;
  111.     CRect   rect;
  112.  
  113.     lStyleOld = GetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE );
  114.     if ( bSetBit )
  115.         lStyleOld |= lStyle;
  116.     else
  117.         lStyleOld &= ~lStyle;
  118.     SetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE, lStyleOld );
  119.     m_Slider.GetWindowRect(&rect);
  120.     m_Slider.InvalidateRect(&rect);
  121. }
  122.  
  123. BEGIN_MESSAGE_MAP(CSliderCtrlPage, CPropertyPage)
  124.     //{{AFX_MSG_MAP(CSliderCtrlPage)
  125.     ON_BN_CLICKED(IDC_SLIDER_AUTOTICKS, OnAutoticks)
  126.     ON_EN_KILLFOCUS(IDC_SLIDER_TICKFREQ, OnTickfreq)
  127.     ON_BN_CLICKED(IDC_SLIDER_NOTICKS, OnNoticks)
  128.     ON_BN_CLICKED(IDC_SLIDER_NOTHUMB, OnNothumb)
  129.     ON_BN_CLICKED(IDC_SLIDER_FIXED, OnFixed)
  130.     ON_BN_CLICKED(IDC_SLIDER_ENABLESEL, OnEnablesel)
  131.     ON_EN_KILLFOCUS(IDC_SLIDER_LINE, OnLine)
  132.     ON_EN_KILLFOCUS(IDC_SLIDER_PAGE, OnPage)
  133.     ON_EN_KILLFOCUS(IDC_SLIDER_RANGEFROM, OnRangefrom)
  134.     ON_EN_KILLFOCUS(IDC_SLIDER_RANGETO, OnRangeto)
  135.     ON_CBN_SELCHANGE(IDC_SLIDER_ORIENTATION, OnOrientation)
  136.     ON_CBN_SELCHANGE(IDC_SLIDER_POINT, OnPoint)
  137.     ON_EN_KILLFOCUS(IDC_SLIDER_SELRANGEFROM, OnSelrangefrom)
  138.     ON_EN_KILLFOCUS(IDC_SLIDER_SELRANGETO, OnSelrangeto)
  139. //  ON_WM_HSCROLL()
  140.     //}}AFX_MSG_MAP
  141. END_MESSAGE_MAP()
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CSliderCtrlPage message handlers
  145.  
  146. void CSliderCtrlPage::OnAutoticks()
  147. {
  148.     UpdateData();
  149.  
  150.     // Shows tick marks according to increment/frequency when set (default frequency is 1)
  151.     ChangeCtrlStyle( TBS_AUTOTICKS, m_bAutoticks );
  152.  
  153.     // Enable/disable edit controls
  154.     GetDlgItem( IDC_SLIDER_TICKFREQ )->EnableWindow( m_bAutoticks );
  155.  
  156.     if ( m_bAutoticks )
  157.         // Set tick marks with given frequency (1 = tick at every increment)
  158.         m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ - need TBS_AUTOTICKS
  159.     else
  160.         // Clear tick marks
  161.         m_Slider.ClearTics( TRUE );
  162. }
  163.  
  164. void CSliderCtrlPage::OnTickfreq()
  165. {
  166.     UpdateData();
  167.     if ( m_bAutoticks )
  168.         // Set tick marks at given frequency (1 = tick at every increment)
  169.         m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ - need TBS_AUTOTICKS
  170. }
  171.  
  172. void CSliderCtrlPage::OnNoticks()
  173. {
  174.     UpdateData();
  175.  
  176.     // Removes tick marks when set
  177.     ChangeCtrlStyle( TBS_NOTICKS, m_bNoticks );
  178. }
  179.  
  180. void CSliderCtrlPage::OnNothumb()
  181. {
  182.     UpdateData();
  183.  
  184.     // Removes thumb from slider control when set
  185.     ChangeCtrlStyle( TBS_NOTHUMB, m_bNothumb );
  186. }
  187.  
  188. void CSliderCtrlPage::OnFixed()
  189. {
  190.     UpdateData();
  191.  
  192.     // Fixes length of slider thumb when set (
  193.     ChangeCtrlStyle( TBS_FIXEDLENGTH, m_bFixed );
  194. }
  195.  
  196. void CSliderCtrlPage::OnEnablesel()
  197. {
  198.     UpdateData();
  199.     ChangeCtrlStyle( TBS_ENABLESELRANGE, m_bEnablesel );
  200.  
  201.     // Enable/disable edit controls
  202.     GetDlgItem( IDC_SLIDER_SELRANGEFROM )->EnableWindow( m_bEnablesel );
  203.     GetDlgItem( IDC_SLIDER_SELRANGETO )->EnableWindow( m_bEnablesel );
  204.  
  205.     if ( m_bEnablesel )
  206.         // Set selection range
  207.         m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );  // Send TBM_SETSEL
  208.     else
  209.         // Clear selection range
  210.         m_Slider.ClearSel( TRUE );
  211. }
  212.  
  213. void CSliderCtrlPage::OnLine()
  214. {
  215.     UpdateData();
  216.  
  217.     // Set line size (determines effect of arrow keys)
  218.     m_Slider.SetLineSize( m_uiLine );       // Send TBM_SETLINESIZE
  219. }
  220.  
  221. void CSliderCtrlPage::OnPage()
  222. {
  223.     UpdateData();
  224.  
  225.     // Set page size (determines effect of PageUp/PageDown keys)
  226.     m_Slider.SetPageSize( m_uiPage );       // Send TBM_SETPAGESIZE
  227. }
  228.  
  229. void CSliderCtrlPage::OnRangefrom()
  230. {
  231.     UpdateData();
  232.  
  233.     // Set range of slider control
  234.     m_Slider.SetRangeMin( m_uiRangeFrom, TRUE );    // Send TBM_SETRANGE
  235. }
  236.  
  237. void CSliderCtrlPage::OnRangeto()
  238. {
  239.     UpdateData();
  240.  
  241.     // Set range of slider control
  242.     m_Slider.SetRangeMax( m_uiRangeTo, TRUE );  // Send TBM_SETRANGE
  243. }
  244.  
  245. void CSliderCtrlPage::OnOrientation()
  246. {
  247.     UpdateData();
  248.  
  249.     // Obtain horz/vert place marker (static control)
  250.     CWnd* pWnd =
  251.         GetDlgItem( (0==m_iOrientation)?IDC_SLIDER_HORZPOS:IDC_SLIDER_VERTPOS );
  252.     CRect rect;
  253.     pWnd->GetWindowRect( &rect );
  254.     ScreenToClient( &rect );
  255.  
  256.     // Set slider control style
  257.     ChangeCtrlStyle( TBS_HORZ, (0 == m_iOrientation) );
  258.     ChangeCtrlStyle( TBS_VERT, (1 == m_iOrientation) );
  259.  
  260.     // Move control to horz/vert position
  261.     m_Slider.SetWindowPos( NULL,
  262.                            rect.left, rect.top,
  263.                            rect.Width(), rect.Height(),
  264.                            SWP_NOZORDER|SWP_SHOWWINDOW );
  265.     Invalidate();
  266. }
  267.  
  268. void CSliderCtrlPage::OnPoint()
  269. {
  270.     UpdateData();
  271.  
  272.     switch ( m_iTickpos )
  273.     {
  274.     case 0:
  275.         ChangeCtrlStyle( TBS_BOTH );
  276.         break;
  277.     case 1:
  278.         ChangeCtrlStyle( TBS_TOP );     // Same as TBS_LEFT
  279.         ChangeCtrlStyle( TBS_BOTH, FALSE );
  280.         ChangeCtrlStyle( TBS_BOTTOM, FALSE );
  281.         break;
  282.     case 2:
  283.         ChangeCtrlStyle( TBS_BOTTOM );  // Same as TBS_RIGHT
  284.         ChangeCtrlStyle( TBS_BOTH, FALSE );
  285.         ChangeCtrlStyle( TBS_TOP, FALSE );
  286.         break;
  287.     default:
  288.         break;
  289.     }
  290. }
  291.  
  292. void CSliderCtrlPage::OnSelrangefrom()
  293. {
  294.     UpdateData();
  295.     m_Slider.ClearSel();
  296.     m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );  // Send TBM_SETSEL
  297. }
  298.  
  299. void CSliderCtrlPage::OnSelrangeto()
  300. {
  301.     UpdateData();
  302.     m_Slider.ClearSel();
  303.     m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );  // Send TBM_SETSEL
  304. }
  305.