我是靠谱客的博主 俭朴猫咪,这篇文章主要介绍Direct2D 第1篇 最简单的D2D程序,现在分享给大家,希望可以做个参考。


编译之前,得先安装DirectX SDK



复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <windows.h> #include <d2d1.h> #include <d2d1helper.h> #include <dwrite.h> #pragma comment(lib, "dwrite.lib") #pragma comment(lib, "d2d1.lib") static HINSTANCE g_hinst; static HWND g_hwnd; static bool g_flag_app_exit; static ID2D1Factory * g_factory; static ID2D1HwndRenderTarget * g_render_target; void OnSize(LPARAM lparam) { if(g_render_target) g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam))); } bool AppInit() { D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory); RECT rc; GetClientRect(g_hwnd, &rc); g_factory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(g_hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top) ), &g_render_target); return true; } void OnPaint() { if(!g_render_target) return; g_render_target->BeginDraw(); g_render_target->Clear(D2D1::ColorF(0.63, 0.84, 0.00)); g_render_target->EndDraw(); } void AppExit() { g_render_target->Release(); g_factory->Release(); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_PAINT: OnPaint(); break; case WM_SIZE: OnSize(lparam); break; case WM_DESTROY: g_flag_app_exit = true; break; default: return DefWindowProc(hwnd, msg, wparam, lparam); } return 0; } int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG msg; memset(&wc,0,sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hinst; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } g_hinst = hinst; g_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Direct2D Demo",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hinst, NULL); if(g_hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } if(!AppInit()) { MessageBox(NULL, "Application Initialisation Failed !","Error",MB_ICONEXCLAMATION|MB_OK); return 0; } while(!g_flag_app_exit) { if(PeekMessage(&msg, g_hwnd, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } AppExit(); return msg.wParam; }


最后

以上就是俭朴猫咪最近收集整理的关于Direct2D 第1篇 最简单的D2D程序的全部内容,更多相关Direct2D内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部