<sentencetext xmlns="http://www.w3.org/1999/xhtml">此演练演示如何完成以下这些任务:</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">创建 MFC 应用程序。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">创建一个纯色画笔和一个线性渐变画笔。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">修改渐变画笔,以便在调整窗口大小时它会相应更改。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">实现 D2D 绘图处理程序。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">验证结果。</sentencetext>
说明 |
---|
对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置。 |
创建 MFC 应用程序
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“文件”</span>菜单上指向<span class="label" xmlns="http://www.w3.org/1999/xhtml">“新建”</span>,然后单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“项目”</span>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“新建项目”</span>对话框左窗格的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“已安装的模板”</span>下,展开<span class="label" xmlns="http://www.w3.org/1999/xhtml">“Visual C++”</span>,然后选择<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFC”</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">在中间窗格中,选择<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFC 应用程序”</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“名称”</span>框中键入 <span class="input" xmlns="http://www.w3.org/1999/xhtml">MFCD2DWalkthrough</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“确定”</span>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFC 应用程序向导”</span>中,单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“完成”</span>而不更改任何设置。</sentencetext>
创建一个纯色画笔和一个线性渐变画笔
-
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“解决方案资源管理器”</span>中的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFCD2DWalkthrough”</span>项目的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“头文件”</span>文件夹中,打开 MFCD2DWalkthroughView.h。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">将以下代码添加到 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong> 类以创建三个数据变量。</sentencetext>
CD2DTextFormat* m_pTextFormat; CD2DSolidColorBrush* m_pBlackBrush; CD2DLinearGradientBrush* m_pLinearGradientBrush;
<sentencetext xmlns="http://www.w3.org/1999/xhtml">保存文件并将其关闭。</sentencetext>
-
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“源文件”</span>文件夹中,打开 MFCD2DWalkthroughView.cpp。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">在 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong> 类的构造函数中,添加以下代码。</sentencetext>
// Enable D2D support for this window: EnableD2DSupport(); // Initialize D2D resources: m_pBlackBrush = new CD2DSolidColorBrush(GetRenderTarget(), D2D1::ColorF(D2D1::ColorF::Black)); m_pTextFormat = new CD2DTextFormat(GetRenderTarget(), _T("Verdana"), 50); m_pTextFormat->Get()->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); m_pTextFormat->Get()->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); D2D1_GRADIENT_STOP gradientStops[2]; gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::White); gradientStops[0].position = 0.f; gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::Indigo); gradientStops[1].position = 1.f; m_pLinearGradientBrush = new CD2DLinearGradientBrush(GetRenderTarget(), gradientStops, ARRAYSIZE(gradientStops), D2D1::LinearGradientBrushProperties(D2D1::Point2F(0, 0), D2D1::Point2F(0, 0)));
<sentencetext xmlns="http://www.w3.org/1999/xhtml">保存文件并将其关闭。</sentencetext>
修改渐变画笔,以便在调整窗口大小时它会相应更改
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“项目”</span>菜单上单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“类向导”</span>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFC 类向导”</span>中的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“类名”</span>下,选择 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“消息”</span>选项卡上的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“消息”</span>框中,选择 <span xmlns="http://www.w3.org/1999/xhtml"><span class="input">WM_SIZE</span></span>,然后单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“添加处理程序”</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">此操作将 <strong xmlns="http://www.w3.org/1999/xhtml">OnSize</strong> 消息处理程序添加到 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong> 类。</sentencetext>
-
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“现有处理程序”</span>框中,选择 <strong xmlns="http://www.w3.org/1999/xhtml">OnSize</strong>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“编辑代码”</span>以显示 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView::OnSize</strong> 方法。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">在此方法的末尾,添加以下代码。</sentencetext>
m_pLinearGradientBrush->SetEndPoint(CPoint(cx, cy));
<sentencetext xmlns="http://www.w3.org/1999/xhtml">保存文件并将其关闭。</sentencetext>
实现 D2D 绘图处理程序
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“项目”</span>菜单上单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“类向导”</span>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“MFC 类向导”</span>中的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“类名”</span>下,选择 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“消息”</span>选项卡上,单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“添加自定义消息”</span>。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“添加自定义消息”</span>对话框的<span class="label" xmlns="http://www.w3.org/1999/xhtml">“自定义 Windows 消息”</span>框中,键入 <span class="input" xmlns="http://www.w3.org/1999/xhtml">AFX_WM_DRAW2D</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“消息处理程序名称”</span>框中,键入 <span class="input" xmlns="http://www.w3.org/1999/xhtml">OnDraw2D</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">选择<span class="label" xmlns="http://www.w3.org/1999/xhtml">“已注册的消息”</span>选项,然后单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“确定”</span>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">此操作将 <span xmlns="http://www.w3.org/1999/xhtml"><span class="input">AFX_WM_DRAW2D</span></span> 消息的消息处理程序添加到 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView</strong> 类。</sentencetext>
-
<sentencetext xmlns="http://www.w3.org/1999/xhtml">在<span class="label" xmlns="http://www.w3.org/1999/xhtml">“现有处理程序”</span>框中,选择 <strong xmlns="http://www.w3.org/1999/xhtml">OnDraw2D</strong>。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">单击<span class="label" xmlns="http://www.w3.org/1999/xhtml">“编辑代码”</span>以显示 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView::OnDraw2D</strong> 方法。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">对 <strong xmlns="http://www.w3.org/1999/xhtml">CMFCD2DWalkthroughView::OnDrawD2D</strong> 方法使用以下代码。</sentencetext>
afx_msg LRESULT CMFCD2DWalkthroughView::OnDraw2D(WPARAM wParam, LPARAM lParam) { CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam; ASSERT_VALID(pRenderTarget); CRect rect; GetClientRect(rect); pRenderTarget->FillRectangle(rect, m_pLinearGradientBrush); pRenderTarget->DrawText(_T("Hello, World!"), rect, m_pBlackBrush, m_pTextFormat); return TRUE; }
<sentencetext xmlns="http://www.w3.org/1999/xhtml">保存文件并将其关闭。</sentencetext>
验证结果
<sentencetext xmlns="http://www.w3.org/1999/xhtml">生成并运行应用程序。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">它应有一个在您调整窗口大小时更改的渐变矩形。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">“Hello World!”应显示在矩形中央。</sentencetext>