MainFrm.cpp
上传用户:kssdz899
上传日期:2007-01-08
资源大小:79k
文件大小:11k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- // Contains menu option handlers for customisation of MDI child window
- // captions.
- //
- //
- #include "stdafx.h"
- #include "Resource.h"
- #include "CaptionDemo.h"
- #include "TitleDialog.h"
- #include "ColorPickDialog.h"
- #include "MaxLinesDialog.h"
- #include "BmpCaptionBackground.h"
- #include "ChildFrm.h"
- ///////////////
- // Includes for custom caption classes
- //
- #include "..CustomCaptionMultiLineCaptionEx.h"
- #include "..CustomCaptionCaptionTextAttributes.h"
- #include "..CustomCaptionCaptionBackground.h"
- #include "..CustomCaptionGradientCaptionBackground.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_EDIT_SET_TITLE, OnEditSetTitle)
- ON_COMMAND(ID_EDIT_SET_ACTIVE_FONT, OnEditSetActiveFont)
- ON_COMMAND(ID_EDIT_SET_BMP_CAPTION, OnEditSetBmpCaption)
- ON_COMMAND(ID_EDIT_SET_GRADIENT_CAPTION, OnEditSetGradientCaption)
- ON_COMMAND(ID_EDIT_SET_DEFAULT_BACKGROUND, OnEditSetDefaultBackground)
- ON_COMMAND(ID_EDIT_SET_DEFAULT_ACTIVE_COLORS, OnEditSetDefaultActiveColors)
- ON_COMMAND(ID_EDIT_SET_ACTIVE_COLORS, OnEditSetActiveColors)
- ON_COMMAND(ID_EDIT_SET_DEFAULT_INACTIVE_COLORS, OnEditSetDefaultInactiveColors)
- ON_COMMAND(ID_EDIT_SET_INACTIVE_COLORS, OnEditSetInactiveColors)
- ON_COMMAND(ID_EDIT_SET_DEFAULT_FONT, OnEditSetDefaultFonts)
- ON_COMMAND(ID_EDIT_CAPTION_MULTILINE, OnEditCaptionMultiline)
- ON_COMMAND(ID_EDIT_CAPTION_SINGLELINE, OnEditCaptionSingleline)
- ON_COMMAND(ID_EDIT_SET_INACTIVE_FONT, OnEditSetInactiveFont)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CMDIFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CMDIFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CMDIFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- ////////////
- // Set the title of the active document
- //
- void CMainFrame::OnEditSetTitle()
- {
- CMDIChildWnd* pChild = MDIGetActive();
- if (!pChild)
- return;
- CDocument* pActiveDoc = pChild->GetActiveDocument();
- if (!pActiveDoc)
- return;
- CTitleDialog titleDialog(this);
- pChild->GetWindowText(titleDialog.m_TitleEdit);
- if (titleDialog.DoModal() == IDOK)
- pActiveDoc->SetTitle(titleDialog.m_TitleEdit);
- }
- /////////////////
- // Allow user to change the active caption font
- //
- void CMainFrame::OnEditSetActiveFont()
- {
- CCaption& caption = GetActiveMDICaption();
- /////////////
- // Get current caption active font details to display in dialog
- //
- LOGFONT lf;
- caption.GetTextAttributes()->GetActiveFont()->GetLogFont(&lf);
-
- //////////
- // Display font dialog with current font details
- //
- CFontDialog fontDlg(&lf);
- fontDlg.m_cf.Flags &= ~CF_EFFECTS; // Disable special font effects
- if (fontDlg.DoModal() != IDOK)
- return;
-
- ///////////
- // Retrieve selected font and set into caption text attributes
- //
- fontDlg.GetCurrentFont(&lf);
- caption.GetTextAttributes()->SetActiveFont(lf);
- caption.Refresh();
- }
- /////////////////
- // Allow user to change the inactive caption font
- //
- void CMainFrame::OnEditSetInactiveFont()
- {
- CCaption& caption = GetActiveMDICaption();
- /////////////
- // Get current caption inactive font details to display in dialog
- //
- LOGFONT lf;
- caption.GetTextAttributes()->GetInactiveFont()->GetLogFont(&lf);
- //////////
- // Display font dialog with current font details
- //
- CFontDialog fontDlg(&lf);
-
- fontDlg.m_cf.Flags &= ~CF_EFFECTS; // Disable special font effects
- if (fontDlg.DoModal() != IDOK)
- return;
-
- ///////////
- // Retrieve selected font and set into caption text attributes
- //
- fontDlg.GetCurrentFont(&lf);
- caption.GetTextAttributes()->SetInactiveFont(lf);
- caption.Refresh();
- }
- /////////////////
- // Allow user to reset the caption fonts to the system default
- //
- void CMainFrame::OnEditSetDefaultFonts()
- {
- CCaption& caption = GetActiveMDICaption();
- caption.GetTextAttributes()->UseSystemFonts();
- caption.Refresh();
- }
- /////////////////
- // Allow user to set the caption background to a shaded gradient
- //
- void CMainFrame::OnEditSetGradientCaption()
- {
- CCaption& caption = GetActiveMDICaption();
- //////////
- // Construct a gradient background object
- //
- CCaptionBackground* pNewBackground = new CGradientCaptionBackground();
- //////////
- // Copy the colours from the old background object
- //
- pNewBackground->SetCustomColors(*caption.GetBackground());
- ////////////
- // Set the new background
- //
- caption.SetBackground(pNewBackground);
- }
- /////////////////
- // Allow user to set the caption background to a bitmap
- //
- void CMainFrame::OnEditSetBmpCaption()
- {
- CCaption& caption = GetActiveMDICaption();
- char path[MAX_PATH] = { ' ' };
- /////////////
- // Check for existing bitmap
- //
- CBmpCaptionBackground* pBmpBackground = dynamic_cast<CBmpCaptionBackground*>(caption.GetBackground());
- /////////////
- // Use previous bitmap path if valid, otherwise set to Windows directory
- //
- if (pBmpBackground && pBmpBackground->IsValid())
- {
- strcpy(path, pBmpBackground->GetPath());
- }
- else
- {
- GetWindowsDirectory(path, MAX_PATH);
- }
- /////////////
- // Display file dialog set to look for .BMP files in the path
- //
- DWORD flags = OFN_FILEMUSTEXIST|OFN_LONGNAMES;
- CFileDialog bmpDlg(TRUE, "bmp", 0, 0, "Bitmap Files (*.bmp)|*.bmp||", this);
- bmpDlg.m_ofn.lpstrInitialDir = path;
- if (bmpDlg.DoModal() != IDOK)
- return;
- ///////////////
- // Retrieve selected file path and set into new caption bitmap background
- //
- CString bmpFile = bmpDlg.GetPathName();
- caption.SetBackground(new CBmpCaptionBackground(bmpFile));
- }
- /////////////////
- // Allow user to reset the caption background to a plain default background
- //
- void CMainFrame::OnEditSetDefaultBackground()
- {
- CCaption& caption = GetActiveMDICaption();
- ///////////
- // Construct a plain background object from the old background
- //
- CCaptionBackground* pNewBackground = new CCaptionBackground(*caption.GetBackground());
- ////////////
- // Set the new background
- //
- caption.SetBackground(pNewBackground);
- }
- /////////////////
- // Allow user to reset the active caption colours to the system defaults
- //
- void CMainFrame::OnEditSetDefaultActiveColors()
- {
- CCaption& caption = GetActiveMDICaption();
- ////////////
- // Set active caption text and background colours to system defaults
- //
- caption.GetTextAttributes()->UseSystemActiveColor();
- caption.GetBackground()->UseSystemActiveColor();
- caption.Refresh();
- }
- /////////////////
- // Allow user to reset the inactive caption colours to the system defaults
- //
- void CMainFrame::OnEditSetDefaultInactiveColors()
- {
- CCaption& caption = GetActiveMDICaption();
- caption.GetTextAttributes()->UseSystemInactiveColor();
- caption.GetBackground()->UseSystemInactiveColor();
- caption.Refresh();
- }
- /////////////////
- // Allow user to set the active caption colours
- //
- void CMainFrame::OnEditSetActiveColors()
- {
- CCaption& caption = GetActiveMDICaption();
- /////////////
- // Get current active colours to show in colour picker
- //
- COLORREF colorTxt = caption.GetTextAttributes()->GetActiveColor();
- COLORREF colorBk = caption.GetBackground()->GetActiveColor();
- ////////////
- // Show colour picker and set selected text and background active colours into the caption
- //
- CColorPickDlg colorPickdlg(colorTxt, colorBk);
- if (colorPickdlg.DoModal() == IDOK)
- {
- caption.GetTextAttributes()->SetActiveColor(colorTxt);
- caption.GetBackground()->SetActiveColor(colorBk);
- caption.Refresh();
- }
- }
- /////////////////
- // Allow user to set the inactive caption colours
- //
- void CMainFrame::OnEditSetInactiveColors()
- {
- CCaption& caption = GetActiveMDICaption();
- /////////////
- // Get current inactive colours to show in colour picker
- //
- COLORREF colorTxt = caption.GetTextAttributes()->GetInactiveColor();
- COLORREF colorBk = caption.GetBackground()->GetInactiveColor();
- ////////////
- // Show colour picker and set selected text and background inactive colours into the caption
- //
- CColorPickDlg colorPickdlg(colorTxt, colorBk);
- if (colorPickdlg.DoModal() == IDOK)
- {
- caption.GetTextAttributes()->SetInactiveColor(colorTxt);
- caption.GetBackground()->SetInactiveColor(colorBk);
- caption.Refresh();
- }
- }
- /////////////////
- // Allow user to set the maximum number of caption lines
- //
- void CMainFrame::OnEditCaptionMultiline()
- {
- CMultiLineCaption& caption = GetActiveMDICaption();
- ///////////
- // Display dialog for input of maximum lines number and set result into caption
- //
- CMaxLinesDialog maxLinesDlg;
- maxLinesDlg.m_StrMaxLines.Format("%d", caption.GetMaxLines());
- if (maxLinesDlg.DoModal() == IDOK)
- {
- caption.SetMaxLines(atoi(maxLinesDlg.m_StrMaxLines));
- }
- }
- /////////////////
- // Allow user to set a single caption line
- //
- void CMainFrame::OnEditCaptionSingleline()
- {
- CMultiLineCaption& caption = GetActiveMDICaption();
- caption.SetMaxLines(1);
- }
- /////////////
- // Helper function to get the caption for the active MDI child frame
- //
- CMultiLineCaption& CMainFrame::GetActiveMDICaption()
- {
- CChildFrame* pChild = dynamic_cast<CChildFrame*>(MDIGetActive());
- CMultiLineCaption& caption = pChild->GetCaption();
- return caption;
- }