////////////////////////////////////////////////////////////////
// MSDN Magazine ! September 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// This program shows how to avoid "placement new" errors when using
// managed extensions with MFC's DEBUG_NEW. This requires defining a new
// symbol, mfcnew, which you must remember to use to allocate MFC
// objects.
//
#include "stdafx.h"
#include "resource.h"
#using <mscorlib.dll>
// Define mfcnew to DEBUG_NEW or ordinary new.
#ifdef _DEBUG
#define mfcnew DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#else
#define mfcnew new
#endif
using namespace System;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
return -1;
}
// Allocate MFC CString with mfcnew
// Since this string is never freed, MFC reports the memory leak if
// you run in the debuggger.
CString *s = mfcnew CString(_T("This is an MFC CString.\n"));
_tprintf((LPCTSTR)*s);
// Allocate managed String with new
String* s2 = new String(S"This is a managed string literal.");
Console::WriteLine(s2);
return 0;
}
Figure 3 MainFrm
MainFrm.h
/ ////////////////////////////////////////////////////////////////
// MSDN Magazine ! August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
#include "LockBar.h"
//////////////////
// Typical main frame. Uses CLockBar to lock the toolbar.
//
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
virtual ~CMainFrame();
protected:
DECLARE_DYNAMIC(CMainFrame)
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CLockBar m_lockToolbar; // toolbar lock
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnLockBars();
afx_msg void OnUpdateLockBars(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
MainFrm.cpp
////////////////////////////////////////////////////////////////
// MSDN Magazine ! August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
#include "StdAfx.h"
#include "MainFrm.h"
#include "resource.h"
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(ID_VIEW_LOCKBARS, OnLockBars)
ON_UPDATE_COMMAND_UI(ID_VIEW_LOCKBARS, OnUpdateLockBars)
END_MESSAGE_MAP()
// typical stuff omitted
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
•••
VERIFY(m_wndToolBar.CreateEx(this));
VERIFY(m_wndToolBar.LoadToolBar(IDR_MAINFRAME));
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC | CBRS_GRIPPER);
// Enable toolbar docking
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
// Install toolbar lock
m_lockToolbar.Install(&m_wndToolBar);
return 0;
}
//////////////////
// Command: toggle toolbar locked state
//
void CMainFrame::OnLockBars()
{
m_lockToolbar.SetLocked(!m_lockToolbar.GetLocked()); // toggle lock
}
//////////////////
// Command update: display checkmark for toolbar locked state
//
void CMainFrame::OnUpdateLockBars(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_lockToolbar.GetLocked());
}
Figure 4 LockBar
LockBar.h
////////////////////////////////////////////////////////////////
// MSDN Magazine ! August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
#include "Subclass.h"
//////////////////
// Plug-in class to support locking toolbars. To use: instantiate one of
// these for each toolbar you want to be able to lock. From your
// InitInstance call:
//
// m_lock.Install(&m_wndToolbar);
//
// Then call m_lock.SetLocked(TRUE/FALSE) to lock/unlock the toolbar.
//
class CLockBar : public CSubclassWnd
{
protected:
CToolBar* m_pBar; // toolbar I am locking
BOOL m_bLocked; // whether locked or not
public:
CLockBar();
~CLockBar();
// Call this to install the lock
BOOL Install(CToolBar* pBar) {
ASSERT(pBar && m_pBar==NULL);
m_pBar = pBar;
return HookWindow(pBar);
}
// Get/set locked state
BOOL GetLocked() {
return m_bLocked;
}
void SetLocked(BOOL bLocked);
// message trap for msgs sent to toolbar
virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
};
LockBar.cpp
////////////////////////////////////////////////////////////////
// MSDN Magazine ! August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
#include "StdAfx.h"
#include "LockBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CLockBar::CLockBar() : m_pBar(NULL), m_bLocked(0)
{
}
CLockBar::~CLockBar()
{
}
void CLockBar::SetLocked(BOOL bLocked)
{
CFrameWnd* pFrame = m_pBar->GetParentFrame();
ASSERT(pFrame);
m_bLocked = bLocked;
DWORD dwStyle = m_pBar->GetBarStyle();
if (bLocked) {
pFrame->DockControlBar(m_pBar); // dock if not already
dwStyle &= ~CBRS_GRIPPER; // turn off gripper
} else {
dwStyle |= CBRS_GRIPPER;
}
m_pBar->SetBarStyle(dwStyle);
pFrame->RecalcLayout(); // make frame recalc toolbar sizes
}
//////////////////
// Trap messages sent to control bar.
//
LRESULT CLockBar::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
if ((msg==WM_LBUTTONDOWN || msg==WM_LBUTTONDBLCLK) && m_bLocked) {
// Got click or double-click and toolbar is locked: if mouse in
// "dead zone" then ignore the message!don't pass to control bar
//
CPoint pt(lp);
if (m_pBar->OnToolHitTest(pt, NULL) == -1)
return 0; // return without handling: bypass control bar
// dragging!
}
// pass unhandled messages subclassed window!this is important!
return CSubclassWnd::WindowProc(msg, wp, lp);
}