Thread: subclassed richedit to handle wm_char

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    subclassed richedit to handle wm_char

    I'm trying to figure out how to make a rich edit control auto indent by inserting the same number of tab characters in a new line as the previous line has. (like block indent in a code editor) I've figured out how to get it done by subtracting 1 and then dividing point.y by 16 to get the correct line number. The problem with this is if the font size (and probably the typeface) is changed 16 will no longer be the correct value. So, I created a CHARFORMAT and retrieved the settings of the font to the RichEDIT control. The value of the yHeight member is 195. Could somebody please explain exactly what 195 means if there's a way to convert it to logical units which is what the y member of a POINT is.

    I'm posting the subclass procedure in case you need to see what I'm trying to do. This is my first attempt at programming so in advance, please forgive me for my cryptic coding style. And I'm not looking for someone to do the work for me, just point me in the right direction and I'll get it done.

    Code:
    LRESULT CALLBACK MDIChildEditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CHAR:
    			switch(wParam)
    			{
    				case 0x0D:
    				{
    					POINT point;		//the y member starts at 1 and increments by 16
    					int num = 0;
    					BOOL bAdd = TRUE;
    					
    					CHARFORMAT cF;			//the yHeight member becomes 195 after I press enter
    					
    					cF.cbSize	= sizeof(CHARFORMAT);
    					cF.dwMask	= CFM_SIZE;
    					
    					GetCaretPos(&point);
    					SendMessage(hwnd, EM_GETCHARFORMAT, (WPARAM)TRUE, (LPARAM)&cF);
    					
    					int iLength = SendMessage(hwnd, EM_LINELENGTH, (WPARAM)(point.y-1)/16, 0);
    					
    					LPSTR lpStr = new char[iLength+1];
    					if(!lpStr)
    					{
    						MessageBox(NULL, "Failed to allocate mem for lpStr", "Error", MB_OK);
    						return 0;
    					}
    					
    					SendMessage(hwnd, EM_GETLINE, (WPARAM)(point.y-1)/16, (LPARAM)lpStr);
    					lstrcat(lpStr, '\0');
    					
    					SendMessage(hwnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)"\r\n");
    					
    					while(bAdd)
    					{
    						if(lpStr[num] == '	')
    						{
    							SendMessage(hwnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)"	");
    							num++;
    							continue;
    						}
    						else
    						{
    							bAdd = FALSE;
    						}
    					}
    					delete lpStr;
    					return 0;
    				}
    			}
    		default:
    			return CallWindowProc(g_pfPrevMDIChildEditProc, hwnd, msg, wParam, lParam);
    	}
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I could have got the wrong idea but the EM_LINEFROMCHAR message with -1 as wParam will return the line number of the caret. Much easier.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thanks worked like a charm

    I knew the solution would be so simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with setting text into LTEXT using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM