home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CTRLTS.PAK / BBUTTON.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.5 KB  |  221 lines

  1. // bbutton.cpp : bitmap button test
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "ctrltest.h"
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // BitmapButton Test dialog #1
  18.  
  19. // In this example we pass the bitmap resource names to LoadBitmaps
  20. //  OnInitDialog is used to Subclass the buttons so the dialog
  21. //  controls get attached to the MFC WndProc for C++ message map dispatch.
  22.  
  23. class CBMTest1Dlg : public CDialog
  24. {
  25. protected:
  26.     CBitmapButton button1, button2;
  27. public:
  28.     //{{AFX_DATA(CBMTest1Dlg)
  29.         enum { IDD = IDM_TEST_BITMAP_BUTTON1 };
  30.     //}}AFX_DATA
  31.     CBMTest1Dlg();
  32.  
  33.     BOOL OnInitDialog();
  34.     //{{AFX_MSG(CBMTest1Dlg)
  35.     //}}AFX_MSG
  36.     DECLARE_MESSAGE_MAP()
  37. };
  38.  
  39. BEGIN_MESSAGE_MAP(CBMTest1Dlg, CDialog)
  40.     //{{AFX_MSG_MAP(CBMTest1Dlg)
  41.     //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43.  
  44.  
  45. CBMTest1Dlg::CBMTest1Dlg()
  46.     : CDialog(CBMTest1Dlg::IDD)
  47. {
  48.     // NOTE: The obsolete MFC V1 CBitmapButton constructor with 3 arguments is
  49.     //  replaced by a call to LoadBitmaps.
  50.     if (!button1.LoadBitmaps(_T("Image1Up"), _T("Image1Down"), _T("Image1Focus")) ||
  51.         !button2.LoadBitmaps(_T("Image2Up"), _T("Image2Down"), _T("Image2Focus")))
  52.     {
  53.         TRACE0("Failed to load bitmaps for buttons\n");
  54.         AfxThrowResourceException();
  55.     }
  56. }
  57.  
  58. BOOL CBMTest1Dlg::OnInitDialog()
  59. {
  60.     // each dialog control has special bitmaps
  61.     VERIFY(button1.SubclassDlgItem(IDOK, this));
  62.     button1.SizeToContent();
  63.     VERIFY(button2.SubclassDlgItem(IDCANCEL, this));
  64.     button2.SizeToContent();
  65.  
  66.     return TRUE;
  67. }
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // BitmapButton Test dialog #2
  71.  
  72. // In this example we use the CBitmapButton AutoLoad member function.
  73. //  Autoload uses the text/title of the button as the base resource name.
  74. //  For this trivial example the buttons are called "OK" and "CANCEL",
  75. //  which use the bitmaps "OKU", "OKD", "OKF", "CANCELU", "CANCELD"
  76. //  and "CANCELF" respectively for the up, down and focused images.
  77.  
  78. #define ID_BUTTON_MIN       IDOK
  79. #define N_BUTTONS   (IDCANCEL - ID_BUTTON_MIN + 1)
  80.  
  81. class CBMTest2Dlg : public CDialog
  82. {
  83. protected:
  84.     // array of buttons constructed with no attached bitmap images
  85.     CBitmapButton buttons[N_BUTTONS];
  86. public:
  87.     //{{AFX_DATA(CBMTest2Dlg)
  88.         enum { IDD = IDM_TEST_BITMAP_BUTTON2 };
  89.     //}}AFX_DATA
  90.     CBMTest2Dlg()
  91.         : CDialog(CBMTest2Dlg::IDD)
  92.         { }
  93.  
  94.     BOOL OnInitDialog();
  95.     //{{AFX_MSG(CBMTest2Dlg)
  96.     //}}AFX_MSG
  97.     DECLARE_MESSAGE_MAP()
  98. };
  99.  
  100. BEGIN_MESSAGE_MAP(CBMTest2Dlg, CDialog)
  101.     //{{AFX_MSG_MAP(CBMTest2Dlg)
  102.     //}}AFX_MSG_MAP
  103. END_MESSAGE_MAP()
  104.  
  105.  
  106. BOOL CBMTest2Dlg::OnInitDialog()
  107. {
  108.     // load bitmaps for all the bitmap buttons (does SubclassButton as well)
  109.     for (int i = 0; i < N_BUTTONS; i++)
  110.         VERIFY(buttons[i].AutoLoad(ID_BUTTON_MIN + i, this));
  111.     return TRUE;
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // BitmapButton Test dialog #3
  116.  
  117. // This is an extension of test dialog 2 using AutoLoad using the disabled
  118. //   state with the "X" suffix.
  119. // Here we use bitmap buttons to select a number between 1 and 10.
  120. // The "PREV" and "NEXT" buttons change the number.  These buttons are
  121. //  disabled when the number hits the limits.
  122.  
  123. class CBMTest3Dlg : public CDialog
  124. {
  125. protected:
  126.     // construct
  127.     CBitmapButton okButton;
  128.     CBitmapButton prevButton;
  129.     CBitmapButton nextButton;
  130.  
  131. public:
  132.     int m_nNumber;
  133.     //{{AFX_DATA(CBMTest3Dlg)
  134.         enum { IDD = IDM_TEST_BITMAP_BUTTON3 };
  135.     //}}AFX_DATA
  136.  
  137.     CBMTest3Dlg()
  138.         : CDialog(CBMTest3Dlg::IDD)
  139.         { }
  140.  
  141.     BOOL OnInitDialog();
  142.     void Update();
  143.  
  144.     //{{AFX_MSG(CBMTest3Dlg)
  145.     afx_msg void OnNextNumber();
  146.     afx_msg void OnPrevNumber();
  147.     //}}AFX_MSG
  148.     DECLARE_MESSAGE_MAP()
  149. };
  150.  
  151. BOOL CBMTest3Dlg::OnInitDialog()
  152. {
  153.     // load bitmaps for all the bitmap buttons (does SubclassButton as well)
  154.     VERIFY(okButton.AutoLoad(IDOK, this));
  155.     VERIFY(prevButton.AutoLoad(ID_PREV, this));
  156.     VERIFY(nextButton.AutoLoad(ID_NEXT, this));
  157.     Update();
  158.     return TRUE;
  159. }
  160.  
  161. BEGIN_MESSAGE_MAP(CBMTest3Dlg, CDialog)
  162.     //{{AFX_MSG_MAP(CBMTest3Dlg)
  163.     ON_COMMAND(ID_PREV, OnPrevNumber)
  164.     ON_COMMAND(ID_NEXT, OnNextNumber)
  165.     //}}AFX_MSG_MAP
  166. END_MESSAGE_MAP()
  167.  
  168. void CBMTest3Dlg::OnPrevNumber()
  169. {
  170.     m_nNumber--;
  171.     Update();
  172. }
  173.  
  174. void CBMTest3Dlg::OnNextNumber()
  175. {
  176.     m_nNumber++;
  177.     Update();
  178. }
  179.  
  180. void CBMTest3Dlg::Update()
  181. {
  182.     SetDlgItemInt(IDC_NUMBEROUT, m_nNumber);
  183.     prevButton.EnableWindow(m_nNumber > 1);
  184.     nextButton.EnableWindow(m_nNumber < 10);
  185.     // move focus to active button
  186.     if (!prevButton.IsWindowEnabled())
  187.         nextButton.SetFocus();
  188.     else if (!nextButton.IsWindowEnabled())
  189.         prevButton.SetFocus();
  190. }
  191.  
  192. /////////////////////////////////////////////////////////////////////////////
  193. // Test driver routines
  194.  
  195. void CTestWindow::OnTestBitmapButton1()
  196. {
  197.     CBMTest1Dlg dlg;
  198.     dlg.DoModal();
  199. }
  200.  
  201. void CTestWindow::OnTestBitmapButton2()
  202. {
  203.     CBMTest2Dlg dlg;
  204.     dlg.DoModal();
  205. }
  206.  
  207. void CTestWindow::OnTestBitmapButton3()
  208. {
  209.     CBMTest3Dlg dlg;
  210.     dlg.m_nNumber = 5;
  211.     dlg.DoModal();
  212.  
  213.     CString strYouChose;
  214.     strYouChose.LoadString(IDS_YOU_CHOSE);
  215.     CString strMsg;
  216.     strMsg.Format(strYouChose, dlg.m_nNumber);
  217.     AfxMessageBox(strMsg);
  218. }
  219.  
  220. /////////////////////////////////////////////////////////////////////////////
  221.