Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

c++ - win32 select all on edit ctrl (textbox)

I am creating my textbox with these options. I can Copy / Cut / Paste / Undo, but when I hit Select All it doesn't select all. I can right click and click Select All but CTRL + A doesn't 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...