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

📄 formctrl.cpp

📁 Visual C++ 实践与提高COM和COM+篇源代码. 对学习COM编程很有帮助.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	ASSERT(lpszResource != NULL);
	HINSTANCE hInst = AfxFindResourceHandle(lpszResource, RT_DIALOG);
	HRSRC hResource = ::FindResource(hInst, lpszResource, RT_DIALOG);
	if (hResource == NULL)
	{
		if (HIWORD(lpszResource) != 0)
			TRACE1("ERROR: Cannot find dialog template named '%s'.\n",
				lpszResource);
		else
			TRACE1("ERROR: Cannot find dialog template with IDD 0x%04X.\n",
				LOWORD((DWORD)lpszResource));
		return FALSE;
	}

	if (!bInvisibleChild)
		return TRUE;        // that's all we need to check

	// we must check that the dialog template is for an invisible child
	//  window that can be used for a form-view or dialog-bar
	HGLOBAL hTemplate = LoadResource(hInst, hResource);
	if (hTemplate == NULL)
	{
		TRACE0("Warning: LoadResource failed for dialog template.\n");
		// this is only a warning, the real call to CreateDialog will fail
		return TRUE;        // not a program error - just out of memory
	}
	DLGTEMPLATEEX* pTemplate = (DLGTEMPLATEEX*)LockResource(hTemplate);
	DWORD dwStyle;
	if (pTemplate->signature == 0xFFFF)
		dwStyle = pTemplate->style;
	else
		dwStyle = ((DLGTEMPLATE*)pTemplate)->style;
	UnlockResource(hTemplate);
	FreeResource(hTemplate);

	if (dwStyle & WS_VISIBLE)
	{
		if (HIWORD(lpszResource) != 0)
			TRACE1("ERROR: Dialog named '%s' must be invisible.\n",
				lpszResource);
		else
			TRACE1("ERROR: Dialog with IDD 0x%04X must be invisible.\n",
				LOWORD((DWORD)lpszResource));
		return FALSE;
	}
	if (!(dwStyle & WS_CHILD))
	{
		if (HIWORD(lpszResource) != 0)
			TRACE1("ERROR: Dialog named '%s' must have the child style.\n",
				lpszResource);
		else
			TRACE1("ERROR: Dialog with IDD 0x%04X must have the child style.\n",
				LOWORD((DWORD)lpszResource));
		return FALSE;
	}

	return TRUE;
}

