⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appbar.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
字号:

#include "stdafx.h"
#include "resource.h"
#include "editcmd.h"
#include "appbar.h"
#include "apbpropg.h"
#include "sheetpls.h"
#include "registry.h"
#include "editreg.h"
#include "wm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma warning ( disable : 4711 )
/////////////////////////////////////////////////////////////////////////////
// CAppBar

CAppBar::CAppBar ()
{
  m_pWnd = this;
  m_dwFlags = 0;
}

CAppBar:: ~ CAppBar ()
{
}

BEGIN_MESSAGE_MAP (CAppBar, baseCAppBar)
//{{AFX_MSG_MAP(CAppBar)
ON_WM_CREATE ()
ON_WM_SIZE ()
ON_WM_DESTROY ()
ON_WM_RBUTTONUP ()
ON_MESSAGE (WM_APPEND_MENU, OnAppendMenu)
ON_MESSAGE (WM_PROCESS_MENU, OnProcessMenu)
ON_MESSAGE (WM_APPEND_PROPS, OnAppendProps)
ON_COMMAND(ID_CLOSE, OnCmdClose)
ON_COMMAND(ID_DOCKABLE, OnCmdDockable)
ON_COMMAND(ID_PROPERTIES, OnProperties)
//}}AFX_MSG_MAP
END_MESSAGE_MAP ()

bool CAppBar::
CreateBar (LPCTSTR pszName, LPRECT rect, DWORD dwStyle, DWORD nFlags, DWORD id, CFrameWnd *pParent)
{
  DWORD dwFlags;
  POINT ptFloat;
  m_dwSCBStyle = dwStyle;
  if (LoadPlacement (&dwFlags, &ptFloat))
    {
      nFlags = dwFlags;
      if (ptFloat.x != -1)
        {
          rect->left = ptFloat.x;
        }
      if (ptFloat.y != -1)
        {
          rect->top = ptFloat.y;
        }
      if (nFlags & ABS_DOCK)
        {
          if ((nFlags & ABS_DOCK) == ABS_DOCKLEFT || (nFlags & ABS_DOCK) == ABS_DOCKRIGHT)
            {
              rect->right = rect->left + m_szVert.cx;
              rect->bottom = rect->top + m_szVert.cy;
            }
          else
            {
              rect->right = rect->left + m_szHorz.cx;
              rect->bottom = rect->top + m_szHorz.cy;
            }
        }
      else
        {
          rect->right = rect->left + m_szFloat.cx;
          rect->bottom = rect->top + m_szFloat.cy;
        }
    }
  if (!Create (pszName, pParent, CSize (rect->right - rect->left, rect->bottom - rect->top), nFlags, id))
    {
      TRACE1 (_T("Failed to create CAppBar \"%s\"\n"), pszName);
      return false;
    }
  SetBarStyle (GetBarStyle () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  if (nFlags & (ABS_DOCKABLE | ABS_DOCK))
    {
      nFlags |= ABS_DOCKABLE;
      EnableDocking (CBRS_ALIGN_ANY);
    }
  m_dwFlags = nFlags;
  if (nFlags & ABS_DOCK)
    {
      pParent->DockControlBar (this, nFlags & ABS_DOCK);
    }
  else
    {
      pParent->FloatControlBar (this, CPoint (rect->left, rect->top));
    }
  if (!(nFlags & ABS_VISIBLE))
    pParent->ShowControlBar (this, FALSE, FALSE);
  return true;
}

bool CAppBar::
LoadPlacement (DWORD *pdwFlags /*= NULL*/, LPPOINT pptFloat /*= NULL*/)
{
  CString sName = REG_EDITPAD_IDE _T ("\\") + GetName ();
  if (pdwFlags && !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("status"), pdwFlags) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("horzWidth"), (DWORD*) &m_szHorz.cx) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("horzHeight"), (DWORD*) &m_szHorz.cy) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("vertWidth"), (DWORD*) &m_szVert.cx) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("vertHeight"), (DWORD*) &m_szVert.cy) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("floatWidth"), (DWORD*) &m_szFloat.cx) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("floatHeight"), (DWORD*) &m_szFloat.cy) ||
    pptFloat && (!RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("floatLeft"), (DWORD*) &pptFloat->x) ||
    !RegLoadNumber (HKEY_CURRENT_USER, sName, _T ("floatTop"), (DWORD*) &pptFloat->y)))
    {
      return false;
    }
  if (m_szMin.cx > m_szHorz.cx)
    {
      m_szHorz.cx = m_szMin.cx;
    }
  if (m_szMin.cy > m_szHorz.cy)
    {
      m_szHorz.cy = m_szMin.cy;
    }
  if (m_szMin.cx > m_szVert.cx)
    {
      m_szVert.cx = m_szMin.cx;
    }
  if (m_szMin.cy > m_szVert.cy)
    {
      m_szVert.cy = m_szMin.cy;
    }
  if (m_szMin.cx > m_szFloat.cx)
    {
      m_szFloat.cx = m_szMin.cx;
    }
  if (m_szMin.cy > m_szFloat.cy)
    {
      m_szFloat.cy = m_szMin.cy;
    }
  return true;
}

