c++ - COLORREF 和 D2D1::ColorF 之间的转换

标签 c++ mfc gdi+ direct2d

在我的 Windows 桌面应用程序开发中,GDI+ 和 Direct2D 都用于绘制矩形、直线、椭圆等。如何从 COLORREF(在 GDI+ 中用于画笔)转换为 D2D1::ColorF(在 Direct2D 中使用)对于画笔)反之亦然?

代码示例:

GDI+中,为了用特定颜色填充矩形,

PAINTSTRUCT ps;
HDC hdc;

hdc = BeginPaint(hWnd, &ps);
HBRUSH brush;

brush = ::CreateSolidBrush(*pColor);    //COLORREF* pColor;

::FillRect(hdc, &ps.rcPaint, brush);
::DeleteObject((HGDIOBJ) brush);

EndPaint(hWnd, &ps);

Direct2D中,绘制具有特定颜色的椭圆,

pRenderTarget->BeginDraw(); 

//type ID2D1SolidColorBrush - alpha value (4th parameter) is omitted
brush->SetColor(D2D1::ColorF(0.5f,0.5f,0.5f));

const D2D1_ELLIPSE circle1 = D2D1::Ellipse(
              D2D1::Point2F(pt1, pt2, 3.0f, 1.0f);

pRenderTarget->DrawEllipse(circle1, brush);

pRenderTarget->EndDraw();

最佳答案

两者均由三种颜色分量构建:R、G、B。

ColorF::ColorF :

ColorF(FLOAT, FLOAT, FLOAT, FLOAT)(
  FLOAT r,
  FLOAT g,
  FLOAT b,
  FLOAT a = 1.0
);

COLORREF :

To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively.

COLORREF RGB(
  BYTE byRed,
  BYTE byGreen,
  BYTE byBlue
);

具体来说,您可以使用 GetRValueGetGValueGetBValue 宏从 COLORREF 和然后在 ColorF 构造函数中使用它们。

关于c++ - COLORREF 和 D2D1::ColorF 之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241991/

相关文章:

C++ 迭代器问题

c++ - 如何为报告设置特定的打印机?

c++ - MFC - 示例编辑框阿拉伯语字符集

c++ - join() 上的 Boost 线程段错误

c++ - SRAND() 在代码块中不起作用?

C++ MFC如何绘制Alpha透明矩形

c - 在使用 GDI 的基于 C 的遗留 win32 系统中使用 GDI+

c++ - HBITMAP 可以包含 alpha channel 信息吗?

c++ - 像素级渲染的 GDI+ 性能

c++ - Intel one-api 编译器在编译 GDAL cpp 文件时出错