CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2012
    Posts
    20

    Why wont thsi mfc code work?

    I'm trying to write a string from CMainFrm to the view window. The View window is inherits CRichEdit. I tried GetActiveView() instead of GetActiveWindow() and it still didn't work. Why won't this work?

    Code:
    	CtestDoc* pDoc=(CtestDoc*)GetActiveDocument();
    	CtestView* pActiveView=(CtestView*)GetActiveWindow();
    	pActiveView->SetWindowText((CString)"Please Help me");//why isn't this working?
    	pDoc->UpdateAllViews(NULL,0,NULL);

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Why wont thsi mfc code work?

    UpdateAllViews should make views to redraw taking the last document state. As you never change the doc, most likely you just lose your text applied directly to view window.
    Best regards,
    Igor

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by printz0r View Post
    Code:
    	CtestDoc* pDoc=(CtestDoc*)GetActiveDocument();
    	CtestView* pActiveView=(CtestView*)GetActiveWindow();
    	pActiveView->SetWindowText((CString)"Please Help me");//why isn't this working?
    And where in the view do you expect to set this text?
    And why do you perform such a strange and absolutely unneeded casting
    Code:
    (CString)"Please Help me"
    Last edited by VictorN; May 31st, 2012 at 07:47 AM.
    Victor Nijegorodov

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why wont thsi mfc code work?

    Assuming it's an SDI app, use GetActiveView, not GetActiveWindow.
    If it's MDI, use GetActiveFrame()->GetActiveView();

    Read the documentation for CDocument::UpdateAllViews(). The way you're using it there makes no sense.

  5. #5
    Join Date
    Mar 2012
    Posts
    20

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by Igor Vartanov View Post
    UpdateAllViews should make views to redraw taking the last document state. As you never change the doc, most likely you just lose your text applied directly to view window.
    This makes sense. So how would I write strings to the document? Or highlight certain words in the document and display it to the active view?

    Code:
    FILE *f;
    CMyDoc *pDoc=(CMyDoc *)GetActiveDocument();
    f=fopen(pDoc,"wb");
    char buffer[] = { 'x' , 'y' , 'z' };
    fwrite (buffer , 1 , sizeof(buffer) , f);
    fclose (f);
    pDoc->UpdateAllViews(NULL,0,0);
    Would the above code work?

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by printz0r View Post
    This makes sense. So how would I write strings to the document? Or highlight certain words in the document and display it to the active view?

    Code:
    FILE *f;
    CMyDoc *pDoc=(CMyDoc *)GetActiveDocument();
    f=fopen(pDoc,"wb");
    char buffer[] = { 'x' , 'y' , 'z' };
    fwrite (buffer , 1 , sizeof(buffer) , f);
    fclose (f);
    pDoc->UpdateAllViews(NULL,0,0);
    Would the above code work?
    That may work but makes no sense as long as you are using MFC.
    Last edited by ovidiucucu; May 31st, 2012 at 01:35 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Mar 2012
    Posts
    20

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by ovidiucucu View Post
    That may work but makes no sense as long as you are using MFC.
    Please enlighten me. What is a better way to view, edit modify the document and view class? Lets say I want certain highlighted in my document?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by printz0r View Post
    Please enlighten me. What is a better way to view, edit modify the document and view class? Lets say I want certain highlighted in my document?
    I have no idea what you're trying to do. It seems like you're not sure how the doc/view architecture works. Your document stores the data and your view renders it. Your document should have some member or members that store its data. You typically populate that data from the view class or through serialization. If data in the document has been changed, you call UpdateAllViews to let the view know they need to take appropriate action. Their OnUpdate method will be called.

    The code that you posted makes absolutely no sense at all to me. Why are you passing a CDocument* to fopen?

    My advice would be to stop immediately and work through a good tutorial on MFC and doc/view. You'll never get it right just by guessing.
    Last edited by GCDEF; May 31st, 2012 at 02:55 PM.

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Why wont thsi mfc code work?

    First of all, I'm totally agree with GCDEF.

    Second:
    dear printz0r, you haven't answered my following question yet
    Quote Originally Posted by VictorN View Post
    And where in the view do you expect to set this text?
    Quote Originally Posted by printz0r
    Code:
    	CtestDoc* pDoc=(CtestDoc*)GetActiveDocument();
    	CtestView* pActiveView=(CtestView*)GetActiveWindow();
    	pActiveView->SetWindowText((CString)"Please Help me");//why isn't this working?
    And why do you perform such a strange and absolutely unneeded casting
    Code:
    (CString)"Please Help me"
    Victor Nijegorodov

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Why wont thsi mfc code work?

    Quote Originally Posted by printz0r View Post
    Please enlighten me. What is a better way to view, edit modify the document and view class? Lets say I want certain highlighted in my document?
    OK, just let me try!

    Understanding document-view (SDI of MDI) architecture isn't difficult.
    Basically, it has the following components:
    • A document object (of a class derived from CDocument), to keep and serialize (read/write) data
    • A view object (of a class derived from CView), to display data.
    • A frame object (of a class derived from CFrameWnd) which keep a view along with menu and control bars.


    The document, view and frame, are usually kept together in a document template (of a class derived from CDocTemplate) which finally is managed by application (of a class derived from CWinApp).
    The basic difference between SDI (Single Document Interface) si MDI (Multiple Document Interface) is, the second may have more than one document template.
    There are many other things to be detailed but this is the basis.

    Usually, the MFC wizard automatically adds all the above components to your project. However, you have to complete them according to your needs/requirements.
    All these components are interconnected in the framework and each offers (virtual) methods which allow you to do that job.

    Let's say we have a document which must contain a name and a numeric identifier (this is the "data", which I mention above).
    Code:
    class CMyDocument : public CDocument
    {
    // ...
    // Attributes
    public:
       CString m_strName;
       UINT m_nID;
    // ...
    };
    To serialize (read/write) the document, the document class offers a virtual method CDocument::Serialize which is automatically called when user chooses File/Open or File/Save As, respectively. We can notice that method has an argument of type CArchive. CArchive is an MFC class which helps us to easily make the serialization.

    In our case, all we have to do is:
    Code:
    void CMyDocument::Serialize(CArchive& ar)
    {
       if(ar.IsStoring())
       {
          // write into archive
          ar << m_strName;
          ar << m_nID;
       }
       else
       {
          // read from archive
          ar >> m_strName;
          ar >> m_nID;
       }
    }
    Also said before, the view is responsible to display data.
    Let's say, we have a class directly derived from CView and we have to display data contained in the document, in the following form: "Name: Baba Safta, ID: 666".
    Again, taking a look, we can discover CView::OnDraw virtual method.
    So, let's override it and do the job:
    Code:
    void CMyView::OnDraw(CDC* pDC)
    {
       // Get a pointer to the document object
       CMyDocument* pDoc = GetDocument();
       ASSERT_VALID(pDoc);
    
       // Format and display data
       CString strData;
       strData.Format(_T("Name: &#37;s, ID: %u"), pDoc->m_strName, pDoc->m_nID);
       pDC->TextOut(10, 10, strData);
    }
    Hopefully, you agree it has much more sense now and it's much more light around there...

    I gave just a brief example, for start undersatanding the MFC document-view mechanism.
    Further, you have to sweat a little by reading the MFC documentation and/or books about MFC. Do more tests and finally, of course, may ask in forum if something is still unclear.
    And important, as long as you are using MFC, get rid of old habits. Usually it's not necessary to add non-MFC stuff, like CRT functions calls (in our case, using fopen and fwrite, as long as we have the MFC's CArchive) or using C-style buffers as long as we have CString.

    When writing MFC applications, follow The MFC Way, i.e. "When in Rome, do as Romans do" (see my signature ).
    Last edited by ovidiucucu; June 1st, 2012 at 02:29 AM. Reason: typos
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Why wont thsi mfc code work?

    [ continued ]
    Hopefully you have noted my previous post, let's return a little bit closer to OP, talking about CRichEditView, then about how to highlight a text.

    CRichEditView and CRichEditDoc (deeply derived from CView and CDocument) are specialized for handling rich-edit format.
    CRichEditDoc implements serialization itself, so you have nothing to do more, except if you want to do something very special.

    If you want to "highlight" some (selected) text you can use CRichEditView::SetCharFormat.
    Here is one example:
    1. Add a command in menu, let's say it ID_EDIT_HIGHLIGHTSELECTION.
    2. Map this command in the view class.
    3. Write this code in the command message handler:
      Code:
      void CMyRichEditView::OnEditHighlightSelection()
      {
         // get selection
         CHARRANGE cr = {0};
         GetRichEditCtrl().GetSel(cr);
         const int nLength = cr.cpMax - cr.cpMin;
         if(nLength == 0)
            return; // no selected text
      
         // keep in mind the current character format
         CHARFORMAT2& cfOld = GetCharFormatSelection();
      
         // get selected text
         CString strText;
         SendMessage(EM_GETSELTEXT, 0, 
            (LPARAM)strText.GetBufferSetLength(nLength + 1));
         strText.ReleaseBuffer();
      
         // replace selection with text having yellow background
         CHARFORMAT2 cfNew;
         ZeroMemory(&cfNew, sizeof(CHARFORMAT2));
         cfNew.cbSize = sizeof(CHARFORMAT2);
         cfNew.crBackColor = RGB(255, 255, 0);
         cfNew.dwMask = CFM_BACKCOLOR;
         SetCharFormat(cfNew);
         GetRichEditCtrl().ReplaceSel(strText);
      
         // put back the old character format
         SetCharFormat(cfOld);
      }

    There is nothing more to do for serialization. As aready said, CRichEditDoc takes care itself to save changes, when the user chooses "File/Save, "File/Save As" menu or hits Ctrl+S.
    Last edited by ovidiucucu; June 1st, 2012 at 05:00 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured