Thread: Word Wrap

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    96

    Word Wrap

    Ok I need some help getting word wrap to work. I saw a thread on this board where to get word wrap to work, you delete the edit control, and then recreate it based on if word wrap is on or off. I'm basically trying to do word wrap how notepad does it. Anyways here is my code:

    Code:
    #include <windows.h>
    #include "editcontrol.h"
    #include "wordwrap.h"
    
    CWordWrap::CWordWrap()
    {
    	
    }
    
    CWordWrap::~CWordWrap()
    {
    	
    }
    
    bool CWordWrap::DoWordWrap(HWND hParentWindow, int iWordWrapMenuID, CEditControl EditControl, DWORD dwStyleEx, DWORD dwStyle, int iX, int iY, 
                               int iWidth, int iHeight, HINSTANCE hInstance)
    {	
    	m_hMenu = GetMenu(hParentWindow);
    				
    	if(GetMenuState(m_hMenu, iWordWrapMenuID, MF_BYCOMMAND) == MF_CHECKED)
    	{		
    		m_bWordWrap = false;
    	}
    	
    	else
    	{		
    		m_bWordWrap = true;
    	}
    	
    	LockWindowUpdate(hParentWindow);	
    	
    	m_dwTextLength = GetWindowTextLength(EditControl.g_hEdit);
    	
    	m_pszFileText = new char[m_dwTextLength];
    	
    	if(!m_pszFileText)
    	{
    		MessageBox(hParentWindow, "Can't allocate memory!", "Error!",
    		           MB_ICONEXCLAMATION | MB_OK);
    		return false;
    	}
    	
    	GetWindowText(EditControl.g_hEdit, m_pszFileText, m_dwTextLength);
    		
    	DestroyWindow(EditControl.g_hEdit);
    	
    	EditControl.Create(dwStyleEx,
    		               dwStyle | (m_bWordWrap ? WS_HSCROLL | ES_AUTOHSCROLL : 0),
    		               iX,
                           iY,
    	                   iWidth,
    	                   iHeight, 
    		     	       hParentWindow,		     		                   
    		     	       hInstance);
    	
    	LockWindowUpdate(NULL);	
    	
    	SetFocus(EditControl.g_hEdit);
    	
    	SetWindowText(EditControl.g_hEdit, m_pszFileText);
    	
    	delete[] m_pszFileText;
    		
    	CheckMenuItem(m_hMenu, iWordWrapMenuID, (m_bWordWrap ? MF_CHECKED : MF_UNCHECKED));	
    		
    	return true;
    }
    Here is my edit control code:
    Code:
    #include <windows.h>
    #include "editcontrol.h"
    
    CEditControl::CEditControl()
    {
    	
    }
    
    CEditControl::~CEditControl()
    {
    	
    }
    
    bool CEditControl::Create(DWORD dwStyleEx, DWORD dwStyle, int iX, int iY, 
                              int iWidth, int iHeight, HWND hParentWindow, 
                              HINSTANCE hInstance)
    {	    
    	g_hEdit = CreateWindowEx(
    	          dwStyleEx,
    	          "EDIT",
    	          NULL,
    	          dwStyle,
    	          iX,
    	          iY,
    	          iWidth,
    	          iHeight,
    	          hParentWindow,
    	          (HMENU)g_iEditControlID,
    	          hInstance,
    	          NULL
    	          );
    
         if(!g_hEdit)
         {
         	MessageBox(hParentWindow, "Can't create edit control!", "Error!",
         	           MB_ICONEXCLAMATION | MB_OK);
         	return false;
         }    
    	          
    	SendMessage(g_hEdit, WM_SETFONT, 
    	            (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
    	            MAKELPARAM(TRUE, 0));
    	            
    	return true && g_hEdit && g_iEditControlID;                    
    }
    In my message loop here is the call:
    Code:
    g_WordWrap.DoWordWrap(hwnd, IDM_FORMAT_WORDWRAP, g_EditControl, WS_EX_CLIENTEDGE,
    		     	                           WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN,
    		                                   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    	                                       CW_USEDEFAULT,  g_Window.g_hThisInstance);
    The function succuessfully completes it's job because EditContol.g_hEdit does not return 0. Unfortunately the edit control does not reappear on the screen.
    Last edited by sethjackson; 09-19-2005 at 04:10 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    g_WordWrap.DoWordWrap(hwnd, IDM_FORMAT_WORDWRAP, g_EditControl, WS_EX_CLIENTEDGE,
    		     	                           WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN,
    		                                   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    	                                       CW_USEDEFAULT,  g_Window.g_hThisInstance);
    CW_USEDEFAULT can not be used for a child window. You need to compute an appropriate position and size for a child window. For example, you can use GetClientRect to get the size of the parent window.
    Quote Originally Posted by MSDN CreateWindowEx
    CW_USEDEFAULT is valid only for overlapped windows; if CW_USEDEFAULT is specified for a pop-up or child window, the nWidth and nHeight parameter are set to zero.
    Also, I think you need to add one to make room for the nul terminator:
    Code:
    m_dwTextLength = GetWindowTextLength(EditControl.g_hEdit) + 1;

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Hmm if I change the Word Wrap call to
    Code:
    g_WordWrap.DoWordWrap(hwnd, IDM_FORMAT_WORDWRAP, g_EditControl, WS_EX_CLIENTEDGE,
    		     	                           WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL |  ES_WANTRETURN,
    		                                   0, 0, 100,
    	                                       100,  g_Window.g_hThisInstance);
    it works sort of, unfortunately the window is always created with the WS_HSCROLL and ES_AUTOHSCROLL.

    So how do I size the edit control to the size of the main window?
    Last edited by sethjackson; 09-20-2005 at 09:05 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Ok here is what I have now.
    Code:
    g_WordWrap.DoWordWrap(hwnd, IDM_FORMAT_WORDWRAP, 
                                                g_EditControl, 
                                                WS_EX_CLIENTEDGE, WS_CHILD | 
                                                WS_VISIBLE | WS_VSCROLL | 
                                                ES_MULTILINE | ES_AUTOVSCROLL | 
                                                ES_WANTRETURN,  0, 0, g_rcClient.right,
                                                g_rcClient.bottom,  
                                                Window.g_hThisInstance);
    In WM_SIZE I have this
    Code:
    case WM_SIZE:
         {
            if(wParam != SIZE_MINIMIZED)
       	{
    	   	GetClientRect(hwnd, &g_rcClient);
    		     		   		 		
    		MoveWindow(g_EditControl.g_hEdit, 0, 0, 
    	                              g_rcClient.right, g_rcClient.bottom, true);
    	}
         }
         break;
    For some reason though the edit control style doesn't change. Why is this?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Hmm this still isn't working. The edit control doesn't get resized properly when I destroy it.

    Code:
    bool CWordWrap::DoWordWrap(HWND hParentWindow, int iWordWrapMenuID, CEditControl EditControl, DWORD dwStyleEx, DWORD dwStyle, int iX, int iY, 
                               int iWidth, int iHeight, HINSTANCE hInstance)
    {	
    	m_hMenu = GetMenu(hParentWindow);
    				
    	if(GetMenuState(m_hMenu, iWordWrapMenuID, MF_BYCOMMAND) == MF_CHECKED)
    	{		
    		m_bWordWrap = false;
    	}
    	
    	else
    	{		
    		m_bWordWrap = true;
    	}
    	
    	LockWindowUpdate(hParentWindow);	
    	
    	m_dwTextLength = GetWindowTextLength(EditControl.g_hEdit);
    	
    	m_pszFileText = new char[m_dwTextLength];
    	
    	if(!m_pszFileText)
    	{
    		MessageBox(hParentWindow, "Can't allocate memory!", "Error!",
    		           MB_ICONEXCLAMATION | MB_OK);
    		delete[] m_pszFileText;           
    		return false;
    	}
    	
    	GetWindowText(EditControl.g_hEdit, m_pszFileText, m_dwTextLength) + 1;
    		
    	DestroyWindow(EditControl.g_hEdit);
    	
    	
    	EditControl.Create(dwStyleEx,
    		               dwStyle | (m_bWordWrap ? 0 : WS_HSCROLL | ES_AUTOHSCROLL),
    		               iX,
                           iY,
    	                   iWidth,
                           iHeight, 
    		               hParentWindow,		     		                   
    		               hInstance);
    	
    	SetFocus(EditControl.g_hEdit);
    	
    	SetWindowText(EditControl.g_hEdit, m_pszFileText);	
    		
    	CheckMenuItem(m_hMenu, iWordWrapMenuID, (m_bWordWrap ? MF_CHECKED : MF_UNCHECKED));	
    	
    	LockWindowUpdate(NULL);
    	
    	delete[] m_pszFileText;
    		
    	return true;
    }
    I think it is not resizing right because it doesn't receognize the edit control handle.

    How can I fix this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to a file with word wrap?
    By Djanvk in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2008, 05:09 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Word Wrap
    By osal in forum Windows Programming
    Replies: 4
    Last Post: 07-02-2004, 11:16 AM