65.9K
CodeProject 正在变化。 阅读更多。
Home

使用新的 MFC7/ATL7 共享类。

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (4投票s)

2001年5月13日

2分钟阅读

viewsIcon

66990

downloadIcon

499

本示例演示了如何使用新的共享 ATL/MFC 类,例如 CPoint、CRect、CSize 和 CString

Sample Image - SharedClasses.gif

引言

从 Visual C++ .NET 7.0 开始,CPointCRectCSizeCStringCImage 类已被编写为独立于库。 这些类可以在 MFC 或 ATL 项目中使用,也可以在不使用这些库的项目中使用。

本文介绍了一个名为 SharedClasses 的应用程序,它是由 AppWizard 生成的 Win32 项目。 该应用程序处理鼠标输入并根据鼠标移动绘制一个椭圆。 共享的 MFC/ATL 类在整个应用程序中使用。

演示应用程序

要绘制椭圆,请按住鼠标左键(这将是椭圆的中心)并在按住按钮的同时移动鼠标。 窗口顶部的状态行显示中心坐标和椭圆的尺寸(宽度和高度)。

AppWizard 生成一个实现简单窗口的应用程序。 要添加新功能,我们必须修改窗口过程 (WndProc) 函数。

我们将使用 CStringCRect 类,因此要添加对它的支持,我们需要包含以下头文件

#include <atltypes.h>	// CPoint, CRect, CSize support.
#include <atlstr.h>	// CString support.

我们还需要包含 *Windowsx.h* 以使用 GET_X_LPARAMGET_Y_LPARAM 宏。

#include <Windowsx.h>	// GET_X_LPARAM and GET_Y_LPARAM defined there.

为了跟踪鼠标输入,让我们添加一个 CRect 类型的变量。

CRect rectEllipse;	// Rectangle to track mouse movement.

为了保存状态行文本,我们使用 CString 类。

CString strCoord;	// String to print ellipse coordinates and dimensions.

我们还需要一个标志来保存鼠标左键的状态。

bool bLBtnPressed = false;	// Flag to keep left mouse button state.

为了跟踪鼠标输入,我们处理三个窗口消息:WM_LBUTTONDOWNWM_LBUTTONUPWM_MOUSEMOVE

只需将以下代码添加到 WndProc 中的 switch 语句的末尾,就在 default 标签之前

case WM_LBUTTONDOWN:
	// Save beginning point.
	rectEllipse.left = GET_X_LPARAM(lParam); 
	rectEllipse.top = GET_Y_LPARAM(lParam);

	// Set flag that keeps mouse button state.
	bLBtnPressed = true;

	// Capture mouse input.
	::SetCapture(hWnd);
	break;

case WM_LBUTTONUP:
	// Reset flag that keeps mouse button state.
	bLBtnPressed = false;

	// Release mouse input.
	::ReleaseCapture();

	// Redraw window.
	::InvalidateRect(hWnd, NULL, TRUE);
	::UpdateWindow(hWnd);
	break;

case WM_MOUSEMOVE:
	// If mouse button is pressed update rectangle that
	// tracks mouse movement with new coordinates,
	// and redraw window.
	if(bLBtnPressed)
	{
		rectEllipse.right = GET_X_LPARAM(lParam); 
		rectEllipse.bottom = GET_Y_LPARAM(lParam);

		::InvalidateRect(hWnd, NULL, TRUE);
		::UpdateWindow(hWnd);
	}
	break;

WM_LBUTTONDOWN 消息处理程序中,我们存储用户按下鼠标左键的点的坐标。 这将是我们椭圆的中心。 我们设置存储鼠标按钮状态的标志并捕获鼠标输入,以便即使在窗口外部也能处理鼠标输入。

WM_MOUSEMOVE 消息处理程序检查鼠标按钮的状态。 如果按下,它将使用新的鼠标坐标更新我们的矩形(我们的 CRect 类对象)并重绘窗口。 如果未按下鼠标按钮,则不执行任何操作。

WM_LBUTTONUP 处理程序中,我们只更新保存鼠标按钮状态的标志并释放鼠标捕获。 在其中,它只是根据存储在 CRect 对象中的坐标绘制椭圆,并格式化和打印一个状态字符串以显示椭圆的信息,即中心坐标和尺寸。

为了绘制我们的椭圆,我们必须修改 WM_PAINT 消息处理程序

// Draw the ellipse using coordinates from mouse input.
::Ellipse(	hdc, 
		rectEllipse.left - rectEllipse.Width(),
		rectEllipse.top - rectEllipse.Height(),
		rectEllipse.left + rectEllipse.Width(),
		rectEllipse.top + rectEllipse.Height());

// If ellipse is not a dot or line, print center coordinates
// and dimensions. Otherwise print prompt to draw ellipse.
if(rectEllipse.Width() != 0 && rectEllipse.Height() != 0)
{
	strCoord.Format(_T("Center coordinates %d,%d     X-size %d, Y-Size %d"),
			rectEllipse.left,
			rectEllipse.top,
			2 * rectEllipse.Width(),
			2 * rectEllipse.Height());
}
else
	strCoord = _T("Use your mouse to draw ellipse in the window");
::TextOut(hdc, 5, 5 , strCoord, strCoord.GetLength());

历史

2001 年 10 月 16 日 - 更新了 VS beta 2 的文件

© . All rights reserved.