Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
5.3 KiB

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