CustomControls.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:12k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // CustomControls.cpp
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "CustomControls.h"
  22. IMPLEMENT_XTP_CONTROL(CControlButtonLine, CXTPControlButton)
  23. CControlButtonLine::CControlButtonLine(double dWidth)
  24. {
  25. m_dWidth = dWidth;
  26. }
  27. void CControlButtonLine::Copy(CXTPControl* pControl, BOOL bRecursive)
  28. {
  29. ASSERT(DYNAMIC_DOWNCAST(CControlButtonLine, pControl));
  30. CXTPControlButton::Copy(pControl, bRecursive);
  31. m_dWidth = ((CControlButtonLine*)pControl)->m_dWidth;
  32. }
  33. void CControlButtonLine::DoPropExchange(CXTPPropExchange* pPX)
  34. {
  35. CXTPControlButton::DoPropExchange(pPX);
  36. PX_Double(pPX, _T("Width"), m_dWidth);
  37. }
  38. CSize CControlButtonLine::GetSize(CDC* pDC)
  39. {
  40. CSize sz = pDC->GetTextExtent(_T(" "));
  41. return CSize(160, max(sz.cy, 16));
  42. }
  43. void CControlButtonLine::Draw(CDC* pDC)
  44. {
  45. XTPPaintManager()->DrawControlEntry(pDC, this);
  46. CRect rc = m_rcControl;
  47. int nWidth = (int)m_dWidth + 1;
  48. CString str;
  49. str.Format(_T("%2.2f"), m_dWidth);
  50. CRect rcText(rc); rcText.DeflateRect(2, 2);
  51. pDC->DrawText(str, rcText, DT_SINGLELINE|DT_VCENTER );
  52. CSize sz = pDC->GetTextExtent(str);
  53. CRect rcButton = CRect(rc.left + sz.cx + 8, rc.CenterPoint().y - nWidth/2, rc.right - 3, rc.CenterPoint().y - nWidth/2 + nWidth);
  54. XTPPaintManager()->Rectangle(pDC, rcButton, COLOR_BTNTEXT, COLOR_BTNTEXT);
  55. }
  56. ////////////////////////////////////////////////////////////////////////
  57. IMPLEMENT_XTP_CONTROL(CControlButtonStyle, CXTPControlButton)
  58. CControlButtonStyle::CControlButtonStyle(int nStyle)
  59. {
  60. m_nStyle = nStyle;
  61. }
  62. void CControlButtonStyle::Copy(CXTPControl* pControl, BOOL bRecursive)
  63. {
  64. ASSERT(DYNAMIC_DOWNCAST(CControlButtonStyle, pControl));
  65. CXTPControlButton::Copy(pControl, bRecursive);
  66. m_nStyle = ((CControlButtonStyle*)pControl)->m_nStyle;
  67. }
  68. void CControlButtonStyle::DoPropExchange(CXTPPropExchange* pPX)
  69. {
  70. CXTPControlButton::DoPropExchange(pPX);
  71. PX_Int(pPX, _T("Style"), m_nStyle);
  72. }
  73. CSize CControlButtonStyle::GetSize(CDC* /*pDC*/)
  74. {
  75. return CSize(110, 16);
  76. }
  77. void CControlButtonStyle::Draw(CDC* pDC)
  78. {
  79. XTPPaintManager()->DrawControlEntry(pDC, this);
  80. CRect rc = m_rcControl;
  81. int nWidth = (int)3;
  82. CRect rcButton = CRect(rc.left + 3, rc.CenterPoint().y - nWidth/2, rc.right - 3, rc.CenterPoint().y - nWidth/2 + nWidth);
  83. int nLine1 = 5; int nPause = 0; int nLine2 = 0;
  84. switch(m_nStyle)
  85. {
  86. case 1: nLine1 = nLine2 = nPause = 3; break;
  87. case 2: nLine1 = nLine2 = 10; nPause = 5; break;
  88. case 3: nLine1 = 10; nLine2 = nPause = 5; break;
  89. case 4: nLine1 = nLine2 = 20; nPause = 5; break;
  90. case 5: nLine1 = 20; nLine2 = nPause = 5; break;
  91. }
  92. int nRight = rcButton.left;
  93. while (nRight < rcButton.right)
  94. {
  95. CRect rcLine1(rcButton);
  96. rcLine1.left = nRight; rcLine1.right = rcLine1.left + nLine1;
  97. if (!rcLine1.IntersectRect(rcLine1, rcButton))
  98. break;
  99. XTPPaintManager()->Rectangle(pDC, rcLine1, COLOR_BTNTEXT, COLOR_BTNTEXT);
  100. CRect rcLine2(rcButton);
  101. rcLine2.left = nPause + rcLine1.right;
  102. rcLine2.right = rcLine2.left + nLine2;
  103. if (!rcLine2.IsRectEmpty())
  104. {
  105. if (!rcLine2.IntersectRect(rcLine2, rcButton))
  106. break;
  107. }
  108. XTPPaintManager()->Rectangle(pDC, rcLine2, COLOR_BTNTEXT, COLOR_BTNTEXT);
  109. nRight = rcLine2.right + nPause;
  110. }
  111. }
  112. ////////////////////////////////////////////////////////////////////////
  113. IMPLEMENT_XTP_CONTROL(CControlButtonArrow, CXTPControlButton)
  114. CControlButtonArrow::CControlButtonArrow(int nStyle)
  115. {
  116. m_nStyle = nStyle;
  117. }
  118. void CControlButtonArrow::Copy(CXTPControl* pControl, BOOL bRecursive)
  119. {
  120. ASSERT(DYNAMIC_DOWNCAST(CControlButtonArrow, pControl));
  121. CXTPControlButton::Copy(pControl, bRecursive);
  122. m_nStyle = ((CControlButtonArrow*)pControl)->m_nStyle;
  123. }
  124. void CControlButtonArrow::DoPropExchange(CXTPPropExchange* pPX)
  125. {
  126. CXTPControlButton::DoPropExchange(pPX);
  127. PX_Int(pPX, _T("Style"), m_nStyle);
  128. }
  129. CSize CControlButtonArrow::GetSize(CDC* pDC)
  130. {
  131. if (m_nStyle == 0)
  132. {
  133. CSize sz = CXTPControlButton::GetSize(pDC);
  134. sz.cx = 120;
  135. return sz;
  136. }
  137. return CSize(120, 18);
  138. }
  139. void CControlButtonArrow::Draw(CDC* pDC)
  140. {
  141. XTPPaintManager()->DrawControlEntry(pDC, this);
  142. if (m_nStyle == 0 )
  143. {
  144. pDC->DrawText(GetCaption(), m_rcControl, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
  145. return;
  146. }
  147. CRect rc = m_rcControl;
  148. CRect rcButton = CRect(rc.left + 3, rc.CenterPoint().y, rc.right - 3, rc.CenterPoint().y);
  149. XTPPaintManager()->Line(pDC, rcButton.left, rcButton.top, rcButton.right, rcButton.top, COLOR_BTNTEXT);
  150. if (m_nStyle == 2 || m_nStyle == 4)
  151. {
  152. XTPPaintManager()->Line(pDC, rcButton.left, rcButton.top, rcButton.left + 8, rcButton.top - 4, COLOR_BTNTEXT);
  153. XTPPaintManager()->Line(pDC, rcButton.left, rcButton.top, rcButton.left + 8, rcButton.top + 4, COLOR_BTNTEXT);
  154. }
  155. if (m_nStyle == 3 || m_nStyle == 4)
  156. {
  157. XTPPaintManager()->Line(pDC, rcButton.right - 8, rcButton.top - 4, rcButton.right, rcButton.top, COLOR_BTNTEXT);
  158. XTPPaintManager()->Line(pDC, rcButton.right - 8, rcButton.top + 4, rcButton.right, rcButton.top, COLOR_BTNTEXT);
  159. }
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. //
  163. BEGIN_MESSAGE_MAP(CControlComboBoxEditEx, CXTMaskEditT<CXTPControlComboBoxEditCtrl>)
  164. ON_MASKEDIT_REFLECT()
  165. ON_WM_CREATE()
  166. END_MESSAGE_MAP()
  167. int CControlComboBoxEditEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
  168. {
  169. SetEditMask(_T("00:00:00:00"), _T("__:__:__:__"), _T("00:00:00:00"));
  170. return CXTMaskEditT<CXTPControlComboBoxEditCtrl>::OnCreate(lpCreateStruct);
  171. }
  172. IMPLEMENT_XTP_CONTROL(CControlComboBoxEx, CXTPControlComboBox)
  173. //////////////////////////////////////////////////////////////////////////
  174. // CControlComboBoxPopup
  175. IMPLEMENT_XTP_CONTROL(CControlComboBoxPopup, CXTPControlComboBox)
  176. CString CControlComboBoxPopup::GetListBoxText() const
  177. {
  178. UINT nID = ((CMainFrame*)AfxGetMainWnd())->m_nIDSaveAs;
  179. CString str;
  180. str.LoadString(nID);
  181. return str;
  182. }
  183. //////////////////////////////////////////////////////////////////////////
  184. // CControlComboBoxCustomDraw
  185. IMPLEMENT_XTP_CONTROL(CControlComboBoxCustomDraw, CXTPControlFontComboBox)
  186. void CControlComboBoxCustomDraw::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  187. {
  188. CString strText;
  189. GetLBText(lpDrawItemStruct->itemID, strText);
  190. CRect rc(&lpDrawItemStruct->rcItem);
  191. ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
  192. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  193. BOOL bSelected = lpDrawItemStruct->itemState & ODS_SELECTED;
  194. CXTPPaintManager* pPaintManager = GetPaintManager();
  195. COLORREF crOldTextColor = pDC->SetTextColor(pPaintManager->GetRectangleTextColor(bSelected, FALSE, TRUE, FALSE, FALSE, xtpBarTypePopup, xtpBarPopup));
  196. if (bSelected)
  197. pPaintManager->DrawRectangle(pDC, rc, TRUE, FALSE, TRUE, FALSE, FALSE, xtpBarTypePopup, xtpBarPopup);
  198. else
  199. pDC->FillSolidRect(rc, pPaintManager->GetXtremeColor(COLOR_WINDOW));
  200. CFont fnt;
  201. fnt.CreatePointFont(100, strText);
  202. CXTPFontDC font(pDC, &fnt);
  203. pDC->SetBkMode(TRANSPARENT);
  204. rc.left += 3;
  205. pDC->DrawText(strText, rc, DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX);
  206. pDC->SetTextColor(crOldTextColor);
  207. }
  208. #ifdef _XTP_INCLUDE_CALENDAR
  209. //////////////////////////////////////////////////////////////////////////
  210. // CControlComboBoxDatePicker
  211. class CControlComboBoxDatePickerPopupBar : public CXTPControlComboBoxPopupBar
  212. {
  213. DECLARE_XTP_COMMANDBAR(CControlComboBoxDatePickerPopupBar)
  214. public:
  215. CControlComboBoxDatePickerPopupBar();
  216. ~CControlComboBoxDatePickerPopupBar();
  217. public:
  218. BOOL Popup(CXTPControlPopup* pControlPopup, BOOL /*bSelectFirst*/);
  219. BOOL DestroyWindow();
  220. public:
  221. class CDatePickerControl : public CXTPDatePickerControl
  222. {
  223. public:
  224. CDatePickerControl()
  225. {
  226. m_pControl = 0;
  227. SetMaxSelCount(1);
  228. }
  229. void SetModal()
  230. {
  231. m_bIsModal = TRUE;
  232. }
  233. virtual BOOL ContinueModal()
  234. {
  235. return TRUE;
  236. }
  237. virtual void EndModalLoop(int nResult)
  238. {
  239. if (!m_pControl)
  240. return;
  241. if (nResult == IDOK)
  242. {
  243. COleDateTime dt;
  244. GetCurSel(dt);
  245. m_pControl->SetDate(dt);
  246. m_pControl->OnExecute();
  247. }
  248. else
  249. {
  250. m_pControl->GetParent()->GetCommandBars()->ClosePopups();
  251. }
  252. m_pControl = NULL;
  253. ReleaseCapture();
  254. }
  255. CControlComboBoxDatePicker* m_pControl;
  256. };
  257. CDatePickerControl m_wndDatePickerControl;
  258. };
  259. IMPLEMENT_XTP_COMMANDBAR(CControlComboBoxDatePickerPopupBar, CXTPControlComboBoxPopupBar)
  260. CControlComboBoxDatePickerPopupBar::CControlComboBoxDatePickerPopupBar()
  261. {
  262. SetShowGripper(FALSE);
  263. m_wndDatePickerControl.SetBorderStyle(xtpDatePickerBorderNone);
  264. }
  265. CControlComboBoxDatePickerPopupBar::~CControlComboBoxDatePickerPopupBar()
  266. {
  267. }
  268. BOOL CControlComboBoxDatePickerPopupBar::Popup(CXTPControlPopup* pControlPopup, BOOL bSelectFirst)
  269. {
  270. if (!Create())
  271. return FALSE;
  272. ASSERT(pControlPopup->IsKindOf(RUNTIME_CLASS(CControlComboBoxDatePicker)));
  273. GetControls()->RemoveAll();
  274. CXTPControlCustom* pControl = new CXTPControlCustom();
  275. CRect rcMin;
  276. m_wndDatePickerControl.GetMinReqRect(rcMin);
  277. rcMin.bottom += 5;
  278. m_wndDatePickerControl.DestroyWindow();
  279. m_wndDatePickerControl.Create(WS_CHILD|WS_VISIBLE, rcMin, this, 0);
  280. m_wndDatePickerControl.SetModal();
  281. m_wndDatePickerControl.SetFocus();
  282. m_wndDatePickerControl.SetCapture();
  283. m_wndDatePickerControl.m_pControl = (CControlComboBoxDatePicker*)pControlPopup;
  284. m_wndDatePickerControl.SetCurSel(((CControlComboBoxDatePicker*)pControlPopup)->GetDate());
  285. m_wndDatePickerControl.EnsureVisibleSelection();
  286. pControl->SetControl(&m_wndDatePickerControl);
  287. pControl->SetSize(rcMin.Size());
  288. pControl->SetID(pControlPopup->GetID());
  289. GetControls()->Add(pControl);
  290. return CXTPControlComboBoxPopupBar::Popup(pControlPopup, bSelectFirst);
  291. }
  292. BOOL CControlComboBoxDatePickerPopupBar::DestroyWindow()
  293. {
  294. m_wndDatePickerControl.m_pControl = NULL;
  295. if (::GetCapture() == m_wndDatePickerControl.GetSafeHwnd())
  296. ReleaseCapture();
  297. return CXTPControlComboBoxPopupBar::DestroyWindow();
  298. }
  299. #endif
  300. IMPLEMENT_XTP_CONTROL(CControlComboBoxDatePicker, CXTPControlComboBox)
  301. CControlComboBoxDatePicker::CControlComboBoxDatePicker()
  302. {
  303. #ifdef _XTP_INCLUDE_CALENDAR
  304. CXTPCommandBar* pCommandBar = new CControlComboBoxDatePickerPopupBar();
  305. SetCommandBar(pCommandBar);
  306. SetDropDownListStyle();
  307. pCommandBar->InternalRelease();
  308. #endif
  309. SetDate(COleDateTime::GetCurrentTime());
  310. }
  311. CControlComboBoxDatePicker::~CControlComboBoxDatePicker()
  312. {
  313. }
  314. void CControlComboBoxDatePicker::SetDate(COleDateTime& dt)
  315. {
  316. if (m_dt != dt)
  317. {
  318. m_dt = dt;
  319. SetEditText(m_dt.Format(VAR_DATEVALUEONLY, LANG_USER_DEFAULT));
  320. }
  321. }
  322. const COleDateTime& CControlComboBoxDatePicker::GetDate() const 
  323. {
  324. return m_dt;
  325. }
  326. void CControlComboBoxDatePicker::OnKillFocus()
  327. {
  328. SetFocused(FALSE);
  329. }
  330. BOOL CControlComboBoxDatePicker::OnHookKeyDown(UINT nChar, LPARAM lParam)
  331. {
  332. if (nChar == VK_RETURN)
  333. {
  334. COleDateTime dt;
  335. if (dt.ParseDateTime(GetEditText()))
  336. {
  337. SetDate(dt);
  338. }
  339. ::SetFocus(m_pParent->GetTrackFocus());
  340. OnExecute();
  341. return TRUE;
  342. }
  343. if (nChar == VK_ESCAPE)
  344. return FALSE;
  345. return CXTPControlComboBox::OnHookKeyDown(nChar, lParam);
  346. }