?
Solved

How to limit characters per line in CRichEditCtrl

Posted on 1999-12-12
8
Medium Priority
?
412 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:limbe
  • 5
  • 3
8 Comments
 
LVL 8

Accepted Solution

by:
Vinayak Kumbar earned 300 total points
ID: 2275999
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
 
LVL 8

Expert Comment

by:Vinayak Kumbar
ID: 2276005
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
 
LVL 8

Expert Comment

by:Vinayak Kumbar
ID: 2276008
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:limbe
ID: 2276418
Hi VinExpert;

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

Cheers.

Lim
0
 
LVL 8

Expert Comment

by:Vinayak Kumbar
ID: 2276638
Hi,

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

Cheers,

VinExpert
0
 

Author Comment

by:limbe
ID: 2295695
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
 
LVL 8

Expert Comment

by:Vinayak Kumbar
ID: 2296784
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
 

Author Comment

by:limbe
ID: 2309967
Thanks. I used your suggesstion in OnKeyDown() handler, it works fine.

OK. This is your mark. See you around.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question