MainFrm.cpp
上传用户:ljqmhy0909
上传日期:2007-01-02
资源大小:185k
文件大小:7k
源码类别:

工具条

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "FlatBar.h"
  5. #include "MainFrm.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMainFrame
  13. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  14. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  15. //{{AFX_MSG_MAP(CMainFrame)
  16. ON_WM_CREATE()
  17. ON_UPDATE_COMMAND_UI(ID_VIEW_FLATBAR, OnUpdateViewFlatbar)
  18. ON_COMMAND(ID_VIEW_FLATBAR, OnViewFlatbar)
  19. ON_UPDATE_COMMAND_UI(ID_ToggleCase, OnUpdateToggleCase)
  20. ON_COMMAND(ID_ToggleCase, OnToggleCase)
  21. //}}AFX_MSG_MAP
  22. // required handlers for adjusting toolbar-buttons
  23. ON_NOTIFY(TBN_QUERYDELETE, AFX_IDW_TOOLBAR, OnQueryDelete)
  24.     ON_NOTIFY(TBN_QUERYINSERT, AFX_IDW_TOOLBAR, OnQueryInsert)
  25. ON_NOTIFY(TBN_QUERYDELETE, AFX_IDW_TOOLBAR+5, OnQueryDelete)
  26.     ON_NOTIFY(TBN_QUERYINSERT, AFX_IDW_TOOLBAR+5, OnQueryInsert)
  27. #if _MFC_VER >= 0x0420
  28. ON_NOTIFY( NM_CUSTOMDRAW, AFX_IDW_TOOLBAR, OnCustomToolbarDraw )
  29. ON_NOTIFY( NM_CUSTOMDRAW, AFX_IDW_TOOLBAR+5, OnCustomToolbarDraw )
  30. #endif
  31. END_MESSAGE_MAP()
  32. static UINT indicators[] =
  33. {
  34. ID_SEPARATOR,           // status line indicator
  35. ID_INDICATOR_CAPS,
  36. ID_INDICATOR_NUM,
  37. ID_INDICATOR_SCRL,
  38. };
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMainFrame construction/destruction
  41. CMainFrame::CMainFrame()
  42. : m_bCaseCheck(FALSE)
  43. {
  44. // TODO: add member initialization code here
  45. }
  46. CMainFrame::~CMainFrame()
  47. {
  48. }
  49. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  50. {
  51. if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  52. return -1;
  53. if (!m_wndToolBar.Create(this) ||
  54. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  55. {
  56. TRACE0("Failed to create toolbarn");
  57. return -1;      // fail to create
  58. }
  59. if (!m_wndTextBar.Create(this,WS_CHILD | WS_VISIBLE | CBRS_TOP,AFX_IDW_TOOLBAR+5) ||
  60. !m_wndTextBar.LoadToolBar(IDR_MAINFRAME))
  61. {
  62. TRACE0("Failed to create toolbarn");
  63. return -1;      // fail to create
  64. }
  65. if (!m_wndStatusBar.Create(this) ||
  66. !m_wndStatusBar.SetIndicators(indicators,
  67.   sizeof(indicators)/sizeof(UINT)))
  68. {
  69. TRACE0("Failed to create status barn");
  70. return -1;      // fail to create
  71. }
  72. // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  73. m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  74. CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  75. m_wndTextBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  76. CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  77. // Let the user adjust the buttons by Shift-LeftClick.
  78. // This requires to install handlers for TBN_QUERYDELETE
  79. // and TBN_QUERYINSERT notifications (see above and below).
  80. m_wndToolBar.GetToolBarCtrl().ModifyStyle(0,CCS_ADJUSTABLE,0);
  81. m_wndTextBar.GetToolBarCtrl().ModifyStyle(0,CCS_ADJUSTABLE,0);
  82. // TODO: Delete these three lines if you don't want the toolbar to
  83. //  be dockable
  84. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  85. m_wndTextBar.EnableDocking(CBRS_ALIGN_ANY);
  86. m_wndTextBar.SetSizes(CSize(42,40), CSize(16,15));
  87. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_FILE_NEW), "New");
  88. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_FILE_OPEN), "Open");
  89. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_FILE_SAVE), "Save");
  90. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_EDIT_CUT), "Cut");
  91. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_EDIT_COPY), "Copy");
  92. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_EDIT_PASTE), "Paste");
  93. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_ToggleCase), "Case");
  94. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_FILE_PRINT), "Print");
  95. m_wndTextBar.SetButtonText(m_wndTextBar.CommandToIndex(ID_APP_ABOUT), "About");
  96. EnableDocking(CBRS_ALIGN_ANY);
  97. DockControlBar(&m_wndToolBar);
  98. DockControlBar(&m_wndTextBar);
  99. return 0;
  100. }
  101. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  102. {
  103. // TODO: Modify the Window class or styles here by modifying
  104. //  the CREATESTRUCT cs
  105. return CMDIFrameWnd::PreCreateWindow(cs);
  106. }
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CMainFrame diagnostics
  109. #ifdef _DEBUG
  110. void CMainFrame::AssertValid() const
  111. {
  112. CMDIFrameWnd::AssertValid();
  113. }
  114. void CMainFrame::Dump(CDumpContext& dc) const
  115. {
  116. CMDIFrameWnd::Dump(dc);
  117. }
  118. #endif //_DEBUG
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMainFrame message handlers
  121. void CMainFrame::OnUpdateViewFlatbar(CCmdUI* pCmdUI) 
  122. {
  123. pCmdUI->SetCheck(m_wndToolBar.IsFlatLook());
  124. }
  125. void CMainFrame::OnViewFlatbar() 
  126. {
  127. m_wndToolBar.SetFlatLook(!m_wndToolBar.IsFlatLook());
  128. m_wndTextBar.SetFlatLook(!m_wndTextBar.IsFlatLook());
  129. }
  130. void CMainFrame::OnQueryDelete(NMHDR*, LRESULT* pResult) {
  131.   *pResult = TRUE; // allow deletion of a button
  132. }
  133. void CMainFrame::OnQueryInsert(NMHDR*, LRESULT* pResult) {
  134.   *pResult = TRUE; // allow insertion of a button
  135. }
  136. #if _MFC_VER >= 0x0420
  137. // custom draw of a toolbar is available since VC++ 4.2
  138. void CMainFrame :: OnCustomToolbarDraw( NMHDR * pNotify, LRESULT * pResult ) {
  139. LPNMCUSTOMDRAW pCDRW = (LPNMCUSTOMDRAW) pNotify;
  140. *pResult = CDRF_DODEFAULT;
  141. CToolBarEx * pBar = (pNotify->idFrom == AFX_IDW_TOOLBAR) ?
  142. &m_wndToolBar : &m_wndTextBar;
  143. switch( pCDRW->dwDrawStage ) {
  144. case CDDS_PREPAINT:
  145. TRACE0("receiving PREPAINTn");
  146. *pResult = CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYITEMDRAW;
  147. break;
  148. case CDDS_ITEMPREPAINT:
  149. TRACE1("receiving ITEMPREPAINT for index %dn",pBar->CommandToIndex(pCDRW->dwItemSpec));
  150. if( pBar->IsFlatLook() ) {
  151. // if the cursor is on this button, then draw it with blue
  152. // text instead of black:
  153. CRect rect;
  154. pBar->GetItemRect(pBar->CommandToIndex(pCDRW->dwItemSpec), rect);
  155. CPoint pt;
  156. ::GetCursorPos(&pt);
  157. pBar->ScreenToClient(&pt);
  158. if( rect.PtInRect(pt) )
  159. ::SetTextColor(pCDRW->hdc, RGB(0,0,255));
  160. }
  161. *pResult = CDRF_NOTIFYPOSTPAINT; // request for ITEMPOSTPAINT
  162. break;
  163. case CDDS_ITEMPOSTPAINT:
  164. TRACE1("receiving ITEMPOSTPAINT for index %dn",pBar->CommandToIndex(pCDRW->dwItemSpec));
  165. break;
  166. case CDDS_POSTPAINT:
  167. TRACE0("receiving POSTPAINTn");
  168. break;
  169. default:
  170. // CToolBar does not send other draw-stages than the above.
  171. TRACE0("unknown draw stage ...n");
  172. break;
  173. }
  174. }
  175. #endif // _MFC_VER >= 0x0420
  176. void CMainFrame::OnUpdateToggleCase(CCmdUI* pCmdUI) 
  177. {
  178. pCmdUI->SetCheck(m_bCaseCheck);
  179. }
  180. void CMainFrame::OnToggleCase() 
  181. {
  182. m_bCaseCheck = ! m_bCaseCheck;
  183. }