Link to home
Start Free TrialLog in
Avatar of limbe
limbe

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Vinayak Kumbar
Vinayak Kumbar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vinayak Kumbar
Vinayak Kumbar

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.
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
Avatar of limbe

ASKER

Hi VinExpert;

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

Cheers.

Lim
Hi,

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

Cheers,

VinExpert
Avatar of limbe

ASKER

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.
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
Avatar of limbe

ASKER

Thanks. I used your suggesstion in OnKeyDown() handler, it works fine.

OK. This is your mark. See you around.