Subclassing controls

This is a discussion on Subclassing controls within the Windows Programming forums, part of the Platform Specific Boards category; Hi all - I just wanted to ask a question regarding subclassing multiple controls in the one dialog. For instance ...

  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Subclassing controls

    Hi all - I just wanted to ask a question regarding subclassing multiple controls in the one dialog.

    For instance If I have a dialog with three edit controls, all of which must only take integers between 0 and 255 - the only way I think I can achieve this is to subclass the controls (which is no problem). However, what is the ettiquette with regards to this? If each of the three controls are identical, is it OK to use the same WNDPROC to subclass the controls, or should they be seperate (in effect replicating the code)?

    Thanks all.
    Visual C++ .net
    Windows XP profesional

  2. #2
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Speaking of subclassing controls - i have subclassed an edit control to make sure that the user only enters unsigned integers no greater or less than 255 or 0. But the code looks really ugly. Any suggestions for improving this, or perhaps I have overlooked a much simpler way of achieving this.

    Code:
    LRESULT CALLBACK CApp::EditRedProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	BOOL fError;
    	UINT value;
    	LRESULT lresult;
    	HWND hParent = GetParent(hwnd);
    
    	if (msg == WM_CHAR)
    	{
    		// We have to allow the backspace character through!
    		if ((char)wParam == '\b')
    			return CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
    
    		// Make sure we have an actual digit
    		if (!isdigit((char)wParam))
    			return 0;
    		else
    		{
    			lresult = CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
    
    			value = GetDlgItemInt(hParent, IDC_REDEDIT, &fError, FALSE);
    			if (value > 255 || value < 0 || fError == FALSE)
    			{
    				// Clean out the edit control
    				SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)TEXT(""));
    			}
    			return lresult;
    		}
    	}
    	else
    		return CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
    }
    Visual C++ .net
    Windows XP profesional

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by filler_bunny
    three edit controls, all of which must only take integers between 0 and 255 - the only way I think I can achieve this is to subclass the controls.
    You don't have to subclass them to achieve this. Instead you can give them the ES_NUMBER style, send an EM_SETLIMITTEXT message to limit the maximum number of characters the edit can contain to three and handle EN_CHANGE notifications (as parent WM_COMMAND messages) to ensure the 0-255 range is not exceeded.

    Having said that, if you still prefer to subclass the controls then a single subclass procedure is perfectly fine. To satisfy your curiosity you may wish to compare the returned WNDPROC values from SetWindowLongPtr when you subclass windows with the same window class name.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Excellent - thank you very much.
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 12:35 PM
  2. Controls
    By osal in forum Windows Programming
    Replies: 7
    Last Post: 06-11-2004, 11:38 AM
  3. Subclassing controls in a class
    By filler_bunny in forum Windows Programming
    Replies: 9
    Last Post: 03-21-2004, 09:12 PM
  4. I need help disabling Keyboard and Mouse input on Edit controls
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2003, 12:59 AM
  5. MFC Controls and Thread Safety :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21