溫馨提示×

C++ COLORREF與字符串互轉怎么實現

c++
小億
120
2023-10-24 13:18:13
欄目: 編程語言

要將COLORREF轉換為字符串,您可以使用sprintf函數來實現。以下是一個示例代碼:

COLORREF color = RGB(255, 0, 0); // 紅色

char strColor[9];
sprintf(strColor, "#%02X%02X%02X", GetRValue(color), GetGValue(color), GetBValue(color));

std::string strColorString = strColor;

std::cout << strColorString << std::endl; // 輸出字符串 "#FF0000"

要將字符串轉換為COLORREF,您可以使用sscanf函數來實現。以下是一個示例代碼:

std::string strColorString = "#FF0000";

int red, green, blue;
sscanf(strColorString.c_str(), "#%02X%02X%02X", &red, &green, &blue);

COLORREF color = RGB(red, green, blue);

std::cout << std::hex << color << std::endl; // 輸出16進制表示的COLORREF值 0x000000FF

請注意,這些示例代碼假設您正在使用Windows API的COLORREF類型,并且帶有GetRValue、GetGValue和GetBValue等函數。如果您使用的是其他圖形庫或平臺,請適當調整代碼。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女