ChoiceWindowBtn.cpp
上传用户:jnkyhqw
上传日期:2007-01-01
资源大小:25k
文件大小:3k
源码类别:

按钮控件

开发平台:

Visual C++

  1. // ChoiceWindowBtn.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DropDownBoxDlg.h"
  5. #include "ChoiceWindowBtn.h"
  6. #include "ChoiceWindow.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CChoiceWindowBtn
  14. #ifdef _ITCDLL
  15. IMPLEMENT_DYNCREATE(CChoiceWindowBtn, ITCImageButton)
  16. #else
  17. IMPLEMENT_DYNCREATE(CChoiceWindowBtn, CButton)
  18. #endif
  19. CChoiceWindowBtn::CChoiceWindowBtn()
  20. {
  21. m_pChoiceWindow = new CChoiceWindow;
  22. m_bDisableIfEmpty = TRUE;
  23. }
  24. CChoiceWindowBtn::~CChoiceWindowBtn()
  25. {
  26. Reset(); // Clean up
  27. delete m_pChoiceWindow;
  28. }
  29. #ifdef _ITCDLL
  30. BEGIN_MESSAGE_MAP(CChoiceWindowBtn, ITCImageButton)
  31. #else
  32. BEGIN_MESSAGE_MAP(CChoiceWindowBtn, CButton)
  33. #endif
  34. //{{AFX_MSG_MAP(CChoiceWindowBtn)
  35. ON_WM_CREATE()
  36. //}}AFX_MSG_MAP
  37. ON_CONTROL_REFLECT(BN_CLICKED, OnButtonClicked)
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CChoiceWindowBtn message handlers
  41. void CChoiceWindowBtn::OnButtonClicked()
  42. {
  43. CRect rcButton;
  44. // Size of button window
  45. GetWindowRect(&rcButton);
  46. // Create choice list window
  47. m_pChoiceWindow->m_pParentBtn = this;
  48. // Give it a dummy height and width, so it is possible to calculate the width of the frame
  49. if (!::IsWindow(*m_pChoiceWindow))
  50. {
  51. VERIFY(m_pChoiceWindow->CreateEx(0, AfxRegisterWndClass(0), "", WS_POPUP|WS_DLGFRAME, 
  52. 10000, 10000, 10000, 10000, *this, (HMENU)0));
  53. }
  54. m_pChoiceWindow->UpdateSizeAndPosition();
  55. m_pChoiceWindow->ShowWindow(SW_SHOW);
  56. }
  57. void CChoiceWindowBtn::AddChoice(LPCTSTR szText, BOOL bChecked)
  58. {
  59. // Insert new text/check pair
  60. CChoice* pChoice = new CChoice;
  61. pChoice->bChecked = bChecked;
  62. pChoice->strText = szText;
  63. m_choiceArray.Add(pChoice);
  64. UpdateState();
  65. }
  66. void CChoiceWindowBtn::Reset()
  67. {
  68. // Delete all choices
  69. while (GetItemCount())
  70. {
  71. RemoveAt(0);
  72. }
  73. }
  74. BOOL CChoiceWindowBtn::GetCheck(int iItem)
  75. {
  76. // Is the item with index iItem checked?
  77. ASSERT(iItem>0 && iItem<m_choiceArray.GetSize());
  78. CChoice* pChoice = m_choiceArray[iItem];
  79. return (pChoice->bChecked);
  80. }
  81. void CChoiceWindowBtn::SetCheck(int nItem, BOOL bCheck)
  82. {
  83. // Checks or unchecks the item at the specified index (nItem)
  84. ASSERT(nItem>=0 && nItem<m_choiceArray.GetSize());
  85. m_choiceArray[nItem]->bChecked = bCheck;
  86. }
  87. void CChoiceWindowBtn::RemoveAt(int iIndex)
  88. {
  89. // Remove an tem at the selected index. Make sure to deallocate its memory too
  90. ASSERT(iIndex>=0 && iIndex<m_choiceArray.GetSize());
  91. delete m_choiceArray[iIndex];
  92. m_choiceArray.RemoveAt(iIndex);
  93. UpdateState();
  94. }
  95. int CChoiceWindowBtn::GetItemCount()
  96. {
  97. // Return the number of itemsin the list
  98. return m_choiceArray.GetSize();
  99. }
  100. CListCtrl& CChoiceWindowBtn::GetListCtrl()
  101. {
  102. // Return a reference to the CListCtrl
  103. return m_pChoiceWindow->m_wndList;
  104. }
  105. void CChoiceWindowBtn::UpdateState()
  106. {
  107. // Make sure that the button is disabled if it does not have any items in it
  108. if (m_bDisableIfEmpty && ::IsWindow(*this))
  109. EnableWindow(GetItemCount());
  110. }
  111. int CChoiceWindowBtn::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  112. {
  113. #ifdef _ITCDLL
  114. if (ITCImageButton::OnCreate(lpCreateStruct) == -1)
  115. return -1;
  116. #else
  117. if (CButton::OnCreate(lpCreateStruct) == -1)
  118. return -1;
  119. #endif
  120. // Initially disable window
  121. UpdateState();
  122. return 0;
  123. }