I want when a “LBUTTONDOWN” on edit control, empty the text box.
I know how can I empty the text box , but I don’t know where is the place where this event is added .
My dialog function:
INT CALLBACK dlgProc(HWND hwnd, unsigned int msg, WPARAM wp, LPARAM lp){
switch(msg){
case WM_INITDIALOG:
SetDlgItemText(hwnd, IDC_EDIT1, L"Please enter the txt");
break;
case WM_COMMAND:
switch(LOWORD(wp)){
case BTN_EXIT:
DestroyWindow(hwnd);
break;
case IDC_BUTTON1:
int len = GetWindowTextLength(GetDlgItem(hwnd,IDC_EDIT1));
if(len > 0){
TCHAR *buff = new TCHAR[len+1];
GetDlgItemText(hwnd, IDC_EDIT1, buff, len+1);
MessageBox(NULL,buff,L"Error message",MB_OK);
delete buff;
}
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return false;
}
return true;
}
What I suspect that you really want is to respond to the control receiving input focus rather than just the button down event. For example, suppose the user uses the mouse button to set the input focus on the edit control, then types, and then clicks on the edit control again, whilst it currently has the focus. You presumably do not want the user’s text to be cleared. Or perhaps they set input focus using the keyboard, e.g. TAB. Again I suspect you would want that action to clear the contents.
Assuming my understanding is correct then you should listen for the
EN_SETFOCUSnotification in your dialog procedure’sWM_COMMANDhandler. This will fire no matter how the user brings focus to the edit control, either using the mouse or by using the keyboard.In your code you just need to expand your
switchstatement in theWM_COMMAND: