-
Notifications
You must be signed in to change notification settings - Fork 151
/
ListViewCtrlEdit.h
388 lines (290 loc) · 7.07 KB
/
ListViewCtrlEdit.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#pragma once
#include <Windows.h>
#include <windef.h>
#include <atlstr.h>
#include <atldef.h>
#include <atltypes.h>
#include <atlbase.h>
#include <atlctrls.h>
#include <atlapp.h>
#include <atlwin.h>
#include <atldlgs.h>
#include <DoubleBuffer.h>
#define NM_GetTipToolInfo NM_FIRST-50
using namespace WTL;
struct TipTooInfo
{
NMHDR hr;
HTREEITEM Item;
LPCWSTR String;
};
//#include <atlctrls.h>
#define NM_LVC_EditItem NM_FIRST-51
struct LVCEditItemInfo
{
NMHDR hr;
int iItem;
int SubItem;
BOOL NeedEdit;
};
class CListViewCtrl2 :public CWindowImpl < CListViewCtrl2, CListViewCtrl >
{
public:
CDoubleBuffer mDoubleBuffer;
BEGIN_MSG_MAP_EX(CListViewCtrl2)
CHAIN_MSG_MAP_MEMBER(mDoubleBuffer);
END_MSG_MAP()
};
static void* pData;
class CListViewCtrlEdit :public CWindowImpl <CListViewCtrlEdit, CListViewCtrl>
{
class CEditForListViewCtrl :public CWindowImpl <CEditForListViewCtrl, CEdit>
{
CString Buffer;
BOOL m_bExchange;//是否进行数据交换
int nItem, nIndex;
static LRESULT CALLBACK GetMsgProc(int code, // hook code
WPARAM wParam, // removal option
LPARAM lParam // message
)
{
auto pPage = (CEditForListViewCtrl*)pData;
auto pMsg = (MSG*)lParam;
if (pPage->PreTranslateMessage(pMsg))
{
pMsg->message = WM_NULL;
return 0;
}
return CallNextHookEx(pPage->g_hook, code, wParam, lParam);
}
public:
HHOOK g_hook;
/*virtual WNDPROC GetWindowProc()
{
return MyWindowProc;
}*/
BOOL PreTranslateMessage(MSG* pMsg)
{
if (m_hWnd == pMsg->hwnd&& pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_ESCAPE:
//如果按取消,那么不保存数据
m_bExchange = FALSE;
case VK_RETURN:
ShowWindow(0);
return TRUE;
default:
break;
}
}
return FALSE;
}
BEGIN_MSG_MAP_EX(CEditForListViewCtrl)
MSG_WM_KILLFOCUS(OnKillFocus)
MSG_WM_SHOWWINDOW(OnShowWindow)
//DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
void OnKillFocus(CWindow wndFocus)
{
//
if (IsWindowVisible()==0)
{
SetMsgHandled(FALSE);
}
else
{
m_bExchange = TRUE;
ShowWindow(0);
}
//else
//{
//ShowWindow(FALSE);
//this->
//SetMsgHandled(TRUE);
//SetFocus();
//}
}
LRESULT OnNotify(int idCtrl, LPNMHDR pnmh)
{
SetMsgHandled(FALSE);
return 0;
}
void OnShowWindow(BOOL bShow, UINT nStatus)
{
SetMsgHandled(FALSE);
CListViewCtrl List(GetParent());
List.GetHeader().EnableWindow(!bShow);
if (bShow)
{
pData = this;
g_hook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, GetModuleHandle(NULL), GetCurrentThreadId());
m_bExchange = TRUE;
}
else
{
UnhookWindowsHookEx(g_hook);
if (m_bExchange)
{
//提交数据更改
GetWindowText(Buffer);
List.SetItemText(nItem, nIndex, Buffer);
}
}
}
void ShowEdit(BOOL bShow, int nItem, int nIndex, CListViewCtrlEdit& List)
{
// 如果编辑框对象尚未创建
if (m_hWnd == NULL)
{
if (bShow == FALSE)
return;
//创建一个编辑框(大小为零)
Create(List.m_hWnd, 0, NULL, ES_AUTOHSCROLL | WS_CHILD | ES_LEFT
| ES_WANTRETURN | WS_BORDER, 0, 1000);
//CWindow(m_hWnd).ModifyStyle()
//m_Edit.ShowWindow(SW_HIDE);// 隐藏
//使用默认字体
//CFont tpFont(DEFAULT_GUI_FONT);
//tpFont.CreateFontWCreate//StockObject(DEFAULT_GUI_FONT);
//m_edit.SetFont(&tpFont);
//tpFont.DeleteObject();
}
if (IsWindowVisible() == bShow)
return;
//如果bShow为true,显示编辑框
if (bShow == TRUE)
{
this->nItem = nItem;
this->nIndex = nIndex;
CRect rcCtrl; //数据项所在区域
SetWindowTextW(List.GetItemText(nItem, nIndex));
//CString strItem = CListCtrl::GetItemText(nItem, nIndex);//获取列表控件中数据项的内容
//m_Edit.SetCtrlData(nItem, nIndex, CListCtrl::GetItemText(nItem, nIndex));
List.GetSubItemRect(nItem, nIndex, LVIR_LABEL, rcCtrl);
rcCtrl.bottom += 3;
MoveWindow(rcCtrl);// 移动到子项所在区域
ShowWindow(SW_SHOW);//显示控件
//m_edit.SetWindowText(strItem);// 显示数据
SetFocus();//设置焦点
//::SendMessage(m_Edit, EM_SETSEL, 0, -1);//使数据处于选择状态
SetSelAll();
}
else
{
ShowWindow(SW_HIDE);
}
}
} m_Edit;
CToolTipCtrl m_pToolTip;
TipTooInfo TipInfo;
public:
BOOL IsEditShow()
{
if (m_Edit.m_hWnd)
{
return m_Edit.IsWindowVisible();
}
else
return FALSE;
}
/*BOOL PreTranslateMessage(MSG* pMsg)
{
return m_Edit.PreTranslateMessage(pMsg);
}*/
//CDoubleBuffer mDoubleBuffer;
BEGIN_MSG_MAP_EX(CListViewCtrlEdit)
MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
MSG_WM_LBUTTONDOWN(OnLButtonDown)
MSG_WM_MOUSEMOVE(OnMouseMove)
//CHAIN_MSG_MAP_MEMBER(mDoubleBuffer);
END_MSG_MAP()
void OnLButtonDblClk(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
LVHITTESTINFO lvhti; //用于列表控件子项鼠标点击测试的数据结构
lvhti.pt = point; //输入鼠标位置
int nItem = SubItemHitTest(&lvhti);//调用基类的子项测试函数,返回行号
if (nItem == -1) //如果鼠标在控件外双击,不做任何处理
return;
LVCEditItemInfo ItemInfo{ NMHDR{ m_hWnd, GetDlgCtrlID(), NM_LVC_EditItem }, nItem, lvhti.iSubItem, TRUE };
::SendMessage(GetParent(), WM_NOTIFY, 0, (LPARAM)&ItemInfo);
if (ItemInfo.NeedEdit == FALSE)
return;
m_Edit.ShowEdit(TRUE, nItem, lvhti.iSubItem, *this); //调用自定义函数,显示编辑框
}
void OnLButtonDown(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
if (m_Edit.m_hWnd)
{
m_Edit.ShowWindow(0);
}
}
BOOL SubclassWindow(HWND hWnd)
{
if (hWnd&&CWindowImpl<CListViewCtrlEdit, CListViewCtrl>::SubclassWindow(hWnd))
{
TipInfo.hr.code = NM_GetTipToolInfo;
TipInfo.hr.hwndFrom = m_hWnd;
TipInfo.hr.idFrom = CWindow(m_hWnd).GetDlgCtrlID();
TipInfo.Item = (HTREEITEM)-1;
m_pToolTip.Create(m_hWnd, 0, NULL, TTS_ALWAYSTIP);
m_pToolTip.AddTool(m_hWnd, LPSTR_TEXTCALLBACK);
this->SetToolTips(m_pToolTip);
m_pToolTip.Activate(TRUE);
//m_pToolTip.SetTipBkColor(GetSysColor(COLOR_WINDOW));
//m_pToolTip.SetTipTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
m_pToolTip.SetDelayTime(TTDT_AUTOPOP, 10000);
m_pToolTip.SetMaxTipWidth(300);
return TRUE;
}
else
{
return FALSE;
}
}
void OnMouseMove(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
UINT temp;
HTREEITEM hItem = (HTREEITEM)HitTest(point, &temp);
if (temp & LVHT_ONITEM)
{
if (TipInfo.Item != hItem)
{
TipInfo.Item = hItem;
//m_pToolTip.SetWindowTextW(L"哈哈");
//m_pToolTip.Activate(1);
//
//m_pToolTip.ShowWindow(1);
TipInfo.String = NULL;
::SendMessage(GetParent(), WM_NOTIFY, 0, (LPARAM)&TipInfo);
if (TipInfo.String)
{
//m_pToolTip.SetToolInfo()
m_pToolTip.Activate(1);
m_pToolTip.UpdateTipText(TipInfo.String, m_hWnd);
//m_pToolTip.ShowWindow(1);
}
else
{
//m_pToolTip.ShowWindow(0);
//m_pToolTip.Pop();
m_pToolTip.Activate(0);
}
}
return;
}
else
{
TipInfo.Item = (HTREEITEM)-1;
}
m_pToolTip.Activate(0);
//m_pToolTip.Pop();
//m_pToolTip.ShowWindow(0);
//m_pToolTip.Activate(0);
//SetMsgHandled(FALSE);
}
};