//**************************************************************************
// Handles keyboard and mouse processing such as tabbing through the  
// form control.
BOOL CFormControl::PreTranslateMessage(MSG* pMsg)
{
	ASSERT(pMsg != NULL);
	CWnd*	pWndFocus = CWnd::GetFocus();
	BOOL	bDefault = TRUE;

	if (m_hWnd) {
		// If an OLE Control has the focus, then give it the first crack at key
		// and mouse messages.

		HWND hWndFocus = pWndFocus->GetSafeHwnd();
		UINT uMsg = pMsg->message;

		if (((uMsg >= WM_KEYFIRST) && (uMsg <= WM_KEYLAST)) ||
			((uMsg >= WM_MOUSEFIRST) && (uMsg <= WM_MOUSELAST)))
		{
			CWnd2* pWndCtrl = (CWnd2*)pWndFocus;

			// Walk up the parent chain, until we find an OLE control.
			while ((pWndCtrl != NULL) && (pWndCtrl->GetControlSite() == NULL) &&
				(pWndCtrl->GetParent() != this))
			{
				pWndCtrl = (CWnd2*)pWndCtrl->GetParent();
			}

			if ((pWndCtrl != NULL) && (pWndCtrl->GetControlSite() != NULL) &&
				(pWndCtrl->GetControlSite()->m_pActiveObject != NULL) &&
				(pWndCtrl->GetControlSite()->m_pActiveObject->TranslateAccelerator(
					pMsg) == S_OK))
			{
				return TRUE;
			}
		}

		HWND hFirstDlgItem = ::GetNextDlgTabItem(m_hWnd, NULL, FALSE);
		HWND hLastDlgItem = ::GetNextDlgTabItem(m_hWnd, hFirstDlgItem, TRUE);
		BOOL ShiftKey = ::GetKeyState(VK_SHIFT) < 0;
	
		// Check OLE controls for boundry conditions
		switch (uMsg)
		{
			case WM_KEYDOWN:
			{
				switch (LOWORD(pMsg->wParam))
				{
					case VK_LEFT:
					case VK_UP:
					{
						ShiftKey = TRUE;
					}

					case VK_TAB:
					case VK_RIGHT:
					case VK_DOWN:
					{
						if (ShiftKey) {
							// Are we at the first control on the form and moving up?
							if (hWndFocus == hFirstDlgItem) {
								CWnd* pCntr = GetOuterWindow()->GetParent();
								if (pCntr && pCntr->GetNextDlgTabItem(this, TRUE) != this) {
									bDefault = FALSE;
								}
							}
						// Are we at the last control on the form and moving down?
						}else if (hWndFocus == hLastDlgItem) {
							CWnd* pCntr = GetOuterWindow()->GetParent();
							if (pCntr && pCntr->GetNextDlgTabItem(this, FALSE) != this) {
								bDefault = FALSE;
							}
						}
					}
				}
			}
		}

		if (bDefault) {
			return PreTranslateInput(pMsg);
		}else{
			// Update the default push button's control ID.
			::SendMessage(m_hWnd,DM_SETDEFID, 0L, 0L);
			// Reset the current default push button to a regular button.
			UINT bn_style = (UINT)GetWindowLong(pWndFocus->m_hWnd, GWL_STYLE) & 0xff;
			if (bn_style & BS_DEFPUSHBUTTON) {
				::SendMessage(pWndFocus->m_hWnd, BM_SETSTYLE, 
							  MAKELONG(0,bn_style & ~BS_DEFPUSHBUTTON), (LPARAM)TRUE);
			}

			return FALSE;
		}
	}else{
		return FALSE;
	}
}

//**************************************************************************
BEGIN_MESSAGE_MAP(CFormControl, COleControl)
	//{{AFX_MSG_MAP(CFormControl)
	ON_WM_SETFOCUS()
	ON_MESSAGE(WM_ACTIVATE_CONTROL, OnActivateControl)
	ON_WM_MOUSEACTIVATE()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MBUTTONDBLCLK()
	ON_WM_MBUTTONDOWN()
	ON_WM_MBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
	// special command for Initial Update
	ON_MESSAGE_VOID(WM_INITIALUPDATE, OnInitialUpdate)
#ifndef _AFX_NO_OCC_SUPPORT
	ON_MESSAGE(WM_INITDIALOG, HandleInitDialog)
#endif
END_MESSAGE_MAP()

//**************************************************************************
// Reset the default button and set the focus to the appropriate child control
// on the form.
void CFormControl::OnSetFocus(CWnd* pOldWnd) 
{
	short ShiftState = ::GetKeyState(VK_SHIFT);
	short TabState = ::GetKeyState(VK_TAB);

	HWND hFirstDlgItem = ::GetNextDlgTabItem(m_hWnd, NULL, FALSE);
	HWND hLastDlgItem = ::GetNextDlgTabItem(m_hWnd, hFirstDlgItem, TRUE);

	// Set the focus to the appropriate child control in our form.
	if (TabState < 0) {
		if (ShiftState < 0) {
			m_hWndCurrentChild = hLastDlgItem;
		}else{
			m_hWndCurrentChild = hFirstDlgItem;
		}
	}else if ((::GetKeyState(VK_UP) < 0) || (GetKeyState(VK_LEFT) < 0)) {
		m_hWndCurrentChild = hLastDlgItem;
	}else if ((::GetKeyState(VK_DOWN) < 0) || (GetKeyState(VK_RIGHT) < 0)) {
		m_hWndCurrentChild = hFirstDlgItem;
	}else{
		m_hWndCurrentChild = ::GetFocus();
	}

	::SetFocus(m_hWndCurrentChild);
}

