Discussion:
VC: ctrl+a to select all in listbox;ctrl+c to copy into clipboard
(too old to reply)
Viv
2010-07-12 14:19:20 UTC
Permalink
Raw Message
Hi all,

I have a very basic application which has just a dialog with a listbox
on it. I need to simulate in the listbox the ctrl+a and ctrl+c
behavior. I mean, in the listbox there are some lines (messages sent
by another application) and I would like to do like any windows user
is used: ctrl+a to select the whole content of the listbox, then
ctrl+c to copy everything in the clipboard.

The code I'm having at the moment:

<myfile.rc>

// Microsoft Visual C++ generated resource script.
//
#include "resource."

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_PRIMARYFAMILY_DLG DIALOGEX 260, 200, 431, 306
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION
| WS_SYSMENU
CAPTION "!@$@!$#@$%@#$#!@#@!#!*^&$%^#$%@#$@!#!"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
LISTBOX IDC_PRIMARYFAMILY_LB,13,10,404,252,LBS_EXTENDEDSEL
END


#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.\0"
END

3 TEXTINCLUDE
BEGIN
"\r\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\0"
END

#endif // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_PRIMARYFAMILY_DLG, DIALOG
BEGIN
RIGHTMARGIN, 297
BOTTOMMARGIN, 244
END
END
#endif // APSTUDIO_INVOKED

#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//

/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

</myfile.rc>

=================================================


<myfile.cpp>

#include <windows.h>
#include <Windowsx.h>
#include "Resource.h"
#include "wchar.h"
#include "strsafe.h"

//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInstGlobal;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM
lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInstGlobal = hInstance;
DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM
lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG: {
LONG style;
style = GetWindowLong(hWndDlg, GWL_STYLE);
//style &= ~(WS_CAPTION);
SetWindowLong(hWndDlg, GWL_STYLE, style);
return TRUE;
}
case WM_COMMAND: {
switch(LOWORD(wParam))
{
case IDC_PRIMARYFAMILY_LB:
{
return TRUE;
}
}
return TRUE;
} break;
case WM_CLOSE: {
PostQuitMessage(WM_QUIT);
} break;
}
return FALSE;
}
//---------------------------------------------------------------------------

</myfile.cpp>


Can smbd point me how could I have the ctrl+a, ctrl+c behaviour
implemented into this listbox?

Thanks in advance,
Viv
Dee Earley
2010-07-12 15:22:58 UTC
Permalink
Raw Message
Post by Viv
Hi all,
I have a very basic application which has just a dialog with a listbox
on it. I need to simulate in the listbox the ctrl+a and ctrl+c
behavior. I mean, in the listbox there are some lines (messages sent
by another application) and I would like to do like any windows user
is used: ctrl+a to select the whole content of the listbox, then
ctrl+c to copy everything in the clipboard.
Are you sure they work by the user pressing them?
I don't think the standard listbox does.

You'll need to handle those key presses and copy the text to the
clipboard in code.
You'll then find it MUCH easier to just call that code rather than
trying to fake the key presses.
--
Dee Earley (***@icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)
Ivo Beltchev
2010-07-12 15:40:13 UTC
Permalink
Raw Message
I don't think the OP wants to simulate the keypresses. He wants when
Ctrl+A is pressed to select all items, and when Ctrl+C is pressed to
copy (somehow) the items to the clipboard. Imagine how in Explorer you
press Ctrl+A to select all files, and then Ctrl+C to copy them. This is
not a standard listbox behavior, but is a standard UI behavior so many
users will be expecting that.

This is done by subclassing the list control and handling the WM_KEYDOWN
message. Something like this:
if (uMsg==WM_KEYDOWN && wParam=='A' && GetKeyState(VK_CONTROL)<1)
SelectAll();
if (uMsg==WM_KEYDOWN && wParam=='C' && GetKeyState(VK_CONTROL)<1)
Copy();

Use the LB_SELITEMRANGE message to select all items.

The clipboard is trickier, because we don't know what you keep in your
listbox items and how you want that to be stored in the clipboard -
plain text, Unicode text, file names, private format?

So basically you will need to read up on subclassing and clipboards first.

Ivo
Post by Dee Earley
Post by Viv
Hi all,
I have a very basic application which has just a dialog with a listbox
on it. I need to simulate in the listbox the ctrl+a and ctrl+c
behavior. I mean, in the listbox there are some lines (messages sent
by another application) and I would like to do like any windows user
is used: ctrl+a to select the whole content of the listbox, then
ctrl+c to copy everything in the clipboard.
Are you sure they work by the user pressing them?
I don't think the standard listbox does.
You'll need to handle those key presses and copy the text to the
clipboard in code.
You'll then find it MUCH easier to just call that code rather than
trying to fake the key presses.
Viv
2010-07-13 11:53:30 UTC
Permalink
Raw Message
Hi guys,

Thanks for your tips!
Viv
Post by Ivo Beltchev
I don't think the OP wants to simulate the keypresses. He wants when
Ctrl+A is pressed to select all items, and when Ctrl+C is pressed to
copy (somehow) the items to the clipboard. Imagine how in Explorer you
press Ctrl+A to select all files, and then Ctrl+C to copy them. This is
not a standard listbox behavior, but is a standard UI behavior so many
users will be expecting that.
This is done by subclassing the list control and handling the WM_KEYDOWN
if (uMsg==WM_KEYDOWN && wParam=='A' && GetKeyState(VK_CONTROL)<1)
SelectAll();
if (uMsg==WM_KEYDOWN && wParam=='C' && GetKeyState(VK_CONTROL)<1)
Copy();
Use the LB_SELITEMRANGE message to select all items.
The clipboard is trickier, because we don't know what you keep in your
listbox items and how you want that to be stored in the clipboard -
plain text, Unicode text, file names, private format?
So basically you will need to read up on subclassing and clipboards first.
Ivo
Post by Dee Earley
Post by Viv
Hi all,
I have a very basic application which has just a dialog with a listbox
on it. I need to simulate in the listbox the ctrl+a and ctrl+c
behavior. I mean, in the listbox there are some lines (messages sent
by another application) and I would like to do like any windows user
is used: ctrl+a to select the whole content of the listbox, then
ctrl+c to copy everything in the clipboard.
Are you sure they work by the user pressing them?
I don't think the standard listbox does.
You'll need to handle those key presses and copy the text to the
clipboard in code.
You'll then find it MUCH easier to just call that code rather than
trying to fake the key presses.
Dirk Noack
2010-07-15 05:18:32 UTC
Permalink
Raw Message
Hi Viv,

J.Newcomers essay "Copying ListBox Contents to the Clipboard" at
http://www.flounder.com/lbcopy.htm could be very useful for you.

Dirk

Loading...