Question

C++/MFC: Adding text to CRichEditView?

Asked by: zitt

I am completely new to MFC programming ... having written several 100's of applications in Delphi; the method for creating MFC applications under Visual C++ 5.0 is confusing me.

I'm trying to write a simple application which uses a RichEdit control to display status from another thread.
I want to use a the CRichEditView to allow me to display the status in color... then save it to a RTF file so I can be examined later.

I've only been at the MFC programming for a few hours; but I'm not sure how to best proceed.

I have a menu (for testing) which executes the following function:
void CMainFrame::OnTestSendstring()
{
     CRichMFCView* ActiveView;
     //CRichMFCView* GetActiveView()
     ActiveView = (CRichMFCView*) GetActiveView();

     
     //ActiveView.StreamIn();
}

Obviously I'm not sure what I'm doing... but the ide is a click on the Test menu and Send String option and the end result is a string appears in the current active view.

I was thinking the StreamIn() member function would do this for me... but it seems like overkill.

Seems like this should be a super simple thing to do.

If i were writing this in Delphi, I'd be done already.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2001-06-12 at 16:34:45ID20134238
Tags

cricheditview

Topic

Windows MFC Programming

Participating Experts
2
Points
50
Comments
5

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. RichEdit - Justify text
    Is there a way to fully justify the text within a RichEdit? Cheers Chris
  2. MFC, VC++ 6.0, RichEdit -> CString w/ lots of text
    I have a CRichEditCtrl in my dialog box. I need to be able to exchange data between the rich edit control and the CString. Now, normally this can be accomplished with something like this: if (IsWindow (GetDlgItem (IDC_RICHEDIT_DATA)->m_hWnd)) { if (pDX->m_bSave...
  3. Using MFC DLL in delphi
    Dear all, I have some problem with loadlibrary function. I have use Visual studio to create a MFC DLL. I tried to call the function in this dll from delphi but was unable to. Please help. As I am a newbie in delphi programming, please keep the explanation simple.. Thank you...
  4. RichEdit border problem (using API, not MFC)
    I would like to increase the borders (more specifically, the spacing between text and the sides) of my dynamically created richedit control, much like the TRichEdit.BorderWidth property in Delphi. I have tried, according to the API docs, sending the EM_SETPARAFORMAT message w...
  5. mfc executables
    hi, i am having some basic knowledge in mfc.i am learning mfc my self only.i had attended one interview last week,there i had faced some questions.pls answer these so that it may useful for me in other interviews. Thanks in advance.pls answer these questions 1)differen...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: GloriousRainPosted on 2001-06-12 at 19:31:28ID: 6184255

Hi zitt,
If you want to put a text in the current active view, you have to override OnDraw function of CRichMFCView and use CDC::TextOut to do.
ex:
void CRichMFCView::OnDraw(CDC* pDC)
{
     CRichMFCDoc* pDoc = GetDocument();
     ASSERT_VALID(pDoc);
        pDC->TextOut(0,0, "Your text");
}

To override OnDraw function right click in CRichMFCView in workspace view->select Add virtual function..->one popup window appears->select Add Handler for OnDraw in "New Virtual Functions" list  

 

by: GloriousRainPosted on 2001-06-12 at 19:46:09ID: 6184286

CRichEditCtrl::StreamIn, CRichEditCtrl::StreamOut use to
write/read the contents of CRichEditCtrl object to/from the specified output stream
Example of StreamIn
<MSDN>
Example

// My callback procedure that writes the rich edit control contents
// to a file.
static DWORD CALLBACK
MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CFile* pFile = (CFile*) dwCookie;

   *pcb = pFile->Read(pbBuff, cb);

   return 0;
}

// The example code.
   // The pointer to my rich edit control.
   extern CRichEditCtrl* pmyRichEditCtrl;
   // The file from which to load the contents of the rich edit control.
   CFile cFile(TEXT("myfile.rtf"), CFile::modeRead);
   EDITSTREAM es;

   es.dwCookie = (DWORD) &cFile;
   es.pfnCallback = MyStreamInCallback;
   pmyRichEditCtrl->StreamIn(SF_RTF, es);

<MSDN>

Example of StreamOut
<MSDN>
Example

// My callback procedure that reads the rich edit control contents
// from a file.
static DWORD CALLBACK
MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CFile* pFile = (CFile*) dwCookie;

   pFile->Write(pbBuff, cb);
   *pcb = cb;

   return 0;
}

// The example code.
   // The pointer to my rich edit control.
   extern CRichEditCtrl* pmyRichEditCtrl;
   // The file to store the contents of the rich edit control.
   CFile cFile(TEXT("myfile.rtf"), CFile::modeCreate|CFile::modeWrite);
   EDITSTREAM es;

   es.dwCookie = (DWORD) &cFile;
   es.pfnCallback = MyStreamOutCallback;
   pmyRichEditCtrl->StreamOut(SF_RTF, es);
<MSDN>

 

by: zittPosted on 2001-06-12 at 20:22:55ID: 6184361

GloriousRain,

OK. I like your second example; however, can you help me figure out how I get the current view from outside the scope? I will need to send data to the view using a scope outside of all of these classes.

Let's say I have 4 RichEditView's open... How do I ensure I'm sending to the current active view?

Further, do I have to use a CFile? I will be sending text to 1 of 4 windows using a simple TCHAR buffer. I don't want to create a text file to send data to the control.

