Skip to content

Commit

Permalink
文本框中输入的COLORREF和RGB值支持自动判断进制,支持10、8、16进制
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jul 11, 2020
1 parent 8b8a7cd commit 8665e17
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Binary file modified ColorPicker/ColorPicker.rc
Binary file not shown.
12 changes: 6 additions & 6 deletions ColorPicker/ColorPickerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ BOOL CColorPickerDlg::OnInitDialog()
SetColorBText();
SetColorHexText();

m_edit.SetLimitText(8);
m_edit.SetLimitText(10);
m_edit_r.SetLimitText(3);
m_edit_g.SetLimitText(3);
m_edit_b.SetLimitText(3);
m_edit_hex.SetLimitText(6);
m_edit_hex.SetLimitText(8);

m_color_list.SetDrawColorRow(CLC_COLOR);
m_color_list.LoadColors((theApp.GetModleDir() + CONFIG_FILE_NAME).c_str()); //从配置文件加载颜色表
Expand Down Expand Up @@ -364,7 +364,7 @@ void CColorPickerDlg::OnEnChangeColorValue()
{
CString str;
m_edit.GetWindowText(str);
m_color = _wtoi(str);
m_color = CCommon::StringToNumber(str);
m_color_r = GetRValue(m_color);
m_color_g = GetGValue(m_color);
m_color_b = GetBValue(m_color);
Expand All @@ -390,7 +390,7 @@ void CColorPickerDlg::OnEnChangeColorR()
{
CString str;
m_edit_r.GetWindowText(str);
m_color_r = _wtoi(str);
m_color_r = CCommon::StringToNumber(str);
m_color = RGB(m_color_r, m_color_g, m_color_b);
m_color_hex = CColorConvert::RGB2Hex(m_color_r, m_color_g, m_color_b);
SetColorRefText();
Expand All @@ -412,7 +412,7 @@ void CColorPickerDlg::OnEnChangeColorG()
{
CString str;
m_edit_g.GetWindowText(str);
m_color_g = _wtoi(str);
m_color_g = CCommon::StringToNumber(str);
m_color = RGB(m_color_r, m_color_g, m_color_b);
m_color_hex = CColorConvert::RGB2Hex(m_color_r, m_color_g, m_color_b);
SetColorRefText();
Expand All @@ -434,7 +434,7 @@ void CColorPickerDlg::OnEnChangeColorB()
{
CString str;
m_edit_b.GetWindowText(str);
m_color_b = _wtoi(str);
m_color_b = CCommon::StringToNumber(str);
m_color = RGB(m_color_r, m_color_g, m_color_b);
m_color_hex = CColorConvert::RGB2Hex(m_color_r, m_color_g, m_color_b);
SetColorRefText();
Expand Down
16 changes: 16 additions & 0 deletions ColorPicker/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ CString CCommon::LoadTextFormat(UINT id, const std::initializer_list<CVariant>&
str.LoadString(id);
return StringFormat(str.GetString(), paras);
}

unsigned int CCommon::StringToNumber(const CString& str)
{
//判断进制
int radix{ 10 };
if (str.GetLength() >= 3 && str[0] == _T('0') && (str[1] == _T('x') || str[1] == _T('X')))
{
radix = 16;
}
else if (str.GetLength() >= 2 && str[0] == _T('0'))
{
radix = 8;
}
TCHAR* end_ptr{};
return _tcstoul(str.GetString(), &end_ptr, radix);
}
3 changes: 3 additions & 0 deletions ColorPicker/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ class CCommon
//从资源文件中载入字符串,并将资源字符串中形如<%序号%>的字符串替换成可变参数列表中的参数
static CString LoadTextFormat(UINT id, const std::initializer_list<CVariant>& paras);

//将字符串转换成数字,支持自动识别十六进制、八进制和十进制
static unsigned int StringToNumber(const CString& str);

};

0 comments on commit 8665e17

Please sign in to comment.