For an edit box, select all and deselect all (prerequisite: the edit box gets the Focus)
Create a new dialog based MFC application named "d1". The interface is as follows:
Add a member variable bool m_bselectall for the dialog box class to control the full selection status. In addition, to overload the pretranslatemessage event of the dialog box class, add the followingCode:
Bool cd1dlg: pretranslatemessage (MSG * PMSG) {If (getasynckeystate (vk_control) & 0x8000) & (getasynckeystate ('A') & 0x8000) & PMSG-> hwnd = m_pedit-> m_hwnd) {If (m_bselectall) {m_pedit-> setsel (-1 ); // cancel all selection} else {m_pedit-> setsel (0,-1); // select all} m_bselectall =! M_bselectall; return true;} return cdialog: pretranslatemessage (PMSG );}
In this way, when the edit box gets the focus, if you press Ctrl + A, the content in the edit box will be "all selected" or "unall selected ".
However, the above m_bselectall is used to determine whether the "All selected" status has a problem, that is, when all is selected, the user clicks the edit box to cancel all selection, but at this time, m_bselectall is still in the all-selected state, that is, this sign (m_bselectall) is inconsistent with the actual all-selected state. Therefore, another method is used to verify whether the selected content is in the all-selected status (that is, whether the length of the selected content is consistent with the content length of the edit box). The Code is as follows:
Bool cd1dlg: pretranslatemessage (MSG * PMSG) {If (PMSG-> hwnd = m_pedit-> m_hwnd & (getasynckeystate (vk_control) & 0x8000) & (getasynckeystate (_ T ('A') & 0x8000) {cstring txt; int start, end; m_pedit-> getwindowtext (txt ); m_pedit-> getsel (START, end); If (txt. getlength () = end-Start) // In the all-selected status {m_pedit-> setsel (-1); // cancel all-selected} else {m_pedit-> setsel (0, -1); // select all} return true;} return cdialog: pretranslatemessage (PMSG );}
Description: Getasynckeystate () only checks whether a key is pressed, not case sensitive!