How to limit characters per line in CRichEditCtrl

I've a multiline, auto horizontal and vertical scroll CRichEditCtrl in a modeless dialog, I've set the default character format to "Courier new" which is fixed-width font, I allow my user to specify the editor size such as how many lines and how many characters per line. So how to achieve that?

In other word,  how to restrict a line with fixed amount of characters and the cursor automatically proceed to next line?

Please help. Thanks.
limbeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:
Hi,

Derive Ur own class from CRichEditCtrl and map the WM_CHAR function. When user types a character it will be called. U can maintain a Counter for that class to count how many characters user has typed for that line. When it exceeds the limit put the new line character and put the character.

Something like this will be there in OnChar() function

if(m_nCharForLine == 47)
{
m_nCharForLine = 0;
CEdit::OnChar(nChar, nRepCnt, nFlags);
                        this->SendMessage(WM_CHAR,0x0d);
m_nCharForLine++;
}

Here I am restricting 48 characters for a line.

this->SendMessage(WM_CHAR,0x0d); will put the new line character.

Hope that helps.

VinExpert
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vinayak KumbarSr Program ManagerCommented:
Hi,

Then one more prob is there!!!. User can click somewhere in the middle and start editing!!!. For that put some code for that class like

void CServ3BEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
if( nChar == VK_LEFT  ||
nChar == VK_RIGHT ||
nChar == VK_UP        ||
nChar == VK_DOWN  ||
nChar == VK_DELETE)
{
int nStartPos = 0, nEndPos = 0;
GetSel(nStartPos, nEndPos);
m_nCharForLine = nStartPos;
}
}

void CServ3BEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
      int nStartPos = 0, nEndPos = 0;
      CEdit::OnLButtonDown(nFlags, point);
      GetSel(nStartPos, nEndPos);
      m_nCharForLine = nStartPos;
}


Similarly u can maintain count for the line numbers, and increament it in When u insert the new line character. And decreament it when user presses the up arrow key and handle such kind of messages.

Try it out.
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

If u want a workspace of that kind, pls give me Ur email ID. I will send u the sample wokspace which is similar to Ur requirement.

Cheers,

VinExpert
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

limbeAuthor Commented:
Hi VinExpert;

My email is limbe@pc.jaring.my, It'll be great appreciated if you could send an sample workspace.

Cheers.

Lim
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

I have sent u the sample workspace. Try it out.

Cheers,

VinExpert
0
limbeAuthor Commented:
Hi VinExpert;

A thousand thanks for your idea and sample code, it has inspired me to accomplish my task.

One last question here.

I can override the CRichEditCtrl::Paste() but when I pressed Ctrl-V in the RichEdit control, my overrided version of Paste() doesn't get call, I try to map WM_PASTE from Class wizard, and I found that there is no WM_PASTE message, how to do that?

Thanks.
0
Vinayak KumbarSr Program ManagerCommented:
Hi limbe,

The documentation for WM_PASTE says"An application sends a WM_PASTE message to an edit control or combo box to copy the current content of the clipboard to the edit control at the current caret position. Data is inserted only if the clipboard contains data in CF_TEXT format". So I dont think we can override that function like that. Our application has to send that message to copy clipboard  contents to the edit control or combo box. But if u want to trap the Ctrl+V into ur control, then map the PreTranslateMessage() function to that class and place the following code.

BOOL CIntelligent::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == 0x56 && (GetKeyState(VK_CONTROL) & 0x8000))      
      AfxMessageBox("Pasted");
return CEdit::PreTranslateMessage(pMsg);
}

Here U can write Ur own Paste function say OnMyPaste() and call that function instead of messagebox.

Try it out.
VinExpert
0
limbeAuthor Commented:
Thanks. I used your suggesstion in OnKeyDown() handler, it works fine.

OK. This is your mark. See you around.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.