John

 

by: GloriousRainPosted on 2001-06-12 at 21:35:02ID: 6184498

"how I get the current view from
outside the scope? I will need to send data to the view using a scope outside of all of these classes.

Let's say I have 4 RichEditView's open... How do I ensure I'm sending to the current active view?"
->
I'd like to show you a general solution about this

<MSDN>
HOWTO: Get Current CDocument or CView from Anywhere
---------------------
SUMMARY
In a Microsoft Foundation Classes (MFC) Windows-based application, it is often useful to be able to get a pointer to the currently active document or view from anywhere in the program. For example, being able to access the current document from a dialog box is sometimes useful. This article discusses the simplest methods for doing so, using the relationships between MFC objects, and basic MFC architecture.

MORE INFORMATION
One of the cases where you might need a pointer to the currently active view or document is in a modal or modeless dialog box. Generally, a dialog box should be created by the view class, because the view is what deals with the application's user interface.

Because the view class is creating the dialog box, it can pass a pointer to itself, or the active document [obtained with the GetActiveDocument() function] to the dialog box. This could be done through the dialog box's constructor or some other member function. For modal dialog boxes, the view could also put data from the dialog box into the document when DoModal() returns.

These methods are generally preferable to relying on generic functions to return pointers to the currently active view or document.

To allow you to get a pointer to the currently active document from anywhere in the program, add a static member function to your CDocument derived class as follows:

Edit the document's header file as follows to add a static member function, GetDoc():


   // Document header file
   class CMyDoc : public CDocument
   {
      ...
      public:
      static CMyDoc * GetDoc();
      ...
   };
For a single document interface (SDI) application, add the following code to your SDI document's implementation file for CMyDoc::GetDoc():

   // SDI document implementation file
   CMyDoc * CMyDoc::GetDoc()
   {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
      return (CMyDoc *) pFrame->GetActiveDocument();
   }
For a multiple document interface (MDI) application, the CMyDoc::GetDoc() code should be the following:

   CMyDoc * CMyDoc::GetDoc()
   {
      CMDIChildWnd * pChild =
          ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

      if ( !pChild )
          return NULL;

      CDocument * pDoc = pChild->GetActiveDocument();

      if ( !pDoc )
         return NULL;

      // Fail if doc is of wrong kind
      if ( ! pDoc->IsKindOf( RUNTIME_CLASS(CMyDoc) ) )
         return NULL;

      return (CMyDoc *) pDoc;
   }
To allow you to get a pointer to the currently active view from anywhere in the program, add a static member function to your CView derived class as follows:

Edit the view's header file as follows to add a static member function, GetView():

   // View header file
   class CMyView
   {
      ...
      public:
      static CMyView * GetView();
      ...
   };
For an SDI application, add the following code to your SDI view's implementation file for CMyView::GetView():

   // View implementation file
   CMyView * CMyView::GetView()
   {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

      CView * pView = pFrame->GetActiveView();

      if ( !pView )
         return NULL;

      // Fail if view is of wrong kind
      // (this could occur with splitter windows, or additional
      // views on a single document
      if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
         return NULL;

      return (CMyView *) pView;
   }
For an MDI application, the CMyView::GetView() code should be the following:

   // MDI view implementation file
   CMyView * CMyView::GetView()
   {
      CMDIChildWnd * pChild =
          ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

      if ( !pChild )
          return NULL;

      CView * pView = pChild->GetActiveView();

      if ( !pView )
         return NULL;

      // Fail if view is of wrong kind
      if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
         return NULL;

      return (CMyView *) pView;
   }
Now, from anywhere in your program, where the document or view header files have been included, you can call:

   CMyDoc::GetDoc();
- Or -

   CMyView::GetView();
to get a pointer to the currently active document or view, respectively. These functions return NULL if there is no active document or view.

Note that a pointer to the application's CDocument or CView derived class is returned, not just a generic CDocument or CView pointer. This allows you to access members specific to your new class. The MDI versions of these functions, and the SDI version of the GetView() function, will use run-time type checking to verify that the document or view is of the correct class.
<MSDN>

"Further, do I have to use a CFile? I will be sending text to 1 of 4 windows using a simple TCHAR buffer.
I don't want to create a text file to send data to the control."
->If you just want to sending simple text to specific view, using SetWindowText, ex:

pmyRichEditCtrl.SetWindowText("Your text");

or folow my fisrt comment.

I am not sure you have to use CFile or not, but remember StreamIn/Out use for input/output stream. Look EDITSTREAM structure:

typedef struct _editstream {
    DWORD_PTR dwCookie;
    DWORD dwError;
    EDITSTREAMCALLBACK pfnCallback;
} EDITSTREAM;

Members
dwCookie:Specifies an application-defined value that the rich edit control passes to the EditStreamCallback callback function specified by the pfnCallback member.

dwError:Indicates the results of the stream-in (read) or stream-out (write) operation. A value of zero indicates no error. A nonzero value can be the return value of the EditStreamCallback function or a code indicating that the control encountered an error.

pfnCallback: Pointer to an EditStreamCallback function, which is an application-defined function that the control calls to transfer data. The control calls the callback function repeatedly, transferring a portion of the data with each call.

 

by: roshmonPosted on 2003-10-03 at 06:46:59ID: 9484649

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : GloriousRain

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...