【发布时间】:2014-04-10 12:04:37
【问题描述】:
简介及相关资料:
我正在使用GradientFill API 来绘制 2 个渐变三角形。
这是我写的辅助函数:
void GradientTriangle( HDC MemDC,
LONG x1, LONG y1,
LONG x2, LONG y2,
LONG x3, LONG y3,
COLORREF top, COLORREF bottom )
{
TRIVERTEX vertex[3];
vertex[0].x = x1;
vertex[0].y = y1;
vertex[0].Red = GetRValue(bottom) << 8;
vertex[0].Green = GetGValue(bottom) << 8;
vertex[0].Blue = GetBValue(bottom) << 8;
vertex[0].Alpha = 0x0000;
vertex[1].x = x3;
vertex[1].y = y3;
vertex[1].Red = GetRValue(bottom) << 8;
vertex[1].Green = GetGValue(bottom) << 8;
vertex[1].Blue = GetBValue(bottom) << 8;
vertex[1].Alpha = 0x0000;
vertex[2].x = x2;
vertex[2].y = y2;
vertex[2].Red = GetRValue(top) << 8;
vertex[2].Green = GetGValue(top) << 8;
vertex[2].Blue = GetBValue(top) << 8;
vertex[2].Alpha = 0x0000;
// Create a GRADIENT_TRIANGLE structure that
// references the TRIVERTEX vertices.
GRADIENT_TRIANGLE gTriangle;
gTriangle.Vertex1 = 0;
gTriangle.Vertex2 = 1;
gTriangle.Vertex3 = 2;
// Draw a shaded triangle.
GradientFill( MemDC, vertex, 3, &gTriangle, 1, GRADIENT_FILL_TRIANGLE);
}
问题:
我在Windows XP 上遇到了一个问题,当我将窗口移动到最左侧时,我得到了图形伪影,因此它的一部分看不见了。
为了重现问题,这里是制作SSCCE的说明:
在 Visual Studio 中创建一个默认的 Win32 项目。
添加上述辅助函数。
-
添加以下
WM_ERASEBKGND处理程序:案例 WM_ERASEBKGND: { 矩形 r; GetClientRect(hwnd, &r);
// bottom triangle --> need help with this one!! GradientTriangle( (HDC)wParam, r.left, r.top, r.left, r.bottom - r.top, r.right, r.bottom - r.top, RGB( 0x0, 0x0, 0xFF ), RGB( 0xFF, 0xFF, 0x0 ) ); // top triangle --> this one is fine ! GradientTriangle( (HDC)wParam, r.right, r.bottom - r.top, r.right, r.top, r.left, r.top, RGB( 0xFF, 0x0, 0x0 ), RGB( 0x0, 0xFF, 0x0 ) ); } return 1L;
当程序启动时,你应该得到这个:
在您将窗口向左移动很远(因此它的一部分“离开屏幕”变得不可见)然后将其移回后,您应该会得到:
我为解决这个问题所做的努力:
按照my previous similar question 的回答中的说明,我能够通过切换传递给辅助函数的坐标顺序来解决工件的问题。
不过,底部三角形的颜色已经改变,我需要帮助修改RGB 参数以使其正确。
这是我的解决方法:
// I have modified parametters for bottom triangle
GradientTriangle( (HDC)wParam,
r.left, r.top,
r.right, r.bottom - r.top, // switched places
r.left, r.bottom - r.top, // of these two coordinates
RGB( 0x0, 0x0, 0xFF ), RGB( 0xFF, 0xFF, 0x0 ) ); // but color has changed
正如我所说,伪影消失了,但结果颜色发生了变化:
问题:
如何修改传递给辅助函数的RGB 参数,以便获得正确的渐变颜色?
谢谢。
最好的问候。
【问题讨论】:
-
您似乎想为 WM_ERASEBKGND 返回一个非零值以忽略它,并首先在 WM_PAINT 中绘制背景。