|
1 | 1 | #include "stdafx.h" |
2 | 2 | #include "EditControl.h" |
3 | 3 |
|
| 4 | + |
| 5 | +void UiElement::EditControl::Create(CWnd* parent) |
| 6 | +{ |
| 7 | + m_edit_ctrl = std::make_unique<CEdit>(); |
| 8 | + m_edit_ctrl->Create(WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE | WS_CLIPCHILDREN | ES_AUTOHSCROLL, GetRect(), parent, system_ctrl_id); |
| 9 | + system_ctrl_id++; |
| 10 | + m_edit_ctrl->SetFont(parent->GetFont()); |
| 11 | + m_edit_ctrl->ShowWindow(SW_SHOW); |
| 12 | +} |
| 13 | + |
4 | 14 | void UiElement::EditControl::Draw() |
5 | 15 | { |
6 | 16 | CalculateRect(); |
7 | 17 |
|
8 | | - if (m_edit_ctrl != nullptr && IsWindow(m_edit_ctrl->m_hWnd)) |
| 18 | + //DrawTextCtrl(); |
| 19 | + |
| 20 | + if (IsEditControlValid()) |
9 | 21 | { |
10 | 22 | m_edit_ctrl->SetWindowPos(nullptr, GetRect().left, GetRect().top, GetRect().Width(), GetRect().Height(), SWP_NOZORDER); |
11 | 23 | } |
12 | 24 | } |
13 | 25 |
|
14 | | -void UiElement::EditControl::Create(CWnd* parent) |
| 26 | +void UiElement::EditControl::DrawTextCtrl() |
15 | 27 | { |
16 | | - m_edit_ctrl = std::make_unique<CEdit>(); |
17 | | - m_edit_ctrl->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, GetRect(), parent, system_ctrl_id); |
18 | | - system_ctrl_id++; |
19 | | - m_edit_ctrl->SetFont(parent->GetFont()); |
| 28 | + //绘制背景 |
| 29 | + COLORREF back_color; |
| 30 | + if (hover) |
| 31 | + back_color = ui->GetUIColors().color_button_hover; |
| 32 | + else |
| 33 | + back_color = ui->GetUIColors().color_control_bar_back; |
| 34 | + ui->DrawRectangle(rect, back_color); |
| 35 | + //计算文本区域位置 |
| 36 | + CRect rect_text{ rect }; |
| 37 | + rect_text.left += ui->DPI(4); |
| 38 | + rect_text.right -= ui->DPI(4); |
| 39 | + //获取当前文本 |
| 40 | + CString text; |
| 41 | + if (IsEditControlValid()) |
| 42 | + m_edit_ctrl->GetWindowText(text); |
| 43 | + //绘制光标 |
| 44 | + if (is_edit && m_edit_ctrl != nullptr) |
| 45 | + { |
| 46 | + int text_height = ui->DPI(16); |
| 47 | + int start_char = 0; |
| 48 | + int end_char = 0; |
| 49 | + m_edit_ctrl->GetSel(start_char, end_char); |
| 50 | + int text_length = text.GetLength(); |
| 51 | + int start_width = GetTextWidthByPos(start_char, text); |
| 52 | + int end_width = GetTextWidthByPos(end_char, text); |
| 53 | + //绘制选择区域 |
| 54 | + if (start_char < end_char) |
| 55 | + { |
| 56 | + CRect rect_sel = rect; |
| 57 | + rect_sel.top = rect.top + (rect.Height() - text_height) / 2; |
| 58 | + rect_sel.bottom = rect_sel.top + text_height; |
| 59 | + rect_sel.left = rect_text.left + start_width; |
| 60 | + rect_sel.right = rect_text.left + end_width; |
| 61 | + rect_sel &= rect_text; |
| 62 | + COLORREF color_selection; |
| 63 | + if (theApp.m_app_setting_data.dark_mode) |
| 64 | + color_selection = theApp.m_app_setting_data.theme_color.dark1; |
| 65 | + else |
| 66 | + color_selection = theApp.m_app_setting_data.theme_color.light1_5; |
| 67 | + ui->GetDrawer().FillRect(rect_sel, color_selection); |
| 68 | + } |
| 69 | + |
| 70 | + CPoint point1; |
| 71 | + point1.x = rect_text.left + end_width; |
| 72 | + point1.y = rect.top + (rect.Height() - text_height) / 2; |
| 73 | + if (rect.PtInRect(point1)) |
| 74 | + { |
| 75 | + CPoint point2 = point1; |
| 76 | + point2.y += text_height; |
| 77 | + ui->GetDrawer().DrawLine(point1, point2, ui->GetUIColors().color_text, ui->DPI(1), false); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + //绘制文本 |
| 82 | + COLORREF text_color = ui->GetUIColors().color_text; |
| 83 | + out_of_bounds = false; |
| 84 | + ui->GetDrawer().DrawWindowText(rect_text, text.GetString(), text_color, Alignment::LEFT, true, false, false, &out_of_bounds); |
| 85 | +} |
| 86 | + |
| 87 | +bool UiElement::EditControl::LButtonUp(CPoint point) |
| 88 | +{ |
| 89 | + if (rect.PtInRect(point)) |
| 90 | + { |
| 91 | + //点击时显示关联的控件 |
| 92 | + if (rect.PtInRect(point)) |
| 93 | + { |
| 94 | + if (IsEditControlValid()) |
| 95 | + { |
| 96 | + m_edit_ctrl->SetWindowPos(nullptr, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER); |
| 97 | + is_edit = true; |
| 98 | + //m_edit_ctrl->ShowWindow(SW_SHOW); |
| 99 | + } |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + if (IsEditControlValid()) |
| 106 | + { |
| 107 | + is_edit = false; |
| 108 | + //m_edit_ctrl->ShowWindow(SW_HIDE); |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | +} |
| 113 | + |
| 114 | +bool UiElement::EditControl::LButtonDown(CPoint point) |
| 115 | +{ |
| 116 | + return false; |
| 117 | +} |
| 118 | + |
| 119 | +bool UiElement::EditControl::MouseMove(CPoint point) |
| 120 | +{ |
| 121 | + hover = rect.PtInRect(point); |
| 122 | + if (out_of_bounds && IsEditControlValid()) |
| 123 | + { |
| 124 | + if (!last_hover && hover) |
| 125 | + { |
| 126 | + CString tooltip_text; |
| 127 | + m_edit_ctrl->GetWindowText(tooltip_text); |
| 128 | + ui->UpdateMouseToolTip(TooltipIndex::TEXT_BLOCK, tooltip_text.GetString()); |
| 129 | + } |
| 130 | + |
| 131 | + if (hover) |
| 132 | + { |
| 133 | + ui->UpdateMouseToolTipPosition(TooltipIndex::TEXT_BLOCK, rect); |
| 134 | + } |
| 135 | + last_hover = hover; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + ui->UpdateMouseToolTipPosition(TooltipIndex::TEXT_BLOCK, CRect()); |
| 140 | + } |
| 141 | + |
| 142 | + return false; |
| 143 | +} |
| 144 | + |
| 145 | +bool UiElement::EditControl::MouseLeave() |
| 146 | +{ |
| 147 | + hover = false; |
| 148 | + return false; |
| 149 | +} |
| 150 | + |
| 151 | +bool UiElement::EditControl::SetCursor() |
| 152 | +{ |
| 153 | + if (hover && IsEditControlValid()) |
| 154 | + { |
| 155 | + ::SetCursor(::LoadCursor(NULL, IDC_IBEAM)); |
| 156 | + return true; |
| 157 | + } |
| 158 | + return false; |
| 159 | +} |
| 160 | + |
| 161 | +bool UiElement::EditControl::IsEditControlValid() |
| 162 | +{ |
| 163 | + return m_edit_ctrl != nullptr && IsWindow(m_edit_ctrl->m_hWnd); |
| 164 | +} |
| 165 | + |
| 166 | +int UiElement::EditControl::GetTextWidthByPos(int pos, const CString& text) |
| 167 | +{ |
| 168 | + if (pos <= 0) |
| 169 | + return 0; |
| 170 | + |
| 171 | + if (pos >= text.GetLength()) |
| 172 | + return ui->GetDrawer().GetTextExtent(text).cx; |
| 173 | + |
| 174 | + return ui->GetDrawer().GetTextExtent(text.Left(pos)).cx; |
20 | 175 | } |
0 commit comments