Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i just wrote this and used SetWindowSubclass and for once, something i did worked almost without a hitch. ALMOST. when i press enter on the keyboard while the edit control it's subclassed to has the focus, it does precisely what i want it to. EXCEPT, that every time i press it, it makes a loud f***ing dinging noise. Is there anyway to fix this?

C++
LRESULT CALLBACK EditSubclass(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
	switch (msg)
	{
		case WM_KEYDOWN:
		{
			switch(wParam)
			{
				case VK_RETURN:
				{
					SendMessage(sendbutton, BM_CLICK, 0, 0);
					SetFocus(editbox);
				}
			}
			return TRUE;
		}
	}
	return DefSubclassProc(hwnd, msg, wParam, lParam);
}
Posted
Comments
FatalCatharsis 29-Aug-11 22:25pm    
in the end the exact solution to my problem was to override the WM_CHAR message instead of the WM_KEYDOWN message, and to place the return TRUE; within the VK_RETURN case. Thanks Ivertheengine!

It beeps because, being a single line, pressing enter is considered an attempt to insert another line.

On dialog boxes, the Enter key is trapped by the dialog procedure and sent to the default pushbutton (if any), otherwise left to the edit control itself.
On other window, such "trapping" requires a messge-loop that check the presence of a dialog message before dispatching to the focused window.

Something like
/* Window procedure definition */
LRESULT WINAPI CALLBACK winproc(HWND h, UINT m, WPARAM w, LPARAM z)
{
    ....
}


/* may be WinMain, _tmain, _tWinMain or whatever entry point */
int main() 
{
    /* Class registration stuff */
    WNDCLASSEX wcx = { ..... };
    ....
    RegisterWindowClassEx(&wcx);

    
    /* WIndow Creation */ 
    HWND hwnd = CreateWindow( .... );
    ....
    
    /* messge loop */
    for(;;)
    {
        MSG msg;
        if(GetMessage(&msg,0,0,0)<=0) break; //< exit on error or "quit" 
        if(IsDialogMessage(hwnd,&msg)) continue; //< skip regular dispatching
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
 
Share this answer
 
Comments
FatalCatharsis 29-Aug-11 18:57pm    
I'm not at all sure what you meant by this, but when i put that line in my message loop, no text was able to be typed in. and still dinged as well :P.
Emilio Garavaglia 30-Aug-11 7:42am    
Is there a default push-button in your dialog?
I am not sure if this is any help. I have had the same dinging problem on sub classed controls and found the problem was caused by 'swallowing' the WM_KEYDOWN.
In the message loop WM_KEYDOWN + WM_KEYUP = WM_CHAR. Ding occurs when system sees WM_KEYUP without WM_KEYDOWN.
 
Share this answer
 
Comments
FatalCatharsis 29-Aug-11 22:20pm    
oh, after reading this again, this would make perfect sense, cause before i was overriding just the keydown, preventing the default message from being passed, hence it not seeing a keydown, hence the beep. then when i override the WM_CHAR instead, it sees the WM_KEYDOWN, as well as the WM_KEYUP. I'll delete my post and accept this solution cause you posted before i did :P
I did a google search for "enter edit control beep" and the first result contains the following:

ES_MULTILINE

Designates a multiline edit control. The default is single-line edit control.

When the multiline edit control is in a dialog box, the default response to pressing the ENTER key is to activate the default button. To use the ENTER key as a carriage return, use the ES_WANTRETURN style.

When the multiline edit control is not in a dialog box and the ES_AUTOVSCROLL style is specified, the edit control shows as many lines as possible and scrolls vertically when the user presses the ENTER key. If you do not specify ES_AUTOVSCROLL, the edit control shows as many lines as possible and beeps if the user presses the ENTER key when no more lines can be displayed.
 
Share this answer
 
Comments
FatalCatharsis 29-Aug-11 1:26am    
but it's not a multiline control. it never attempts to display a line outside the dimensions of the control so is there another reason it would beep? been googling furiously.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900