Direct2D 第4篇 渐变画刷
#include <windows.h>
#include <d2d1.h>
#include <d2d1helper.h>
#include <dwrite.h>
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "d2d1.lib")
HINSTANCE g_hinst;
HWND g_hwnd;
ID2D1Factory * g_factory;
ID2D1HwndRenderTarget * g_render_target;
ID2D1SolidColorBrush * g_brush;
IDWriteFactory * g_write_factory;
IDWriteTextFormat * g_text_format;
ID2D1GradientStopCollection * g_gradient_stop_collection;
ID2D1LinearGradientBrush * g_linear_gradient_brush;
ID2D1RadialGradientBrush * g_radial_gradient_brush;
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);
g_render_target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::WhiteSmoke), &g_brush);
// Init Font
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,__uuidof(g_write_factory),reinterpret_cast<IUnknown **>(&g_write_factory));
g_write_factory->CreateTextFormat(L"Arial Block", NULL, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 40, L"",&g_text_format);
// Create Gradient Stops
D2D1_GRADIENT_STOP gradient_stops[3];
gradient_stops[0].color = D2D1::ColorF(D2D1::ColorF::YellowGreen);
gradient_stops[0].position = 0.0f;
gradient_stops[1].color = D2D1::ColorF(D2D1::ColorF::ForestGreen);
gradient_stops[1].position = 0.5f;
gradient_stops[2].color = D2D1::ColorF(D2D1::ColorF::Green);
gradient_stops[2].position = 1.0f;
// Create Interface
g_render_target->CreateGradientStopCollection(gradient_stops, 3, &g_gradient_stop_collection);
// Create Linear Gradient Brush
g_render_target->CreateLinearGradientBrush(
D2D1::LinearGradientBrushProperties(D2D1::Point2F(0,0), D2D1::Point2F(600,600)),
g_gradient_stop_collection, &g_linear_gradient_brush);
g_render_target->CreateRadialGradientBrush(
D2D1::RadialGradientBrushProperties(D2D1::Point2F(500,500),D2D1::Point2F(),1000.0f,1000.0f),
g_gradient_stop_collection,&g_radial_gradient_brush);
return true;
}
void OnSize(LPARAM lparam)
{
if(g_render_target)
g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam)));
}
void OnPaint()
{
if(!g_render_target)
return;
g_render_target->BeginDraw();
// Clear Background
g_render_target->Clear(D2D1::ColorF(0.63, 0.84, 0.00));
// Draw Ellipse
D2D1_SIZE_F size = g_render_target->GetSize();
D2D1_ELLIPSE ellipse;
ellipse.point = D2D1::Point2F(size.width/2.0, size.height/2.0);
ellipse.radiusX = 280;
ellipse.radiusY = 100;
g_render_target->FillEllipse(&ellipse, g_radial_gradient_brush);
//g_render_target->FillEllipse(&ellipse, g_linear_gradient_brush);
// Draw Text
const wchar_t * text = L"Direct2D Gradient Brush";
g_render_target->DrawText(text, wcslen(text),
g_text_format,
D2D1::RectF(100, 190, size.width, size.height),
g_brush);
g_render_target->EndDraw();
}
void OnDestroy()
{
g_linear_gradient_brush->Release();
g_radial_gradient_brush->Release();
g_gradient_stop_collection->Release();
g_text_format->Release();
g_write_factory->Release();
g_brush->Release();
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:
OnDestroy();
PostQuitMessage(0);
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(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Direct2D 第4篇 渐变画刷的更多相关文章
- WPF中线性渐变画刷的一个小窍门
最近被项目里面控件的设计搞的死去活来的,大部分的设计都会需要使用进度条的功能,因为UI形状的变态,使用ProgressBar不能满足需求,没办法就自己想办法实现进度显示.折腾的多了发现一个很不错的方法 ...
- WPF线性渐变画刷应用之——炫彩线条
效果图: Xaml代码: <Rectangle Width="800" Height="10"> <Rectangle.Fill> &l ...
- (C#)WPF:LinearGradientBrush 线性渐变画刷和RadialGradientBrush 圆形渐变画刷
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...
- 简述WPF中的画刷(Brush)
原文:简述WPF中的画刷(Brush) -------------------------------------------------------------------------------- ...
- WPF样式之画刷结合样式
第一种画刷,渐变画刷GradientBrush (拿线性渐变画刷LinearGradientBrush(其实它涵盖在GradientBrush画刷内.现在拿他来说事.),还有一个圆心渐变画刷Radia ...
- C#GDI+基础(三)画刷详解
SolidBrush:一般的画刷,通常只用一种颜色去填充GDI+图形 创建一般画刷: SolidBrush sbBrush1 = new SolidBrush(Color.Green); HatchB ...
- 《WPF程序设计指南》读书笔记——第2章 基本画刷
1.Color结构 using System; using System.Windows; using System.Windows.Input; using System.Windows.Media ...
- Windows Phone开发(31):画刷
原文:Windows Phone开发(31):画刷 画刷是啥玩意儿?哈,其实画刷是用来涂鸦,真的,没骗你,至于你信不信,反正我信了. 本文通过价绍几个典型的画刷,使你明白画刷就是用来涂鸦的. 一.纯色 ...
- Sliverlight之 画刷
1,5种画刷 (见Project15) (1)TextBlock控件中的Forground和BackGround属性是一个什么对象?它在前台的完整的写法是什么?(实际是.net做了一个转换,可以直接写 ...
随机推荐
- jquery移除元素某个属性
removeAttr() 方法从被选元素中移除属性. 例如:$("p").removeAttr("style");
- hdfs写并发问题
hdfs文件写入不支持多个进程同时写入一个文件,每次只能一个FS挟持对象的人写入
- HZOI20190829模拟33题解
题面:https://www.cnblogs.com/Juve/articles/11436771.html A:春思 我们对a分解质因数,则$a=\prod\limits_p^{p|a}p^k$ 所 ...
- 再谈MFC学习——模态对话框的数据传递
学习c++的目的就是为了使用封装好的类.而最最经典的封装类当然是微软的MFC窗体程序类. 学习MFC编程之前要学习c++的主要知识,掌握c++的基本编程思想. 以下就看下我学习的MFC模态对话框的数据 ...
- 简单linux查询
1查看日志异常 tailf nohup.out|grep ERROR -A 3 --color tailf nohup.out|grep ERROR|grep chunk -A 3 -B 3 -- ...
- HDU 3086 马拉车模板
模板,但是对这个算法还是不太清楚,真实不明觉厉.... #include <iostream> #include <cstdio> #include <string.h& ...
- map 的使用 UVA156
借用这一水题来熟悉map的操作,以前以为看过c++ primer,对map虽然不熟悉但用还是会用的,实际上手发现不是那么回事,还是有很多坑的 #include <iostream> #in ...
- Django项目:CRM(客户关系管理系统)--48--39PerfectCRM实现登录+验证码+过期时间+页面保留账号
# gbacc_urls.py # ————————38PerfectCRM实现全局账号登录注销———————— from django.conf.urls import url from gbacc ...
- iview 小问题笔记
总结一下用 iview 组件期间遇到的小问题.一边做项目一边做笔记. 官方文档:iview API 按需引入: 1,iview 分页组件 <Page :total="aboutPage ...
- mysql日常sql
重置表 truncate table david_account; 触发器 /* 商户资料更新时更新终端 */ DELIMITER | CREATE TRIGGER bankChange AFTER ...