📄 ekutil.cpp
字号:
{
// Compute message text extent
CClientDC dc( pStatusBar );
CFont* pFont = pStatusBar->GetFont();
CFont* pOldFont = dc.SelectObject( pFont );
CSize sizeText = dc.GetTextExtent( szMessage );
dc.SelectObject( pOldFont );
rc.left += sizeText.cx + cxMargin;
}
// 3 - Compute progress bar width
if( cxMaxWidth != -1 )
{
rc.right = rc.left + min( cxMaxWidth, rc.Width() );
}
// 4 - Display message text
pStatusBar->SetPaneText( nPaneIndex, szMessage );
pStatusBar->RedrawWindow();
// 5 - Create progress control
return pProgressCtrl->Create( WS_CHILD | WS_VISIBLE,
rc, pStatusBar, nIDControl );
}
///////////////////////////////////////////////////////////
// EkCreateToolBar: function implementation
BOOL EkCreateToolBar( CFrameWnd* pParentFrame,
CToolBar* pBar,
UINT nIDBar,
UINT nIDResource /* = 0 */,
DWORD dwStyle1 /* = WS_CHILD | WS_VISIBLE | CBRS_TOP */,
DWORD dwStyle2 /* = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC */ )
{
ASSERT_VALID( pParentFrame );
ASSERT_VALID( pBar );
// 1 - No IDResource means resource ID is same as bar ID
if( nIDResource == 0 )
{
nIDResource = nIDBar;
}
// 2 - Create toolbar and load resources
if( !pBar->Create( pParentFrame, dwStyle1, nIDBar ) ||
!pBar->LoadToolBar( nIDResource ) )
{
TRACE1( "Failed to create toolbar with ID=%d\n", nIDBar );
return FALSE; // fail to create
}
// 3 - Define toolbar styles
pBar->SetBarStyle( pBar->GetBarStyle() | dwStyle2 );
return TRUE;
}
///////////////////////////////////////////////////////////
// EkDockBarNextTo: function implementation
void EkDockBarNextTo( CControlBar* pNewBar,
CControlBar* pDockedBar,
UINT nDockBarID /* = AFX_IDW_DOCKBAR_TOP */ )
{
ASSERT_VALID( pDockedBar );
ASSERT_VALID( pNewBar );
// 1 - Find the frame where we will dock
CFrameWnd* pFrame = pDockedBar->GetDockingFrame();
// 2 - Force MFC to compute the positions
// of the docked control bar(s)
pFrame->RecalcLayout();
// 3 - Compute rectangle of already "docked" bar
CRect rect;
pDockedBar->GetWindowRect( &rect );
// 4 - Offset the rectangle slightly to the bottom right
// so that the new bar will dock either to the right or
// to the bottom of the existing bar (depending on the
// side where this last bar is already docked)
rect.OffsetRect(1,1);
// 5 - Dock new bar to specified position
pFrame->DockControlBar( pNewBar, nDockBarID, &rect );
}
///////////////////////////////////////////////////////////
// EkDockToolBar: function implementation
void EkDockToolBar( CFrameWnd* pParentFrame,
CToolBar* pBar,
UINT nDockBarID /* = AFX_IDW_DOCKBAR_TOP */,
CControlBar* pBarNextTo /* = NULL */,
DWORD dwStyleDocking /* = CBRS_ALIGN_ANY */ )
{
ASSERT_VALID( pParentFrame );
ASSERT_VALID( pBar );
ASSERT( ::IsWindow( pBar->GetSafeHwnd() ) );
// 1 - Define toolbar docking behavior
pBar->EnableDocking( dwStyleDocking );
if( pBarNextTo != NULL )
{
// 2a - Dock toolbar next to an existing bar
EkDockBarNextTo( pBar, pBarNextTo, nDockBarID );
}
else
{
// 2b - Dock toolbar on its own dockbar
pParentFrame->DockControlBar( pBar, nDockBarID );
}
}
///////////////////////////////////////////////////////////
// EkIsBarVisible: function implementation
BOOL EkIsBarVisible( CControlBar* pBar )
{
ASSERT_VALID( pBar );
return ( ( pBar->GetStyle() & WS_VISIBLE ) != 0 );
}
///////////////////////////////////////////////////////////
// EkSetToolBarButtonText: function implementation
void EkSetToolBarButtonText( CToolBar* pBar, UINT nIDStrings, TCHAR chSep /* = _T( '\n' ) */ )
{
ASSERT_VALID( pBar );
// 1 - Load string from resource
CString strButtons;
VERIFY( strButtons.LoadString( nIDStrings ) );
// 2 - Start at beginning of string
int nIndex = 0;
LPCTSTR pszStart = strButtons;
LPCTSTR pszEnd = NULL;
// 3 - Isolate each substring and set button text
do
{
// 4 - Find next separator
pszEnd = _tcschr( pszStart, chSep );
int nLen = ( pszEnd == NULL ) ?
_tcslen( pszStart ) : (int)( pszEnd - pszStart );
ASSERT( nLen >= 0 );
// 5 - Create CString from substring
CString strSubString;
memcpy( strSubString.GetBufferSetLength( nLen ),
pszStart, nLen * sizeof( TCHAR ) );
// 6 - Set button text, bump index
pBar->SetButtonText( nIndex++, strSubString );
// 7 - Skip separator
pszStart += nLen + 1;
}
while( pszEnd != NULL );
}
///////////////////////////////////////////////////////////
// EkSwitchBars: function implementation
void EkSwitchBars( CFrameWnd* pFrame,
CControlBar* pBar1,
CControlBar* pBar2,
BOOL bShowBar2,
BOOL bDelay /* = TRUE */ )
{
ASSERT_VALID( pFrame );
ASSERT_VALID( pBar1 );
ASSERT_VALID( pBar2 );
// 1 - Compute "From" and "To" bars
CControlBar* pBarFrom = bShowBar2 ? pBar1 : pBar2;
CControlBar* pBarTo = bShowBar2 ? pBar2 : pBar1;
BOOL bVisible = EkIsBarVisible( pBarFrom );
// 2 - Exchange "From" and "To" window IDs
UINT nIDFrom = ::GetWindowLong( pBarFrom->GetSafeHwnd(), GWL_ID );
UINT nIDTo = ::GetWindowLong( pBarTo->GetSafeHwnd(), GWL_ID );
::SetWindowLong( pBarFrom->GetSafeHwnd(), GWL_ID, nIDTo );
::SetWindowLong( pBarTo->GetSafeHwnd(), GWL_ID, nIDFrom );
// 3 - Hide "From" bar, show "To" bar if "From" bar was visible
pFrame->ShowControlBar( pBarFrom, FALSE, bDelay );
pFrame->ShowControlBar( pBarTo, bVisible, bDelay );
}
//=======================================================//
// Chapter 07: Menus //
//=======================================================//
///////////////////////////////////////////////////////////
// EkUpdateMenuUI: function implementation
void EkUpdateMenuUI( CWnd* pOwner, CMenu* pMenu, BOOL bAutoMenuEnable /* = TRUE */ )
{
// Checks the enabled/checked state of various menu items
// (adapted from MFC's own CFrameWnd::OnInitMenuPopup() function --
// WinFrm.cpp)
ASSERT_VALID( pOwner );
ASSERT( pMenu != NULL );
// Create and initialize the famous CCmdUI object
CCmdUI state;
state.m_pMenu = pMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// determine if menu is popup in top-level menu and set m_pOther to
// it if so (m_pParentMenu == NULL indicates that it is secondary popup)
HMENU hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu)
state.m_pParentMenu = pMenu; // parent == child for tracking popup
else if ((hParentMenu = ::GetMenu(pOwner->m_hWnd)) != NULL)
{
CWnd* pParent = pOwner->GetTopLevelParent();
// child windows don't have menus -- need to go to the top!
if (pParent != NULL &&
(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
{
int nIndexMax = ::GetMenuItemCount(hParentMenu);
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
{
if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu)
{
// when popup is found, m_pParentMenu is containing menu
state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax = pMenu->GetMenuItemCount();
// For each menu item...
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
state.m_nIndex++)
{
// Get menu item ID
state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // menu separator or invalid cmd - ignore it
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1)
{
// Maybe a popup menu, route to first item of
// that popup
state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);
if (state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1)
{
continue; // first item of popup can't be routed to
}
state.DoUpdate(pOwner, FALSE); // popups are never auto disabled
}
else
{
// Normal menu item:
// Auto enable/disable if 'bAutoMenuEnable' argument is set
// and command is _not_ a system command
state.m_pSubMenu = NULL;
state.DoUpdate(pOwner, bAutoMenuEnable && state.m_nID < 0xF000);
}
// adjust for menu deletions and additions
UINT nCount = pMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax)
{
state.m_nIndex -= (state.m_nIndexMax - nCount);
while (state.m_nIndex < nCount &&
pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}
//=======================================================//
// Chapter 08: Printing and print preview //
//=======================================================//
///////////////////////////////////////////////////////////
// CEkPrintingDlg: class implementation
CEkPrintingDlg::CEkPrintingDlg(CWnd* pParent, CWnd* pStandardDialog)
: CDialog(CEkPrintingDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEkPrintingDlg)
//}}AFX_DATA_INIT
ASSERT_VALID( pStandardDialog );
m_pStandardDialog = pStandardDialog;
// Create modeless dialog box
// (MFC automatically disables the application's
// main window while printing)
Create(CEkPrintingDlg::IDD, pParent);
}
void CEkPrintingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEkPrintingDlg)
DDX_Control(pDX, AFX_IDC_PRINT_DOCNAME, m_DocName);
DDX_Control(pDX, AFX_IDC_PRINT_PRINTERNAME, m_PrinterName);
DDX_Control(pDX, AFX_IDC_PRINT_PORTNAME, m_PortName);
DDX_Control(pDX, AFX_IDC_PRINT_PAGENUM, m_PageNum);
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEkPrintingDlg, CDialog)
//{{AFX_MSG_MAP(CEkPrintingDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CEkPrintingDlg::OnInitDialog()
{
SetWindowText( AfxGetAppName() );
CenterWindow();
return CDialog::OnInitDialog();
}
void CEkPrintingDlg::OnCancel()
{
ASSERT_VALID( m_pStandardDialog );
ASSERT( ::IsWindow( m_pStandardDialog->GetSafeHwnd() ) );
// Ask MFC's "Printing..." dialog to cancel
// the printing process
m_pStandardDialog->SendMessage( WM_COMMAND, IDCANCEL, 0 );
}
///////////////////////////////////////////////////////////
// EkChangePrintingOrientation: function implementation
void EkChangePrintingOrientation( CDC* pDC, CPrintInfo* pInfo, short NewOrientation )
{
ASSERT_VALID( pDC );
ASSERT( pInfo != NULL );
DEVMODE* pDevMode = pInfo->m_pPD->GetDevMode();
// Only change if new orientation is different
if( pDevMode->dmOrientation != NewOrientation )
{
pDevMode->dmOrientation = NewOrientation;
pDC->ResetDC( pDevMode );
}
}
///////////////////////////////////////////////////////////
// EkColorRefToGray: function implementation
COLORREF EkColorRefToGray( COLORREF crColor )
{
// Extract (r, g, b) factors from COLORREF
BYTE red = GetRValue( crColor );
BYTE green = GetGValue( crColor );
BYTE blue = GetBValue( crColor );
// Convert (r, g, b) to gray using standard formula
BYTE gray = static_cast< BYTE >( ( red*30 + green*59 + blue*11 ) / 100 );
return RGB( gray, gray, gray );
}
///////////////////////////////////////////////////////////
// EkCreateDefaultPrinterDC: function implementation
BOOL EkCreateDefaultPrinterDC( CDC* pDC )
{
ASSERT_VALID( pDC );
PRINTDLG pd;
AfxGetApp()->GetPrinterDeviceDefaults( &pd );
return AfxGetApp()->CreatePrinterDC( *pDC );
}
///////////////////////////////////////////////////////////
// EkGetPrintMode: function implementation
/* enum EK_PRINT_MODE { DISPLAYING, PRINTING, PREVIEWING }; */
EK_PRINT_MODE EkGetPrintMode( CPrintInfo* pInfo )
{
if( pInfo == NULL )
{
return DISPLAYING;
}
if( pInfo->m_bPreview )
{
return PREVIEWING;
}
return PRINTING;
}
EK_PRINT_MODE EkGetPrintMode( CDC* pDC )
{
ASSERT_VALID( pDC );
if( !pDC->IsPrinting() )
{
return DISPLAYING;
}
if( pDC->m_hDC != pDC->m_hAttribDC )
{
return PREVIEWING;
}
return PRINTING;
}
///////////////////////////////////////////////////////////
// EkIsMonoPreview: function implementation
BOOL EkIsMonoPreview( CDC* pDC )
{
ASSERT_VALID( pDC );
// Check for monochrome device context
BOOL bMono = ( pDC->GetDeviceCaps( NUMCOLORS ) == 2 );
// Check for print preview mode
BOOL bPreview = ( pDC->m_hDC != pDC->m_hAttribDC );
return( bMono && bPreview );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -