Thank you for your time.
My program works well. Thank you again.
Joseph M. Newcomer wrote:
Sorry. I went back and checked my code. here's a stripped-down version (mine is
owner-draw with the data arranged in the ItemData part). Note the addition of the caret
to maintain position, and the OnChar handler is what is necessary to avoid the behavior
you are seeing.
joe
#pragma once
// CListBoxCutCopy
class CListBoxCutCopy : public CListBox
{
DECLARE_DYNAMIC(CListBoxCutCopy)
public:
CListBoxCutCopy();
virtual ~CListBoxCutCopy();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg LRESULT OnCopy(WPARAM, LPARAM);
afx_msg LRESULT OnCut(WPARAM, LPARAM);
};
===============================================================
// ListBoxCutCopy.cpp : implementation file
//
#include "stdafx.h"
#include "ListBoxCutCopy.h"
#include "Clipboard.h"
// CListBoxCutCopy
IMPLEMENT_DYNAMIC(CListBoxCutCopy, CListBox)
CListBoxCutCopy::CListBoxCutCopy()
{
}
CListBoxCutCopy::~CListBoxCutCopy()
{
}
BEGIN_MESSAGE_MAP(CListBoxCutCopy, CListBox)
ON_WM_KEYDOWN()
ON_WM_CHAR()
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_COPY, OnCopy)
END_MESSAGE_MAP()
// CListBoxCutCopy message handlers
void CListBoxCutCopy::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(GetKeyState(VK_CONTROL) < 0)
{ /* control key */
switch(nChar)
{ /* nChar */
case _T('A'):
{ /* Select all */
int caret = GetCaretIndex();
for(int i = 0; i < GetCount(); i++)
SetSel(i, TRUE);
SetCaretIndex(caret);
} /* Select all */
break;
case _T('C'):
SendMessage(WM_COPY);
break;
case _T('X'):
SendMessage(WM_CUT);
break;
default:
return;
} /* nChar */
} /* control key */
CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
}
/****************************************************************************
* CListBoxCutCopy::OnChar
* Inputs:
* UNIT nChar: Character code
* UINT NRepCnt: Repeat count
* UINT nFlags: flags
* Result: void
*
* Effect:
* Ignores OnChar if we have a control key
****************************************************************************/
void CListBoxCutCopy::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(GetKeyState(VK_CONTROL) < 0)
return;
CListBox::OnChar(nChar, nRepCnt, nFlags);
}
/****************************************************************************
* CListBoxCutCopy::OnCopy
* Inputs:
* WPARAM: ignored
* LPARAM: ignored
* Result: LRESULT
* Logically void, 0, always
* Effect:
* Copies the strings to the clipboard
****************************************************************************/
LRESULT CListBoxCutCopy::OnCopy(WPARAM, LPARAM)
{
CString text;
int sels = GetSelCount();
CArray<int>selections;
selections.SetSize(sels);
GetSelItems(sels, selections.GetData());
for(int i = 0; i < selections.GetSize(); i++)
{ /* scan selections */
CString s;
GetText(selections[i], s);
text += s;
text += _T("\r\n");
} /* scan selections */
SendToClipboard(text);
return 0;
} // CListBoxCutCopy::OnCopy
/****************************************************************************
* CListBoxCutCopy::OnCut
* Inputs:
* WPARAM: ignored
* LPARAM: ignored
* Result: LRESULT
* Logically void, 0, always
* Effect:
* Cuts the elements from the listbox
****************************************************************************/
LRESULT CListBoxCutCopy::OnCut(WPARAM, LPARAM)
{
OnCopy(NULL, NULL);
int sels = GetSelCount();
CArray<int>selections;
selections.SetSize(sels);
GetSelItems(sels, selections.GetData());
for(int i = selections.GetSize() - 1; i >= 0; i--)
{ /* delete each */
DeleteString(selections[i]);
} /* delete each */
return 0;
} // CListBoxCutCopy::OnCut
On Wed, 01 Apr 2009 20:29:58 -0500, TonyG <TonyG@junk.com> wrote:
I STILL HAVE ONE PROBLEM.
I implemented your example code. I also took your criticisms to heart.
Everything is in the subclassed ListBox.
I still have the same problem. Apparently the CListBox has default
activity for all ctrl-keys. This is implemented even though my OnKeyDown
traps and does not forward all ctrl-keys.
Ctrl-C works and places the text in the paste buffer. The default action
of changing the selection still occurs. Ctrl-A is especially
problematic. My code causes everything to be selected. The default
action then clears that and selects only the top most line that begins
with an 'A';
HOW DO I STOP THE DEFAULT ACTION.
I am using Visual C++ version 6.
Joseph M. Newcomer wrote:
See below...
On Tue, 31 Mar 2009 21:32:20 -0500, TonyG <TonyG@junk.com> wrote:
I still have one problem.
I successfully implemented OnKeyDown to capture ctrl-c and I
successfully implemented the clipboard stuff.
What I want to do is to disable the default mechanism. In a ListBox when
you type ctrl-any letter. Whatever you have selected is unselected and a
new selection is made of the top most line that begins with whatever
ctrl-letter you are using. I don't want that. I want to disable that
functionality.
How do I do that?
FYI: here is my OnKeyDown function in my subclassed ListBox. Instead of
handling the clipboard in the subclass, I chose to pass it to the dialog
to deal with. I have a reason to do that.
void ZScListBox::OnKeyDown(
UINT nChar,
UINT nRepCnt,
UINT nFlags
)
{
switch(nChar)
{
case 65: // a
*****
case _T('A'):
why use silly numbers like "65" when you can use the letter itself?
****
// check if ctrl is depressed
if (0x8000 == (GetKeyState(VK_CONTROL) & 0x8000))
****
I believe the documented means of doing this test is
if(GetKeyState(VK_CONTROL) < 0)
****
{
// ctrl-a is pressed
// tell the parent
// send the ID of the list box
GetParent()->SendMessage(SC__MESSAGE_LIST_BOX_CTRL_A, 0,
GetDlgCtrlID());
****
I would simply SendMessage() a user-defined message to this control and let it do the
selection; the parent doesn't get involved in this at all!
****
}
break;
case 67: // c
****
case _T('C'):
why make it hard to understand the code? Who knows what "67" means?
****
// check if ctrl is depressed
if (0x8000 == (GetKeyState(VK_CONTROL) & 0x8000))
{
// ctrl-c is pressed
// tell the parent
// send the ID of the list box
GetParent()->SendMessage(SC__MESSAGE_LIST_BOX_CTRL_C, 0,
GetDlgCtrlID());
****
I would SendMessage(WM_COPY), that is, send the message to this control, and implement an
OnCopy handler to deal with it. The parent does not need to be involved in this at all,
and shouldn't be.
****
}
break;
case 17: // ctrl
****
What's a 17? I have no clue. Didn't you mean VK_CONTROL?
What is this horrid fixation on integer values?
****
default:
// call base class
CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
break;
};
}
****
The problem is that you are not dealing with the combinations Ctrl+B, Ctrl+D, Ctrl+E, ...,
Ctrl+Z. So what you need to write is this:
void ZScListBox::OnKeyDown(
UINT nChar,
UINT nRepCnt,
UINT nFlags
)
{
if(GetKeyState(VK_CONTROL) < 0)
{ /* control key */
case _T('A'):
for(int i = 0; i < GetCount(); i++)
SetSel(i, TRUE);
break;
case _T('C'):
SendMessage(WM_COPY);
break;
case _T('X'):
SendMessage(WM_CUT);
break;
} /* control key */
else
CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
}
LRESULT ZScListBox::OnCut(WPARAM, LPARAM)
{
ZScListBox::OnCopy(WPARAM, LPARAM);
int sels = GetSelCount();
CArray<int> selections;
selections.SetSize(sels);
GetSelItems(selections.GetData(), sels); // check parameter order, I never remember
// it
for(int i = sels - 1; i >= 0; i--)
DeleteString(selections[i]);
return 0;
}
LRESULT ZScListBox::OnCopy(WPARAM, LPARAM)
{
int sels = GetSelCount();
CArray<int> selections;
selections.SetSize(sels);
GetSelItems(selections.GetData(), sels);
for(int i = 0; i < sels; i++)
{
CString s;
GetText(i, s);
... add string to total copied text, etc.
}
/// put into clipboard
return 0;
}
Note that it is none of the parent window's business how copying, cutting, or selection
are done!
joe
****
Ajay wrote:
On Mar 31, 8:39 am, TonyG <To...@junk.com> wrote:
I have a many line owner-draw list box in my program. I use the
"Extended" selection method that allows lines to be highlighted.
How do I trap the ctrl-c key so that I can copy the selected lines to
the paste buffer?
Once I have text, how do I put the text into the paste buffer?
You should be able to use OnKeyDown of your control to trap it. Also
to put the data on clipboard, take a look at SetClipboardData.
--
Ajay
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm