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

📄 findsheet.cpp

📁 < VC++编程宝典>>配套源代码, 对于想精通VC++的朋友很有帮助!
💻 CPP
字号:
// FindSheet.cpp : implementation file
//

#include "stdafx.h"
#include "ModalDemo.h"
#include "FindSheet.h"

#define DISABLED_TEXT " - Disabled"
#define RESULTS_TAB_INDEX 1
#define RESULTS_TAB_CAPTION "Results: %ld books found"

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

/////////////////////////////////////////////////////////////////////////////
// CFindSheet

IMPLEMENT_DYNAMIC(CFindSheet, CPropertySheet)

CFindSheet::CFindSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
}

CFindSheet::CFindSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
}

CFindSheet::~CFindSheet()
{
}


BEGIN_MESSAGE_MAP(CFindSheet, CPropertySheet)
	//{{AFX_MSG_MAP(CFindSheet)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindSheet message handlers
BOOL CFindSheet::OnInitDialog()
{
 BOOL bResult = CPropertySheet::OnInitDialog();

	// Remove the Apply Now and Cancel buttons
	int ids[] = {ID_APPLY_NOW, IDCANCEL};
	for (int i = 0; i < sizeof ids / sizeof ids[0]; i++) 
	{
		CWnd* pWnd = GetDlgItem(ids[i]);
		ASSERT(pWnd);
		if (pWnd) pWnd->ShowWindow(FALSE);
	}

	// Move the OK button
	CWnd* pbtnOk = GetDlgItem(IDOK);
	ASSERT(pbtnOk);

	CRect rectSheet;
	GetWindowRect(rectSheet);

	// get size of ok button
	CRect rectOkBtn;
	pbtnOk->GetWindowRect(rectOkBtn);

	// get border space between btn bottom and sheet bottom 
	int iBorder = rectSheet.bottom - rectOkBtn.bottom;

	// resize sheet
	rectSheet.right += rectOkBtn.Width() + iBorder;
	rectSheet.bottom = rectOkBtn.top;
	MoveWindow(rectSheet);

	// find first page
	CPropertyPage* pPage = GetPage(0);
	ASSERT(pPage);
	CRect rectPage;
	pPage->GetWindowRect(rectPage);

	// save width and height
	int cxOk = rectOkBtn.Width();
	int cyOk = rectOkBtn.Height();

	// move ok button
	rectOkBtn.top = rectPage.top;
	rectOkBtn.bottom = rectOkBtn.top + cyOk;
	rectOkBtn.left = rectSheet.right - 
		(cxOk + iBorder);
	rectOkBtn.right = rectOkBtn.left + cxOk;
	ScreenToClient(rectOkBtn);
	pbtnOk->MoveWindow(rectOkBtn);

	// Change the text of the OK button to "Close"
	CWnd* pWnd = GetDlgItem(IDOK);
	pWnd->SetWindowText(_T("Cl&ose"));

	// Set the fonts for all of the property sheet's tabs
	m_fontTab.CreateFont(-8, 0, 0, 0, FW_BOLD, 0, 0,
		0, 1, 0, 0, 0, 0, _T("MS Sans Serif"));	
	CTabCtrl* pTab = GetTabControl();
	ASSERT(pTab);
	if (pTab)
	{
		pTab->SetFont(&m_fontTab);
	}

	// Init Results tab
	SetResults(0);

	return bResult;
}

void CFindSheet::DisablePage(int iFirstPage, ...)
{
 int iPage = iFirstPage;
	
 va_list marker;
 va_start (marker, iFirstPage);
 int nArgs = 0;

 while (iPage != -1)
 {
  // add page to disabled page index array
  m_arrDisabledPages.Add(iPage);
		
  SetDisabledText(iPage);

  // Get next page index
  iPage = va_arg(marker, UINT);

  // the list MUST end with a -1!!!
  ASSERT(nArgs++ < 100);
 }
}

void CFindSheet::SetDisabledText(int iPage)
{
 CTabCtrl* pTab = GetTabControl();
 ASSERT(pTab);
 TC_ITEM ti;
 char szText[100];
 ti.mask = TCIF_TEXT;
 ti.pszText = szText;
 ti.cchTextMax = 100;
 VERIFY(pTab->GetItem(iPage, &ti));
 strcat(szText, DISABLED_TEXT);
 VERIFY(pTab->SetItem(iPage, &ti));
}

