一、COLORREF與RGB的相互轉化
RGB(r,g,b)是一個宏
實際上它做得事是((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
rgb(r,g,b) = 一個整型值 = r + g * 256 + b*255*256
COLORREF 是 一 個 32-bit 整 型 數 值,它 代 表 了 一 種 顏 色。你 可以 使 用 RGB 函 數 來 初 始 化 COLORREF
它的定義
typedef DWORD COLORREF;
COLORREF變量有兩種賦值方法
第一種
COLORREF cf = RGB(,,);
第二種
if( colorDialog.DoModal() == IDOK )
{
color = colorDialog.GetColor();
}
如何從 COLORREF中取出RGB分量值?
可以使用宏GetRValue
GetGValue
GetBValue
他們的定義如下
#define GetRValue(rgb) ((BYTE)(rgb))
#define GetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
#define GetBValue(rgb) ((BYTE)((rgb)>>16))
二 、Color與ColorRef(int類型表示的顏色)的相互轉換
實際的軟件開發過程中,常需要用到非.net平臺的代碼。這時候就可能碰到ColorRef(也就是以int類型代表的顏色值或是以DWORD值表示的顏色)。這跟.net平臺下的顏色的相互轉換MS并沒有直接實現。那么就需要我們自己處理了。這里給出兩個函數。
Color GetArgbColor(int color)
{
int blue = color & 255;
int green = color >> 8 & 255;
int red = color >> 16 & 255 ;
return Color.FromArgb(blue, green, red);
}
三、 注意
}
////////////////////////////////////////////////////////////////////
COLORREF 和字符之間的轉換
一、格式化字符串的功能很強大
BOOL CDataManager::GetRGBText(std::string &strRGBText , COLORREF color)
{
//COLORREF col = RGB( 255 , 12 , 4);
BYTE Red = GetRValue(color); ///得到紅顏色
BYTE Green = GetGValue(color); ///得到綠顏色
BYTE Blue = GetBValue(color); ///得到蘭顏色
char chR[4];
itoa(Red ,chR , 10 );
char chG[4];
itoa(Green , chG , 10);
char chB[4];
itoa(Blue , chB , 10);
std::string strR , strG, strB;
strR = chR ;
strG = chG;
strB = chB;
strRGBText = strR + "," + strG + "," + strB;
return TRUE;
}
//字符串轉換為COLORREF,如("32","34","21")
BOOL CDataManager::GetColorRGB(CString strColorText , COLORREF& color)
{
char chR[4] = "", chG[4] = "", chB[4] = "";
sscanf( strColorText, "%[^,],%[^,],%[^,]", chR, chG, chB);
color = RGB(atoi(chR), atoi(chG), atoi(chB));
return TRUE;
}
///////////////////////////////////////////////////////
新聞熱點
疑難解答
圖片精選