-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClipListBox.cpp
149 lines (120 loc) · 3.14 KB
/
ClipListBox.cpp
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
// ClipListBox.cpp : implementation file
//
// $Id$
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "phreeqci2.h"
#include "ClipListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClipListBox
CClipListBox::CClipListBox()
{
// accelerator table
m_hAccelTable = ::LoadAccelerators(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_CLIPLISTBOX));
}
CClipListBox::~CClipListBox()
{
}
BEGIN_MESSAGE_MAP(CClipListBox, CListBox)
//{{AFX_MSG_MAP(CClipListBox)
ON_COMMAND(ID_COPY, OnCopy)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
ON_CONTROL_REFLECT_EX(LBN_DBLCLK, OnDblclk)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClipListBox message handlers
BOOL CClipListBox::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST)
{
// translate accelerators
if (m_hAccelTable != NULL && ::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg))
{
return TRUE;
}
}
return CListBox::PreTranslateMessage(pMsg);
}
void CClipListBox::OnCopy()
{
int nCurSel = GetCurSel();
if (nCurSel == LB_ERR) return;
if (::OpenClipboard(m_hWnd))
{
// Retrieve string from ListBox
CString strText;
GetText(nCurSel, strText);
// Allocate global memory
HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_DDESHARE, (strText.GetLength() + 1) * sizeof(TCHAR));
if (hglbCopy)
{
// Lock it and get a pointer to it
LPTSTR lptstrCopy = (LPTSTR)::GlobalLock(hglbCopy);
if (lptstrCopy)
{
// Empty existing clipboard contents
VERIFY(::EmptyClipboard());
// Copy string into memory
VERIFY(::lstrcpy(lptstrCopy, (LPCTSTR)strText));
// Unlock memory
VERIFY(::GlobalUnlock(hglbCopy));
// Send memory to clipboard
#ifdef UNICODE
VERIFY(::SetClipboardData(CF_UNICODETEXT, hglbCopy));
#else
VERIFY(::SetClipboardData(CF_TEXT, hglbCopy));
#endif
}
else
{
// Free memory if it couldn't be locked
::GlobalFree(hglbCopy);
}
}
// Close the clipboard.
VERIFY(::CloseClipboard());
}
}
void CClipListBox::OnContextMenu(CWnd* pWnd, CPoint point)
{
UNUSED(pWnd);
ASSERT(pWnd == this);
CMenu menu;
VERIFY(menu.LoadMenu(IDR_CLIPLISTBOX));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup);
if (pPopup)
{
if (point.x == -1 && point.y == -1)
{
// keystroke invocation
point.x = point.y = 0;
ClientToScreen(&point);
}
// Note: All commands except copy are grayed by default
// disable copy item if there's no selection
if (GetCurSel() == LB_ERR)
{
VERIFY(pPopup->EnableMenuItem((UINT)ID_COPY, MF_BYCOMMAND | MF_GRAYED) != -1);
}
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
BOOL CClipListBox::OnDblclk()
{
OnCopy();
return FALSE; // let parent window be notified
}
void CClipListBox::OnRButtonDown(UINT nFlags, CPoint point)
{
// Make like LButtonDown
SendMessage(WM_LBUTTONDOWN, (WPARAM)nFlags, MAKELPARAM(point.x, point.y));
CListBox::OnRButtonDown(nFlags, point);
}