-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomDrawCombox.cpp
More file actions
201 lines (173 loc) · 10.4 KB
/
Copy pathCustomDrawCombox.cpp
File metadata and controls
201 lines (173 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// CustomDrawCombox.cpp : 实现文件
//
#include "pch.h"
#include "CustomDrawCombox.h"
// CCustomDrawCombox
IMPLEMENT_DYNAMIC(CCustomDrawCombox, CComboBox)
CCustomDrawCombox::CCustomDrawCombox()
{
COMBOBOXINFO si;
::GetComboBoxInfo(this->GetSafeHwnd(),&si);
int iiii = 0;;
m_bAutoComplete = TRUE;
}
CCustomDrawCombox::~CCustomDrawCombox()
{
}
BEGIN_MESSAGE_MAP(CCustomDrawCombox, CComboBox)
ON_WM_PAINT()
ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
END_MESSAGE_MAP()
// CCustomDrawCombox 消息处理程序
void CCustomDrawCombox::PreSubclassWindow()
{
ModifyStyle(0,BS_OWNERDRAW);
CComboBox::PreSubclassWindow();
}
void CCustomDrawCombox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CString strDrawText;
if(!lpDrawItemStruct->itemID!=-1)
{
GetLBText(lpDrawItemStruct->itemID,strDrawText);
}
else
{
strDrawText = "请选择";
}
TEXTMETRIC TextMetr;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if(pDC)
{
pDC->GetTextMetrics(&TextMetr);
CRect rcClient;
GetClientRect(&rcClient);
if((lpDrawItemStruct->itemState & ODS_SELECTED)&&
(lpDrawItemStruct->itemAction&(ODA_DRAWENTIRE|ODA_SELECT)))
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem,RGB(101,160,251));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->SetTextColor(RGB(255,255,255));
pDC->TextOut(lpDrawItemStruct->rcItem.left,lpDrawItemStruct->rcItem.top,strDrawText);
}
else if (lpDrawItemStruct->itemAction&(ODA_SELECT|ODA_DRAWENTIRE))
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem,RGB(255,255,255));
pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->TextOut(lpDrawItemStruct->rcItem.left,lpDrawItemStruct->rcItem.top,strDrawText);
}
ReleaseDC(pDC);
}
}
void CCustomDrawCombox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 40;
}
int CCustomDrawCombox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
return 1;
}
void CCustomDrawCombox::OnPaint()
{
CPaintDC dc(this); // device context for painting
OnNcPaint(&dc);
}
void CCustomDrawCombox::OnNcPaint(CDC* pDC)
{
//绘制客户区
CDC dMemDC;
dMemDC.CreateCompatibleDC(pDC);
dMemDC.SetMapMode(pDC->GetMapMode());
//画动作
CBitmap mNewBmp;
CRect rc;
GetClientRect(&rc);
mNewBmp.CreateCompatibleBitmap(pDC, rc.right - rc.left, rc.bottom - rc.top);
CBitmap* pOldBmp = dMemDC.SelectObject(&mNewBmp);
CPen pen(PS_SOLID,1,RGB(200,200,200));
CPen *pOldPen = dMemDC.SelectObject(&pen);
CBrush bkBrush;
bkBrush.CreateSolidBrush(RGB(255,255,255));
dMemDC.SelectObject(&bkBrush);
CPoint pt(10,10);
dMemDC.Rectangle(rc);//画整个客户区域
CRect rcEnd(rc);//按钮区域
rcEnd.left = rc.right - 20;
//画右边的三角形按钮
CBrush bkBrushRect;
bkBrushRect.CreateSolidBrush(RGB(21,123,237));
dMemDC.SelectObject(&bkBrushRect);
dMemDC.Rectangle(rcEnd);
//画三角形
CRgn rgn;
CPoint ptAngle[3];
int angleSideWidth = 8;//三角形边长
//第一个点的坐标
ptAngle[0].x = rcEnd.left+rcEnd.Width()/2-angleSideWidth/2;
ptAngle[0].y = rcEnd.top+rcEnd.Height()/2-2;
//第二个点的坐标
ptAngle[1].x = ptAngle[0].x + angleSideWidth;
ptAngle[1].y = ptAngle[0].y;
//第三个点的坐标
ptAngle[2].x = rcEnd.left+rcEnd.Width()/2;
ptAngle[2].y = ptAngle[0].y + 5;
CBrush brushAngle;
rgn.CreatePolygonRgn(ptAngle, 3, ALTERNATE); //创建区域
brushAngle.CreateSolidBrush( RGB(255,255,255) ); //创建画刷
dMemDC.FillRgn( &rgn, &brushAngle ); //填充区域
//dMemDC.DrawFrameControl( &rcEnd,DFC_SCROLL,DFCS_SCROLLDOWN|DFCS_FLAT|DFCS_MONO );
pDC->BitBlt(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, &dMemDC,
rc.left ,rc.top, SRCCOPY);
//恢复
dMemDC.SelectObject(pOldBmp);
dMemDC.SelectObject(pOldPen);
pOldPen->DeleteObject();
pOldBmp->DeleteObject();
dMemDC.DeleteDC();
bkBrush.DeleteObject();
bkBrushRect.DeleteObject();
brushAngle.DeleteObject();
}
BOOL CCustomDrawCombox::PreTranslateMessage(MSG* pMsg)
{
// Need to check for backspace/delete. These will modify the text in
// the edit box, causing the auto complete to just add back the text
// the user has just tried to delete.
if (pMsg->message == WM_KEYDOWN)
{
m_bAutoComplete = TRUE;
int nVirtKey = (int)pMsg->wParam;
if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
m_bAutoComplete = FALSE;
}
return CComboBox::PreTranslateMessage(pMsg);
}
void CCustomDrawCombox::OnEditUpdate()
{
// if we are not to auto update the text, get outta here
if (!m_bAutoComplete)
return;
// Get the text in the edit box
CString str;
GetWindowText(str);
int nLength = str.GetLength();
// Currently selected range
DWORD dwCurSel = GetEditSel();
WORD dStart = LOWORD(dwCurSel);
WORD dEnd = HIWORD(dwCurSel);
// Search for, and select in, and string in the combo box that is prefixed
// by the text in the edit box
if (SelectString(-1, str) == CB_ERR)
{
SetWindowText(str); // No text selected, so restore what
// was there before
if (dwCurSel != CB_ERR)
SetEditSel(dStart, dEnd); //restore cursor postion
}
// Set the text selection as the additional text that we have added
if (dEnd < nLength && dwCurSel != CB_ERR)
SetEditSel(dStart, dEnd);
else
SetEditSel(nLength, -1);
}