举报

DWORD如何转化为COLORREF

0

我尝试用GetImageBuffer(NULL)获取绘图窗口的一个像素的DWORD值d,用BGR(d)作为setfillcolor的参数,但是绘制出来的填充颜色与那个像素点的颜色不一样,是为什么呢?应该怎么修改呢?

源代码如下:

#include<easyx.h>

const int _wid = 1280, _hei = 800;

int nPaletteX = _wid - 285, nPaletteY = 32, nPaletteWidth = 256, nPaletteHeight = 256;/*调色板的坐标以及宽高*/

bool fnIsAviField(int x, int y, int nXMin , int nXMax , int nYMin , int nYMax )
{
	return (x > nXMin&&x<nXMax&&y>nYMin&&y < nYMax);
}
void fnFlush(DWORD dValue,int nLength)
{
	int i;
	DWORD *dp = GetImageBuffer(NULL);
	for (i = 0; i < _wid*_hei; i++)
	{
		dp[i] = dValue;
		if (!(i%nLength))
			dValue++;
	}
}
bool fnDrawPalette(int x, int y, int nWidth, int nHeight, bool bIsUsed = false, DWORD dValue = RGB(256, 256, 256))
{
	DWORD *dp = GetImageBuffer(NULL), dColor = 0;
	int i, j;
	if (x<0 || y<0 || x>_wid || y>_hei)
		return false;
	dp += x + y * _wid;
	for (i = 0; i < nHeight; i++)
	{
		if (y + i >= _hei)
			break;
		for (j = 0; j < nWidth; j++)
		{
			if (j + x >= _wid)
				break;
			if (!bIsUsed)
				dp[j] = RGB(i, j, 256);
			else
				dp[j] = RGB(GetRValue(dValue), GetGValue(dValue), j);
		}
		dp += _wid;
	}
	return true;
}
int main(void)
{
	DWORD nGloColor;
	int i;
	ExMessage *stMsg=new ExMessage;
	initgraph(_wid,_hei);
	fnFlush(RGB(231, 177, 134), _wid*_hei/(16) );
	fnDrawPalette(nPaletteX, nPaletteY, nPaletteWidth, nPaletteHeight);
	while (1)
	{
		if (peekmessage(stMsg))
		{
			if (stMsg->message==WM_LBUTTONDOWN)
			{
				if (fnIsAviField(stMsg->x, stMsg->y, nPaletteX, nPaletteX + nPaletteWidth, nPaletteY, nPaletteY + nPaletteHeight))
				{
					nGloColor = GetImageBuffer(NULL)[stMsg->x + (stMsg->y - 1)*_wid];
					for (i = 0; i < 32; i++)
						fnDrawPalette(nPaletteX, 1.08*nPaletteHeight + nPaletteY + i, nPaletteWidth, 1, true, nGloColor);
				}
				else if (fnIsAviField(stMsg->x, stMsg->y, nPaletteX, 1.08*nPaletteHeight + nPaletteY, nPaletteWidth, 32))
				{
					nGloColor = GetImageBuffer(NULL)[stMsg->x + (stMsg->y - 1)*_wid];
					Sleep(1024);
				}
				else
				{
					//setfillcolor(BGR(GetImageBuffer(NULL)[stMsg->y*_wid + stMsg->x]));
					setfillcolor(BGR(nGloColor));
					fillcircle(stMsg->x, stMsg->y, 32);
				}
				
			}
			flushmessage();
		}
	}
	delete stMsg;
	closegraph();
	return 0;
}
ava
子非鱼

2023-7-6

举报
0

如你果绘出制的变渐图案是:

由红到暗红。应该把,写入缓冲区绘图的 RGB 换成 BGR,因为冲区绘图是反相的,不使能用 RGB 去给颜色,得使用 BGR 反着来,颜色才是正确的。

原因解译与参考链接:https://codebus.cn/yangw/high-speed-drawing-with-direct-operation-of-display-buffers

修后改果效图截:

 https://b23.tv/K08z7Wr

下面是修后改的代码:

#include<easyx.h>

const int _wid = 1280, _hei = 800;

int nPaletteX = _wid - 285, nPaletteY = 32, nPaletteWidth = 256, nPaletteHeight = 256;/*调色板的坐标以及宽高*/

bool fnIsAviField(int x, int y, int nXMin, int nXMax, int nYMin, int nYMax)
{
	return (x > nXMin && x<nXMax&& y>nYMin && y < nYMax);
}
void fnFlush(DWORD dValue, int nLength)
{
	int i;
	DWORD* dp = GetImageBuffer(NULL);
	for (i = 0; i < _wid * _hei; i++)
	{
		dp[i] = dValue;
		if (!(i % nLength))
			dValue++;
	}
}
bool fnDrawPalette(int x, int y, int nWidth, int nHeight, bool bIsUsed = false, DWORD dValue = RGB(256, 256, 256))
{
	DWORD* dp = GetImageBuffer(NULL), dColor = 0;
	int i, j;
	if (x<0 || y<0 || x>_wid || y>_hei)
		return false;
	dp += x + y * _wid;
	for (i = 0; i < nHeight; i++)
	{
		if (y + i >= _hei)
			break;
		for (j = 0; j < nWidth; j++)
		{
			if (j + x >= _wid)
				break;
			if (!bIsUsed)
				dp[j] = BGR(i, j, 256);
			else
				dp[j] = BGR(GetRValue(dValue), GetGValue(dValue), j);
		}
		dp += _wid;
	}
	return true;
}
int main(void)
{
	
	int i;
	ExMessage stMsg ;
	initgraph(_wid, _hei);
	DWORD nGloColor=RGB(45,45,45) ;
	fnFlush(RGB(231, 177, 134), _wid * _hei / (16));
	fnDrawPalette(nPaletteX, nPaletteY, nPaletteWidth, nPaletteHeight);
	flushmessage();
	while (1)
	{
		if (peekmessage(&stMsg,EX_MOUSE,TRUE))
		{
			if (stMsg.message == WM_LBUTTONDOWN)
			{
				if (fnIsAviField(stMsg.x, stMsg.y, nPaletteX, nPaletteX + nPaletteWidth, nPaletteY, nPaletteY + nPaletteHeight))
				{
					nGloColor = GetImageBuffer(NULL)[stMsg.x + (stMsg.y - 1) * _wid];
					for (i = 0; i < 32; i++)
						fnDrawPalette(nPaletteX, 1.08 * nPaletteHeight + nPaletteY + i, nPaletteWidth, 1, true, BGR(nGloColor));
				}
				else if (fnIsAviField(stMsg.x, stMsg.y, nPaletteX, 1.08 * nPaletteHeight + nPaletteY, nPaletteWidth, 32))
				{
					nGloColor = GetImageBuffer(NULL)[stMsg.x + (stMsg.y - 1) * _wid];
					Sleep(1024);
				}
				else
				{
					//setfillcolor(BGR(GetImageBuffer(NULL)[stMsg.y*_wid + stMsg.x]));
					setfillcolor(BGR(nGloColor));
					if (nGloColor != 0)fillcircle(stMsg.x, stMsg.y, 32);
				}

			}
			Sleep(5);
			
		}
	}
	
	closegraph();
	return 0;
}
举报
0

补充一个能用来的测试的代码吧。

ava
慢羊羊

2023-7-6

技术讨论社区