bool CAppBar::
SavePlacement ()
{
  CString sName = REG_EDITPAD_IDE _T ("\\") + GetName ();
  DWORD dwFlags = m_dwFlags & ~ABS_DOCK;
  POINT ptFloat;
  if (IsFloating ())
    {
      CRect rc;
      CWnd *pWnd = GetDockingFrame ();
      ASSERT (pWnd);
      pWnd->GetWindowRect (rc);
      ptFloat = rc.TopLeft ();
    }
  else
    {
      dwFlags |= m_nDockBarID;
      ptFloat.x = ptFloat.y = -1;
    }
  if (IsVisible ())
    dwFlags |= ABS_VISIBLE;
  if(RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("status"), dwFlags) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("horzWidth"), m_szHorz.cx) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("horzHeight"), m_szHorz.cy) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("vertWidth"), m_szVert.cx) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("vertHeight"), m_szVert.cy) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("floatWidth"), m_szFloat.cx) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("floatHeight"), m_szFloat.cy) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("floatLeft"), ptFloat.x) &&
    RegSaveNumber (HKEY_CURRENT_USER, sName, _T ("floatTop"), ptFloat.y))
    {
      return true;
    }
  return false;
}

/////////////////////////////////////////////////////////////////////////////
// CAppBar message handlers

int CAppBar::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
  if (baseCAppBar::OnCreate (lpCreateStruct) == -1)
    return -1;
  SetFont (CFont::FromHandle ((HFONT) GetStockObject (DEFAULT_GUI_FONT)));
  // m_butBottom.Create( NULL, WS_CHILD | WS_VISIBLE, CRect( 0, 0, 80, 40 ), this, 1 );
  // AddChild( m_butBottom.GetSafeHwnd(), ORD_FULL_END, CRect( 0, 0, 0, 0 ), NULL );
  return 0;
}

void CAppBar::
OnUpdateCmdUI (CFrameWnd * pTarget, BOOL bDisableIfNoHndler)
{
  baseCAppBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);

  UpdateDialogControls (pTarget, bDisableIfNoHndler);
}

void CAppBar::
OnSize (UINT nType, int cx, int cy)
{
  baseCAppBar::OnSize (nType, cx, cy);
  if (cx >= 0 && cy >= 0 && cx < 65520 && cy < 65520)
    {
      COrderer::OnSize (nType, cx, cy);
    }
}

void CAppBar::
OnDestroy ()
{
  SavePlacement ();
  baseCAppBar::OnDestroy ();
  //STATIC_DOWNCAST (CMDIFrameWnd, AfxGetMainWnd ())->RecalcLayout (FALSE);
}

#define ID_SPACE_FIRST ID_DOCKABLE
#define ID_SPACE_LAST ID_CLOSE

#pragma warning ( disable : 4100 )
void CAppBar::
OnRButtonUp (UINT nFlags, CPoint point)
{
  CMenu menu;
  if (menu.CreatePopupMenu ())
    {
      DWORD dwResult = 0;
      CWnd *pWnd = this;
      bool bSeparated = false, bContinue = true, bProcess = false, bProperties = false;
      do
        {
          dwResult = pWnd->SendMessage (WM_APPEND_MENU, dwResult, (LPARAM) menu.GetSafeHmenu ());
          if (dwResult & 1)
            {
              bProcess = true;
              if (dwResult & 2)
                {
                  bContinue = false;
                }
              if (dwResult & 4)
                {
                  bSeparated = true;
                }
              if (dwResult & 8)
                {
                  bProperties = true;
                }
            }
          pWnd = pWnd->GetParent ();
        }
      while (pWnd && bContinue);
      if (bProcess)
        {
          if (bProperties)
            {
              if (!bSeparated)
                VERIFY (menu.AppendMenu (MF_SEPARATOR));
              if (bProperties)
                {
                  CString sCaption;
                  sCaption.LoadString (IDS_MENU_PROPERTIES);
                  VERIFY (menu.AppendMenu (MF_STRING, ID_PROPERTIES, sCaption));
                }
            }
          ClientToScreen (&point);
          do
            {
              DWORD dwReturn = menu.TrackPopupMenu (TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RETURNCMD|TPM_LEFTALIGN|TPM_TOPALIGN, point.x, point.y, this);
              bProperties = false;
              if (dwReturn)
                {
                  if (dwReturn == ID_PROPERTIES)
                    {
                      SendMessage (WM_COMMAND, dwReturn, (LPARAM) this);
                    }
                  else
                    {
                      dwResult = 0;
                      pWnd = this;
                      bContinue = true;
                      do
                        {
                          dwResult = pWnd->SendMessage (WM_PROCESS_MENU, dwReturn);
                          if (dwResult & 1)
                            {
                              if (dwResult & 2)
                                {
                                  bContinue = false;
                                }
                              if (dwResult & 4)
                                {
                                  bProperties = true;
                                }
                            }
                          pWnd = pWnd->GetParent ();
                        }
                      while (pWnd && bContinue);
                    }
                }
            }
          while (bProperties);
        }
    }
  // CWnd::OnRButtonUp (nFlags, point);
}
#pragma warning ( default : 4100 )

