在我的 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(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
);
具体来说,您可以使用 GetRValue
、GetGValue
和 GetBValue
宏从 COLORREF
和然后在 ColorF
构造函数中使用它们。
关于c++ - COLORREF 和 D2D1::ColorF 之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241991/