我是靠谱客的博主 漂亮太阳,最近开发中收集的这篇文章主要介绍C++ DirectD2D:绘制简单图形rectangle、round rectangle、ellipse、triangle、circle、lineC++ DirectD2D前言一、头文件引用二、头文件声明三、绘制处理总结,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
C++ DirectD2D
第一章 绘制简单图形rectangle、round rectangle、ellipse、triangle、circle、line
文章目录
目录
C++ DirectD2D
文章目录
前言
一、头文件引用
二、头文件声明
三、绘制处理
总结
前言
左上角关注小编不迷路!QQ:1245688904
提示:以下是本篇文章正文内容,下面案例可供参考
一、头文件引用
#pragma comment (lib,"D2d1.lib")
#include <windows.h>
#include <d2d1.h>
#include <tchar.h>
二、头文件声明
我这只是简单处理了一下,封装做的并不是很好,只是为了练习。
struct WndClass {
LPCTSTR plClassName;
LPCTSTR lpWindowName;
UINT dwStyle;
INT x;
INT y;
INT nWidth;
INT nHeight;
} m_wndClass
{
_T("lesson001"),
_T("lesson001"),
WS_CAPTION | WS_VISIBLE | WS_SYSMENU,
100,
100,
800,
600
};
class MainApp
{
public:
ID2D1Factory* pD2D1Factory = NULL; //D2D工厂
ID2D1HwndRenderTarget* pRenderTarget = NULL;//渲染
ID2D1SolidColorBrush* pBlackBrush = NULL;//创建一个刷子
RECT rc;//Render area
HWND m_Hwnd;//window handler
HRESULT hr;
VOID CleanUp();
HRESULT OnRender();
};
三、绘制处理
Win32创建窗口步骤和D2D工厂构造:
MainApp mainApp;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsgId, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
switch (uMsgId)
{
case WM_CREATE:
{
}
break;
case WM_PAINT:
case WM_DISPLAYCHANGE:
{
BeginPaint(hWnd, &ps);
mainApp.OnRender();
EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
{
if (MessageBox(hWnd, _T("Do you want to quit?"), _T("tip"), MB_YESNO | MB_ICONQUESTION) == IDYES)
{
PostQuitMessage(0);
}
}
break;
case WM_DESTROY:
{
}
break;
case WM_QUIT:
{
}
break;
default:
return DefWindowProc(hWnd, uMsgId, wParam, lParam);
}
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
WNDCLASS wnd;
wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = m_wndClass.plClassName;
wnd.style = NULL;
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.cbClsExtra = NULL;
wnd.cbWndExtra = NULL;
wnd.lpszMenuName = NULL;
//注册窗口类
if (!RegisterClass(&wnd))
{
MessageBox(NULL, "Register window class is failed!", _T("tip"), MB_OK | MB_ICONERROR);
return 0;
}
//创建窗口
HWND hwnd = CreateWindow(
m_wndClass.plClassName,
m_wndClass.lpWindowName,
m_wndClass.dwStyle,
m_wndClass.x,
m_wndClass.y,
m_wndClass.nWidth,
m_wndClass.nHeight,
NULL,
NULL,
hInstance,
0
);
if (!hwnd)
{
MessageBox(NULL, "Create window is failed!", _T("tip"), MB_OK | MB_ICONERROR);
return 0;
}
mainApp.m_Hwnd = hwnd;
//构造 D2D工厂
mainApp.hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &mainApp.pD2D1Factory);
if (FAILED(mainApp.hr))
{
MessageBox(hwnd, "Create D2D factory failed!", _T("tip"), MB_OK | MB_ICONERROR);
return 0;
}
GetClientRect(mainApp.m_Hwnd, &mainApp.rc);
//创建渲染句柄
mainApp.hr = mainApp.pD2D1Factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
D2D1::SizeU(mainApp.rc.right - mainApp.rc.left, mainApp.rc.bottom - mainApp.rc.top)
),
&mainApp.pRenderTarget
);
if (FAILED(mainApp.hr))
{
MessageBox(hwnd, _T("Create Hwnd render target failed!"), _T("tip"), MB_OK | MB_ICONERROR);
return 0;
}
//创建笔刷
mainApp.hr = mainApp.pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red), &mainApp.pBlackBrush);
if (FAILED(mainApp.hr))
{
MessageBox(hwnd, _T("Create brush failed!"), _T("tip"), MB_OK | MB_ICONERROR);
return 0;
}
//展示窗口
ShowWindow(hwnd, SW_NORMAL);
//更新窗口
UpdateWindow(hwnd);
//派发消息
MSG msg{ 0 };
while (true)
{
if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
mainApp.CleanUp();
UnregisterClass(m_wndClass.plClassName, hInstance);
return 0;
}
HRESULT MainApp::OnRender()
{
//绘制图形
mainApp.pRenderTarget->BeginDraw();
//clear background color white
mainApp.pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
//draw rectangle
mainApp.pRenderTarget->DrawRectangle(D2D1::RectF(100.0f, 100.0f, 200.0f, 200.0f), mainApp.pBlackBrush);
// draw round rectangle
mainApp.pRenderTarget->DrawRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(300.0f, 300.0f, 400.0f, 400.0f), 30.0f, 30.0f), mainApp.pBlackBrush);
//draw ellipse
mainApp.pRenderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(150, 55), 50, 30), pBlackBrush);
//draw triangle.
D2D1_POINT_2F p0{30,10};
D2D1_POINT_2F p1{20,40};
D2D1_POINT_2F p2{40,40};
mainApp.pRenderTarget->DrawLine(p0, p1, pBlackBrush);
mainApp.pRenderTarget->DrawLine(p1, p2, pBlackBrush);
mainApp.pRenderTarget->DrawLine(p2, p0, pBlackBrush);
//draw Circle,same as radius
mainApp.pRenderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(150, 55), 50, 50), pBlackBrush);
//set line stronger
mainApp.pRenderTarget->DrawLine(D2D1_POINT_2F{ 10,7 }, D2D1_POINT_2F{ 100,7 }, pBlackBrush, 10);
mainApp.hr = mainApp.pRenderTarget->EndDraw();
if (FAILED(mainApp.hr))
{
MessageBox(mainApp.m_Hwnd, _T("Draw failed!"), _T("tip"), MB_OK | MB_ICONERROR);
}
//结束绘制
return mainApp.hr;
}
VOID MainApp::CleanUp() {
SAFE_RELEASE(pRenderTarget);
SAFE_RELEASE(pBlackBrush);
SAFE_RELEASE(pD2D1Factory);
}
总结
提示:以上就是今天要讲的内容,本文仅仅简单介绍了DirectD2D的使用,DirectD2D还有很多要学习掌握的东西,这点东西还远远不够呢。
最后
以上就是漂亮太阳为你收集整理的C++ DirectD2D:绘制简单图形rectangle、round rectangle、ellipse、triangle、circle、lineC++ DirectD2D前言一、头文件引用二、头文件声明三、绘制处理总结的全部内容,希望文章能够帮你解决C++ DirectD2D:绘制简单图形rectangle、round rectangle、ellipse、triangle、circle、lineC++ DirectD2D前言一、头文件引用二、头文件声明三、绘制处理总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复