BOOL CFindSheet::OnNotify(WPARAM wParam,
 LPARAM lParam, LRESULT* pResult) 
{
 NMHDR* pnmh = (NMHDR*)lParam;
 ASSERT(pnmh);
 if (TCN_SELCHANGING == pnmh->code)
 {
  m_iLastActivePage = GetActiveIndex();
 }
 else if (TCN_SELCHANGE == pnmh->code)
 {
  int iCurrPage = GetActiveIndex();
  if (IsPageDisabled(iCurrPage))
  {
   PostMessage(PSM_SETCURSEL, m_iLastActivePage);
  }
 }

 return CPropertySheet::OnNotify(wParam,
  lParam, pResult);
}

BOOL CFindSheet::IsPageDisabled(int iPage)
{
 BOOL bFoundEntry = FALSE;
 int iSize = m_arrDisabledPages.GetSize();

 int i = 0;
 while (i < iSize && !bFoundEntry)
 {
  if (m_arrDisabledPages.GetAt(i) == (UINT)iPage) 
  {
   bFoundEntry = TRUE;
  }
  else
  {
   i++;
  }
 }

 return bFoundEntry;
}

void CFindSheet::EnablePage(int iPage)
{
 BOOL bFoundEntry = FALSE;
 int iSize = m_arrDisabledPages.GetSize();

 int i = 0;
 while (i < iSize && !bFoundEntry)
 {
  if (m_arrDisabledPages.GetAt(i) == (UINT)iPage)
  {
   bFoundEntry = TRUE;
  }
  else
  {
   i++;
  } 
 }

 if (bFoundEntry)
 {
  m_arrDisabledPages.RemoveAt(i);
  SetEnabledText(iPage);
 }
}

void CFindSheet::SetEnabledText(int iPage)
{
 CTabCtrl* pTab = GetTabControl();
 ASSERT(pTab);
 TC_ITEM ti;
 char szText[100];
 ti.mask = TCIF_TEXT;
 ti.pszText = szText;
 ti.cchTextMax = 100;
 VERIFY(pTab->GetItem(iPage, &ti));
 char* pFound = strstr(szText, DISABLED_TEXT);
 if (pFound)
 {
  *pFound = '\0';
  VERIFY(pTab->SetItem(iPage, &ti));
 }
}

void CFindSheet::SetResults(int nHits)
{
 SetResultsTabCaption(nHits);
 if (0 == nHits)
 {
  DisablePage(1, -1);
 }
 else
 {
  EnablePage(1);
 }
}

void CFindSheet::SetResultsTabCaption(int nHits)
{
 CTabCtrl* pTab = GetTabControl();
 ASSERT(pTab);
 TC_ITEM ti;
 char szText[100];
 ti.mask = TCIF_TEXT;
 ti.pszText = szText;
 ti.cchTextMax = 100;
 VERIFY(pTab->GetItem(RESULTS_TAB_INDEX, &ti));

 sprintf(szText, RESULTS_TAB_CAPTION, nHits);
 VERIFY(pTab->SetItem(RESULTS_TAB_INDEX, &ti));
}

BOOL CFindSheet::PreTranslateMessage(MSG* pMsg)
{
 BOOL bHandledMsg = FALSE;

 switch(pMsg->message)
 {
  case WM_SYSKEYDOWN:
  {
   // we only want 0-9 and letters
   if ((0x2f < pMsg->wParam)
   && (0x5b > pMsg->wParam))
   {
    CTabCtrl *pTab = GetTabControl();
    ASSERT(pTab);

    TC_ITEM ti;
    char szText[100];
    ti.mask = TCIF_TEXT;
    ti.pszText = szText;

    char szMnemonic[3];
    sprintf(szMnemonic, "&%c", pMsg->wParam);

    BOOL bFoundMatchingPage = FALSE;
    int iCurrPage = 0;
    while ((iCurrPage < pTab->GetItemCount())
    && (!bFoundMatchingPage))
    {
     ti.cchTextMax = 99;
     pTab->GetItem(iCurrPage, &ti);

     CString strText = szText;
     strText.MakeUpper();
     if (-1 != strText.Find(szMnemonic))
     {
      bFoundMatchingPage = TRUE;
      if (!IsPageDisabled(iCurrPage))
      {
       SetActivePage(iCurrPage);
       bHandledMsg = TRUE;
      }
     }
     else
     {
      iCurrPage++;
     }
    }
   }
  }
  break;

  default: break;
 }

 return (TRUE == bHandledMsg ? 
  TRUE : CPropertySheet::PreTranslateMessage(pMsg));
}

⌨️ 快捷键说明

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