//**************************************************************************
// When a Child control gets the focus, it will send this message to 
// activate the Control.
LONG CFormControl::OnActivateControl(UINT, LONG) 
{
	if (!m_bUIActive)
	{
		OnActivateInPlace(TRUE, NULL);
		GetOuterWindow()->GetParent()->SendMessage(WM_ACTIVATE_CONTROL, 0, 0);
	}
	return 0;
}

//**************************************************************************
// Initialize the child controls.
void CFormControl::OnInitialUpdate()
{
	ASSERT_VALID(this);

	if (!UpdateData(FALSE))
		TRACE0("UpdateData failed during CFormControl initial update.\n");
}

//**************************************************************************
// Functions adding OLE control container support. Copied from MFC.
#ifndef _AFX_NO_OCC_SUPPORT

LRESULT CFormControl::HandleInitDialog(WPARAM, LPARAM)
{
	Default();  // allow default to initialize first (common dialogs/etc)

	// create OLE controls
	COccManager* pOccManager = afxOccManager;
	if ((pOccManager != NULL) && (m_pOccDialogInfo != NULL))
	{
		if (!pOccManager->CreateDlgControls(this, m_lpszTemplateName,
			m_pOccDialogInfo))
		{
			TRACE0("Warning: CreateDlgControls failed during form view init.\n");
			return FALSE;
		}
	}

	return TRUE;
}

BOOL CFormControl::SetOccDialogInfo(_AFX_OCC_DIALOG_INFO* pOccDialogInfo)
{
	m_pOccDialogInfo = pOccDialogInfo;
	return TRUE;
}

#endif //!_AFX_NO_OCC_SUPPORT


//**************************************************************************
// Remove the default flag fastBeginPaint so that the dialog template will 
// be allowed to draw itself.
DWORD CFormControl::GetControlFlags()
{
	return 0;
}

//**************************************************************************
// Draw a default design time representation of the control.
void CFormControl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	if (!m_hWnd) {
		CPen* pOldPen = (CPen*)pdc->SelectStockObject(BLACK_PEN);
		CString temp = AmbientDisplayName();
		if (!temp.IsEmpty()) {
			m_sFormName = temp;
		}

		// Initialize template dimensions.
		CRect rc(rcBounds.left,
				 rcBounds.top,
				 rcBounds.left + m_cTemplateSize.cx,
				 rcBounds.top + m_cTemplateSize.cy);
		
		if (m_bAutoSize) {
			pdc->DrawEdge(rc, EDGE_SUNKEN, BF_RECT);
		}else{
			// Draw the current boundries in solid black.
			pdc->Rectangle(rcBounds);

			// Draw the template dimensions as EDGE_SUNKEN.
			pdc->DrawEdge(rc, EDGE_SUNKEN, BF_RECT);
		}

		// Print out the name of the form.
		pdc->SetTextAlign(TA_LEFT | TA_TOP);
		pdc->ExtTextOut(rcBounds.left + 10, rcBounds.top + 10,
			ETO_CLIPPED, rcBounds, m_sFormName, m_sFormName.GetLength(), NULL);

		pdc->SelectObject(pOldPen);
	}
}

//**************************************************************************
// Disable Mouse messages in the FormControl itself. We want this to pretend to be 
// the background.
int CFormControl::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) 
{
	return MA_NOACTIVATE;
}

void CFormControl::OnMouseMove      (UINT nFlags, CPoint point) {}
void CFormControl::OnLButtonDblClk  (UINT nFlags, CPoint point) {}
void CFormControl::OnLButtonDown    (UINT nFlags, CPoint point) {}
void CFormControl::OnLButtonUp      (UINT nFlags, CPoint point) {}
void CFormControl::OnMButtonDblClk  (UINT nFlags, CPoint point) {}
void CFormControl::OnMButtonDown    (UINT nFlags, CPoint point) {}
void CFormControl::OnMButtonUp      (UINT nFlags, CPoint point) {}
void CFormControl::OnRButtonDblClk  (UINT nFlags, CPoint point) {}
void CFormControl::OnRButtonDown    (UINT nFlags, CPoint point) {}
void CFormControl::OnRButtonUp      (UINT nFlags, CPoint point) {}

⌨️ 快捷键说明

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