win32 select all on edit ctrl (textbox)

2020-02-14 05:53发布

I am creating my textbox with these options. I can copy/cut/paste/undo, but when i hit select A it doesnt select all. I can right click and click select all but ctrl a doesnt do anything, why?

        wnd = CreateWindow("EDIT", 0,
            WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
            x, y, w, h,
            parentWnd,
            NULL, NULL, NULL);

7条回答
何必那么认真
2楼-- · 2020-02-14 06:03

You need to capture that keystroke and do the select all yourself.

Here is some C# code for use with a RichTextBox:

    protected override void OnKeyDown(KeyEventArgs e)
    {
        // Ctrl-A does a Select All in the editor window
        if (e.Control && (e.KeyCode == Keys.A))
        {
            this.SelectAll();
            e.Handled = true;
        }
    }

Sorry, I don't have Win32 code for you.

查看更多
欢心
3楼-- · 2020-02-14 06:06

Could it be that something else is stealing Ctrl+A? Use Spy++ to verify that it reaches your edit control.

查看更多
疯言疯语
4楼-- · 2020-02-14 06:07

The strange thing is that Ctrl+A DOES work (as select all), if you do NOT specify ES_MULTILINE

But that doesn't help if you need multiline

The MSDN documentation for ES_MULTILINE doesn't appear to say anything about this.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-14 06:10

Why not add an accelerator for Ctrl+a to SelectAll?

查看更多
叼着烟拽天下
6楼-- · 2020-02-14 06:12

Ctrl+A is not a built-in accelerator like Ctrl+C and Ctrl+V. This is why you see WM_CUT, WM_PASTE and WM_COPY messages defined, but there is no WM_SELECTALL.

You have to implement this functionality yourself. I did in my MFC app like this:

static BOOL IsEdit( CWnd *pWnd ) 
{
    if ( ! pWnd ) return FALSE ;
    HWND hWnd = pWnd->GetSafeHwnd();
    if (hWnd == NULL)
     return FALSE;

    TCHAR szClassName[6];
    return ::GetClassName(hWnd, szClassName, 6) &&
         _tcsicmp(szClassName, _T("Edit")) == 0;
}

BOOL LogWindowDlg::PreTranslateMessage(MSG* pMsg) 
{
    if(pMsg->message==WM_KEYDOWN)
    {
        if ( pMsg->wParam=='A' && GetKeyState(VK_CONTROL)<0 )
        {
            // User pressed Ctrl-A.  Let's select-all
            CWnd * wnd = GetFocus() ;
            if ( wnd && IsEdit(wnd) )
                ((CEdit *)wnd)->SetSel(0,-1) ;
        }
    }   
    return CDialog::PreTranslateMessage(pMsg);
}

Note, I stole IsEdit from this page: http://support.microsoft.com/kb/145616

I point that out partly because I want to give credit, and partly because I think the IsEdit function (comparing classname strings) is dorky and I want to give blame.

查看更多
我命由我不由天
7楼-- · 2020-02-14 06:14

You could simply use EM_SETSEL message to the textbox,

According to MSDN ,

If the start is 0 and the end is –1, all the text in the edit control is selected. If the start is –1, any current selection is deselected.

so,

SendMessage(hwndEdit,EM_SETSEL,0,-1);

Will work fine.

查看更多
登录 后发表回答