#pragma warning ( disable : 4100 )
LONG CAppBar::
OnAppendMenu (WPARAM wParam, LPARAM lParam)
{
  CMenu menu;
  CMenu *pMenu;
  if (menu.LoadMenu (IDM_APPBAR_SPACE) && (pMenu = menu.GetSubMenu (0)) != NULL)
    {
      int nCount = pMenu->GetMenuItemCount ();
      UINT uItem;
      CString sItem;
      DWORD dwResult = 25;
      for (int i = 0; i < nCount; i++)
        {
          uItem = pMenu->GetMenuItemID (i);
          if (uItem)
            {
              dwResult &= ~4;
              pMenu->GetMenuString (i, sItem, MF_BYPOSITION);
              VERIFY (::AppendMenu ((HMENU) lParam, MF_STRING, uItem, sItem));
            }
          else
            {
              dwResult |= 4;
              VERIFY (::AppendMenu ((HMENU) lParam, MF_SEPARATOR, 0, NULL));
            }
        }
      CheckMenuItem ((HMENU) lParam, ID_DOCKABLE, MF_BYCOMMAND|(m_dwFlags & ABS_DOCKABLE ? MF_CHECKED : MF_UNCHECKED));
      return dwResult;
  }
  return 0;
}
#pragma warning ( default : 4100 )

#pragma warning ( disable : 4100 )
LONG CAppBar::
OnProcessMenu (WPARAM wParam, LPARAM lParam)
{
  if (wParam >= ID_SPACE_FIRST && wParam <= ID_SPACE_LAST)
    {
      SendMessage (WM_COMMAND, wParam, (LPARAM) this);
      return 1;
    }
  return 0;
}
#pragma warning ( default : 4100 )

#pragma warning ( disable : 4100 )
LONG CAppBar::
OnAppendProps (WPARAM wParam, LPARAM lParam)
{
  CAppBarPropPage *pPage = new CAppBarPropPage;
  if (pPage)
    {
      pPage->m_psp.dwFlags &= ~PSP_HASHELP;
      pPage->Init (this);
      ((CPropertySheetPlus*) lParam)->AddPagePlus (pPage);
      return 1;
    }
  return 0;
}
#pragma warning ( default : 4100 )

void CAppBar::
OnProperties ()
{
  CPropertySheetPlus sheet (IDS_CAPTION_PROPERTIES, this);
  DWORD dwResult = 0;
  CWnd *pWnd = this;
  bool bContinue = true, bProcess = false;
  do
    {
      dwResult = pWnd->SendMessage (WM_APPEND_PROPS, dwResult, (LPARAM) &sheet);
      if (dwResult & 1)
        {
          bProcess = true;
          if (dwResult & 2)
            {
              bContinue = false;
            }
        }
      pWnd = pWnd->GetParent ();
    }
  while (pWnd && bContinue);
  if (bProcess)
    {
      sheet.m_psh.dwFlags &= ~PSH_HASHELP;
      sheet.m_psh.dwFlags |= PSH_USEHICON;
      sheet.m_psh.hIcon = ::LoadIcon (NULL, MAKEINTRESOURCE (IDI_EDITPAD));
      sheet.m_bDeletePages = true;
      sheet.DoModal ();
    }
}

void CAppBar::OnCmdDockable() 
{
  DWORD dwStyle = GetBarStyle ();
  if (m_dwFlags & ABS_DOCKABLE)
    {
      if (!IsFloating ())
        {
          ToggleDocking ();
          AfxGetMainFrame ()->RecalcLayout (FALSE);
        }
      EnableDocking (0);
      m_dwFlags &= ~ABS_DOCKABLE;
    }
  else
    {
      EnableDocking (CBRS_ALIGN_ANY);
      m_dwFlags |= ABS_DOCKABLE;
    }
}

void CAppBar::OnCmdClose() 
{
  AfxGetMainFrame ()->ShowControlBar (this, FALSE, FALSE);
}

/////////////////////////////////////////////////////////////////////////////
#pragma warning ( default : 4711 )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -