Ctrl+C and Ctrl+V in a Windows Listbox?

Hi,

Our customer has a bit unusual wish -- to be able to use the content of the clipboard in a list box. Basically, items should be selected based on the content from the clipboard (kind of paste operation), or some info from the selected items should be copied into the clipboard (kind of copy operation).

The application is implemented in C++, using MS Visual Studio C++ (VS 2005 for that version of the application). I am also using ATL and WTL libraries (no MFC is used).

So far, I have the implementation that uses the context menu items for invoking the operations.

Can the operations be bound somehow to the Ctrl+C and Ctrl+V combinations?

Using a Spy++, it seems that the WM_COPY and WM_PASTE messages are not supported for the listbox windows. Is my observation correct?

Definitely, it is possible to capture WM_KEYUP and WM_KEYDOWN and the like low-level messages. Is there any ready-to-be-used code to translate them to something like WM_COPY and WM_PASTE messages? I only need to detect the situation to call the handler functions.

Thanks for your time and experience,
   Petr
LVL 30
peprAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>Definitely, it is possible to capture WM_KEYUP and WM_KEYDOWN and the like low-level
>>messages. Is there any ready-to-be-used code to translate them to something like
>>WM_COPY and WM_PASTE messages?

Not sure if there is any ready-made code, but using a local (i.e. your process only) hook should make that simple, e.g.

// A "WH_GETMESSAGE" hook
LRESULT CALLBACK HookProc	(	int		nCode,	// hook code
                                WPARAM	wParam,	// removal flag 
                                LPARAM  lParam 	// address of structure with message 
                            )
{
    PMSG	pmsg	=	( PMSG)	lParam;

    if	(0 > nCode || PM_NOREMOVE == wParam)	
        {
            return	(	CallNextHookEx	(	g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if	(pmsg->message == WM_KEYDOWN)
        {
            switch (pmsg->wParam)
            {
                case VK_INSERT:
                    // call handler here or translate to 'PostMessage()'
                    break;

                case VK_COPY:
                    // call handler here or translate to 'PostMessage()'
                    break;

            };

    return	(	CallNextHookEx	(	g_hhk,
                                    nCode,
                                    wParam,
                                    lParam
                                )
            );
}

Open in new window


You'll find more about hooks at http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx ("Hooks")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
as the listbox control doesn't handle WM_COPY and WM_PASTE, you can't simply translate the keys.

if you have an own message loop you don't need to hook but can handle the message directly from that loop. the winapi function

BOOL WINAPI IsDialogMessage(
  _In_  HWND hDlg,
  _In_  LPMSG lpMsg
);

Open in new window


could be called in the message loop (after GetMessage has returned a new message) before calling TranslateMessage and DispatchMessage. the IsDialogMessage would check whether the message is directed to the given window and if yes would try to process the message. it sends a WM_GETDLGCODE message to the (dialog) window where you could handle the ctrl+c and ctrl+v .

alternatively, you could use your own TranslateMessage function and check key input against an "accelerator table". that is the way mfc handles accelerator key strokes. the IsDialogMessage has the advantage that it only processes messages directed to the given window and especially keystrokes. it therefore is a good filter for your purpose. you even could try to pass the hwnd of the listbox rather than the hwnd of the dialog or form. if you do so, you need to subclass the listbox window such that it processes an own message proc.

Sara

see http://msdn.microsoft.com/en-us/library/vstudio/ms645498.aspx for details.
0
peprAuthor Commented:
Thanks both for the hints. I was thinking more about it. It actually makes sense not to support copy/paste for a listbox. The problem is that the parent window contains more of them and it is difficult to explain to the average user that the listbox has to have focus first.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.