Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetSelectionCharFormat difficulties when selection contains different charset

SetSelectionCharFormat difficulties when selection contains different charset

Scheduled Pinned Locked Moved C / C++ / MFC
jsontutorialquestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JJeffrey
    wrote on last edited by
    #1

    I am using SetSelectionCharFormat in a RichEditCtrl object on my dialog box to allow a user to change the font/bold/etc text he is typing in the rich edit box. Basically the function should change the character formatting as needed; the code is as follows:

    void CRichEditProjDlg::OnSelchangeFontsize()
    {
    DWORD Selection=0, FontSize=0;
    CString SelFontSize;
    Selection = m_FontSize.GetCurSel(); //Get the current Selection

    m\_FontSize.GetLBText(Selection, SelFontSize);    //Get the requested Font Size from a drop down box
    FontSize = \_ttoi(SelFontSize);
    
    CHARFORMAT Cfm;
        Cfm.cbSize = sizeof(CHARFORMAT);
    m\_RichCatch.GetSelectionCharFormat(Cfm);        //Get the char set of the selection
    
    Cfm.yHeight = FontSize\*20;                      //Calculate the new font size
    
    m\_RichCatch.SetSelectionCharFormat(Cfm);        //Set the new font size
    m\_RichCatch.SetFocus();                         //Update the Rich Edit Box
    UpdateData(TRUE);
    

    }

    void CRichEditProjDlg::OnBold()
    {
    CHARFORMAT Cfm;
    Cfm.cbSize = sizeof(CHARFORMAT);

    m\_RichCatch.GetSelectionCharFormat(Cfm);       //Get the char set of selection
    
    Cfm.dwMask = CFM\_BOLD;                         //Apply "boldness"
    Cfm.dwEffects ^= CFE\_BOLD; 
    
    m\_RichCatch.SetSelectionCharFormat(Cfm);       //Set the char set
    m\_RichCatch.SetFocus();
    

    }

    This bold-ing function works fine, but the Font size one only works when the selected text are all of the same font size. For example, if all of the selected text are of font size 10, I selected all of them and chose Font size 20, they would all change to font size 20. But if the selected text was of differing font sizes, say the first few words were font size 10 and the rest font size 11, the code does not change the font size of any of the selelcted text. Am I approaching the function in the wrong way? Or must I do an internal check for different font sizes and change them all separately somehow? Please feel free to ask for more information if needed. Thank you.

    S 1 Reply Last reply
    0
    • J JJeffrey

      I am using SetSelectionCharFormat in a RichEditCtrl object on my dialog box to allow a user to change the font/bold/etc text he is typing in the rich edit box. Basically the function should change the character formatting as needed; the code is as follows:

      void CRichEditProjDlg::OnSelchangeFontsize()
      {
      DWORD Selection=0, FontSize=0;
      CString SelFontSize;
      Selection = m_FontSize.GetCurSel(); //Get the current Selection

      m\_FontSize.GetLBText(Selection, SelFontSize);    //Get the requested Font Size from a drop down box
      FontSize = \_ttoi(SelFontSize);
      
      CHARFORMAT Cfm;
          Cfm.cbSize = sizeof(CHARFORMAT);
      m\_RichCatch.GetSelectionCharFormat(Cfm);        //Get the char set of the selection
      
      Cfm.yHeight = FontSize\*20;                      //Calculate the new font size
      
      m\_RichCatch.SetSelectionCharFormat(Cfm);        //Set the new font size
      m\_RichCatch.SetFocus();                         //Update the Rich Edit Box
      UpdateData(TRUE);
      

      }

      void CRichEditProjDlg::OnBold()
      {
      CHARFORMAT Cfm;
      Cfm.cbSize = sizeof(CHARFORMAT);

      m\_RichCatch.GetSelectionCharFormat(Cfm);       //Get the char set of selection
      
      Cfm.dwMask = CFM\_BOLD;                         //Apply "boldness"
      Cfm.dwEffects ^= CFE\_BOLD; 
      
      m\_RichCatch.SetSelectionCharFormat(Cfm);       //Set the char set
      m\_RichCatch.SetFocus();
      

      }

      This bold-ing function works fine, but the Font size one only works when the selected text are all of the same font size. For example, if all of the selected text are of font size 10, I selected all of them and chose Font size 20, they would all change to font size 20. But if the selected text was of differing font sizes, say the first few words were font size 10 and the rest font size 11, the code does not change the font size of any of the selelcted text. Am I approaching the function in the wrong way? Or must I do an internal check for different font sizes and change them all separately somehow? Please feel free to ask for more information if needed. Thank you.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Ummm - maybe you should set Cfm.dwMask to CFM_SIZE when setting the font size? Also - why bother getting the selection's char format, when you can specify only the attributes you want to set within the CHARFORMAT structure?

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        Ummm - maybe you should set Cfm.dwMask to CFM_SIZE when setting the font size? Also - why bother getting the selection's char format, when you can specify only the attributes you want to set within the CHARFORMAT structure?

        J Offline
        J Offline
        JJeffrey
        wrote on last edited by
        #3

        Thanks. That seems to have worked. Guess i missed that part. As for the question, I guess I'm used to the "Get" then "Set" process. Didn't think of any other way. Thanks again.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups