CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Binding Data to Silverlight 4.0 Controls Using ASP.NET MVC Framework 2.0
  • ADO.NET Data Services in the .NET Framework
  • Visual C++ Programming: What's new for MFC library in VC++ 2010?
  • Microsoft Visual Studio LightSwitch and What It Can Do For You

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old February 15th, 2010, 12:46 AM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Rich Text box Questions

    ok so I manage to create a RICHEDIT with:
    [code]rtfctl = CreateWindow("RICHEDIT","text",WS_BORDER|WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|WS_HSCROLL,10,10,300,300,MAINSS,0,hInstance,0);[\CODE]

    Now heres where I'm stuck at

    1. I have it set to multiline and both scrollbars are visible. But the text won't go beyond horizontally.
    If I remove the multiline then it will go beyond it but then it will only be that one line.

    2. How can I introduce syntax highlighting for it?

    In VB I would then find the word and use selcolor and similar functions to change the color but this method would slow down after a couple of lines.

    So I asked about it and someone told me I need to parse it. How do I go about doing that?

    Thanks
    Reply With Quote
      #2    
    Old February 15th, 2010, 07:29 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 457
    olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)
    Re: Rich Text box Questions

    Quote:
    In VB I would then find the word and use selcolor and similar functions to change the color
    In C, once you find a word, you can change the color of its characters with the following piece of code:
    Code:
    /* =================================================================
       Write a character in the RTF control
       ================================================================= */
    int RTFEDIT_putc(char c, COLORREF col, int h, int x)
    {
       CHARFORMAT cf; char buf[2]; // HDC hdc;
    
       Edit_Enable(hRTF, FALSE);
    
       ZeroMemory(&cf, sizeof(CHARFORMAT));
       cf.cbSize = sizeof(CHARFORMAT);
       cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_ITALIC |
                              CFM_SIZE | CFM_UNDERLINE;
       cf.dwEffects = 0;
       if (h > 0)
          cf.yHeight = h;
       else
          cf.yHeight = 160;
       cf.yOffset = 0;
       if (col > 0)
          cf.crTextColor = col;
       else
          cf.crTextColor = RGB(0,0,0);
    
       Edit_SetSel(hRTF, x, x+1);
       buf[0] = c; buf[1] = '\0';
       SendMessage(hRTF, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
       Edit_ReplaceSel(hRTF, buf);
       Edit_SetSel(hRTF, 0, 0);
    
       Edit_Enable(hRTF, TRUE);
    
       return TRUE;
    }
    Quote:
    this method would slow down
    Not much. Try.
    Quote:
    So I asked about it and someone told me I need to parse it
    Parsing has nothing to do with speed. It means "finding something". It's already the method you were using in VB.
    Reply With Quote
      #3    
    Old February 16th, 2010, 04:31 PM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Re: Rich Text box Questions

    Quote:
    Originally Posted by olivthill2 View Post
    In C, once you find a word, you can change the color of its characters with the following piece of code:
    Code:
    /* =================================================================
       Write a character in the RTF control
       ================================================================= */
    int RTFEDIT_putc(char c, COLORREF col, int h, int x)
    {
       CHARFORMAT cf; char buf[2]; // HDC hdc;
    
       Edit_Enable(hRTF, FALSE);
    
       ZeroMemory(&cf, sizeof(CHARFORMAT));
       cf.cbSize = sizeof(CHARFORMAT);
       cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_ITALIC |
                              CFM_SIZE | CFM_UNDERLINE;
       cf.dwEffects = 0;
       if (h > 0)
          cf.yHeight = h;
       else
          cf.yHeight = 160;
       cf.yOffset = 0;
       if (col > 0)
          cf.crTextColor = col;
       else
          cf.crTextColor = RGB(0,0,0);
    
       Edit_SetSel(hRTF, x, x+1);
       buf[0] = c; buf[1] = '\0';
       SendMessage(hRTF, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
       Edit_ReplaceSel(hRTF, buf);
       Edit_SetSel(hRTF, 0, 0);
    
       Edit_Enable(hRTF, TRUE);
    
       return TRUE;
    }
    Not much. Try.
    Parsing has nothing to do with speed. It means "finding something". It's already the method you were using in VB.
    Cool thanks for the code. If you don't mind though could you explain how it works please?

    Also how can I make the text go beyond the right side of the rtf textbox?

    Like I said once it gets there it goes to a new line.

    Thanks again
    Reply With Quote
      #4    
    Old February 16th, 2010, 07:54 PM
    VladimirF VladimirF is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 2000
    Location: Del Mar, California, USA
    Posts: 4,841
    VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)VladimirF has a brilliant future (2000+)
    Re: Rich Text box Questions

    Quote:
    Originally Posted by Senith View Post
    Also how can I make the text go beyond the right side of the rtf textbox?

    Like I said once it gets there it goes to a new line.
    Try to set ES_AUTOHSCROLL style.
    __________________
    Vlad - MS MVP [2007 - 2010] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
    Reply With Quote
      #5    
    Old February 17th, 2010, 09:35 PM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Re: Rich Text box Questions

    Quote:
    Originally Posted by VladimirF View Post
    Try to set ES_AUTOHSCROLL style.


    Yes that worked!

    Thank you!
    Reply With Quote
      #6    
    Old February 22nd, 2010, 01:42 AM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Re: Rich Text box Questions

    ok so I took the code and added comments. Now the comments that I added, are the comments correct or wrong?

    Also if you see //??? bla bla bla then that means I think I know what it is but still don't know or I don't know

    Thanks

    Code:
    /* =================================================================
       Write a character in the RTF control
       ================================================================= */
    int RTFEDIT_putc(char c, COLORREF col, int h, int x)// Custom Function
    {
       CHARFORMAT cf; char buf[2]; // HDC hdc; // Declarations
    
       Edit_Enable(hRTF, FALSE);// Disables the RTF Edit box
    
       ZeroMemory(&cf, sizeof(CHARFORMAT)); //???
       cf.cbSize = sizeof(CHARFORMAT);      //???
       cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_ITALIC |CFM_SIZE | CFM_UNDERLINE;// What will be edited
       cf.dwEffects = 0;//??? Effects like tranparency
       if (h > 0)
          cf.yHeight = h; ;//??? Hight of the text
       else
          cf.yHeight = 160;//??? Hight of the text
       cf.yOffset = 0;//???
       if (col > 0)
          cf.crTextColor = col;// Sets the color from col
       else
          cf.crTextColor = RGB(0,0,0);// Sets the color Black
    
       Edit_SetSel(hRTF, x, x+1);// Sets the Selection
       buf[0] = c; buf[1] = '\0';//???
       SendMessage(hRTF, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);// Sends data to RTF Edit
       Edit_ReplaceSel(hRTF, buf);// Replaces the selected text
       Edit_SetSel(hRTF, 0, 0);// Sets the Selection to
    
       Edit_Enable(hRTF, TRUE); // Enable the RTF Edit Box
    
       return TRUE;// Returns to the main program
    }
    Reply With Quote
      #7    
    Old February 22nd, 2010, 04:12 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 457
    olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)
    Re: Rich Text box Questions

    Code:
    ZeroMemory(&cf, sizeof(CHARFORMAT)); //???
    cf.cbSize = sizeof(CHARFORMAT);      //???
    This is needed because you can have different structures having the same name, CHARFORMAT, depending on your version of Windows. This is the same for other standard structures, which may have more fields in some Windows' versions than in others. The classical example is OPENFILENAME which have a different number of fields, and therefore a different size in different versions of Windows. You should clear the memory because otherwise you should fill every field, and it is easier to zero the memory than to remember the name of every field, since they may not be the same from one version to the other, and you remember only important fields.
    Code:
    cf.dwEffects = 0;//??? Effects like tranparency
    See guide.
    Code:
       if (h > 0)
          cf.yHeight = h; ;//??? Hight of the text
       else
          cf.yHeight = 160;//??? Hight of the text
       cf.yOffset = 0;//???
    Yes, it is the height of a character and it is on the standard line, not above or below. See guide.
    Code:
    buf[0] = c; buf[1] = '\0';//???
    Edit_ReplaceSel(hRTF, buf);// Replaces the selected text
    Edit_ReplaceSel() needs a null terminated string. I work with only one character, c, at a time, but you can work with a word or a sentence.
    Your other comments are correct.
    Reply With Quote
      #8    
    Old February 22nd, 2010, 09:39 AM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Re: Rich Text box Questions

    cool thanks for pointing that out

    Also I came across an error in Dev C++

    its saying that CHARFORMAT does not name a type.

    So I think its saying that it doesn't exists like if I use handle instead of HWND.

    Do I need to include a library?
    Reply With Quote
      #9    
    Old February 22nd, 2010, 10:20 AM
    hoxsiew hoxsiew is offline
    Elite Member
     
    Join Date: Feb 2005
    Posts: 2,112
    hoxsiew is a glorious beacon of light (400+)hoxsiew is a glorious beacon of light (400+)hoxsiew is a glorious beacon of light (400+)hoxsiew is a glorious beacon of light (400+)hoxsiew is a glorious beacon of light (400+)hoxsiew is a glorious beacon of light (400+)
    Re: Rich Text box Questions

    I don't know about DevC++, but CHARFORMAT should be a macro that evaluates to CHARFORMATA or CHARFORMATW depending on the build. It should be defined in richedit.h:

    Code:
    #if (_RICHEDIT_VER >= 0x0200)
    #ifdef UNICODE
    #define CHARFORMAT CHARFORMATW
    #else
    #define CHARFORMAT CHARFORMATA
    #endif // UNICODE 
    #else
    #define CHARFORMAT CHARFORMATA
    #endif // _RICHEDIT_VER >= 0x0200
    Reply With Quote
      #10    
    Old February 22nd, 2010, 10:44 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 457
    olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)olivthill2  is a jewel in the rough (300+)
    Re: Rich Text box Questions

    I use dev_cpp.
    At the begining of my file, I have:
    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <richedit.h>
    #include <commdlg.h>
    I don't link with any particular libraries.
    I have
    Code:
        if (!hRTFLib)
          hRTFLib = LoadLibrary("RICHED32.DLL");
        if (!hRTFLib) {
            char szErr[255];
            wsprintf(szErr, "LoadLibrary RICHED32.DLL failed. Error code %ld", GetLastError());
            MessageBox(hWndParent, szErr, "Error", MB_OK );
            hRTFLib = NULL;
            return FALSE;
        }
        hRTF = CreateWindow("RICHEDIT",          // Class Name of RTF Edit
                            "",                  // Text of edit control
                            WS_CHILD   | WS_VISIBLE | ES_MULTILINE   |
                            WS_VSCROLL | WS_BORDER  |
                            ES_AUTOVSCROLL | ES_NOHIDESEL, // Window style
                            0, 0,                // Initially create 0 size,
                            0, 0,                // Main Wnd's WM_SIZE handler will resize
                            hWndParent,          // Use main parent
                            (HMENU)0,            // ID of zero (we don't care)
                            (HINSTANCE) GetWindowLong(hWndParent, GWL_HINSTANCE), // This app instance owns this window
                            NULL                 // Don't need data in WM_CREATE
        );
        SetFocus(hRTF);
    ...
    Reply With Quote
      #11    
    Old February 22nd, 2010, 11:17 AM
    Senith Senith is offline
    Member
     
    Join Date: Jan 2010
    Posts: 63
    Senith is an unknown quantity at this point (<10)
    Re: Rich Text box Questions

    Quote:
    Originally Posted by olivthill2 View Post
    I use dev_cpp.
    At the begining of my file, I have:
    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <richedit.h>
    #include <commdlg.h>
    I don't link with any particular libraries.
    I have
    Code:
        if (!hRTFLib)
          hRTFLib = LoadLibrary("RICHED32.DLL");
        if (!hRTFLib) {
            char szErr[255];
            wsprintf(szErr, "LoadLibrary RICHED32.DLL failed. Error code %ld", GetLastError());
            MessageBox(hWndParent, szErr, "Error", MB_OK );
            hRTFLib = NULL;
            return FALSE;
        }
        hRTF = CreateWindow("RICHEDIT",          // Class Name of RTF Edit
                            "",                  // Text of edit control
                            WS_CHILD   | WS_VISIBLE | ES_MULTILINE   |
                            WS_VSCROLL | WS_BORDER  |
                            ES_AUTOVSCROLL | ES_NOHIDESEL, // Window style
                            0, 0,                // Initially create 0 size,
                            0, 0,                // Main Wnd's WM_SIZE handler will resize
                            hWndParent,          // Use main parent
                            (HMENU)0,            // ID of zero (we don't care)
                            (HINSTANCE) GetWindowLong(hWndParent, GWL_HINSTANCE), // This app instance owns this window
                            NULL                 // Don't need data in WM_CREATE
        );
        SetFocus(hRTF);
    ...

    Yep I needed richedit.h Thanks for posting your code and helping me out with this.
    Appreciate it !


    Quote:
    Originally Posted by hoxsiew View Post
    I don't know about DevC++, but CHARFORMAT should be a macro that evaluates to CHARFORMATA or CHARFORMATW depending on the build. It should be defined in richedit.h:

    Code:
    #if (_RICHEDIT_VER >= 0x0200)
    #ifdef UNICODE
    #define CHARFORMAT CHARFORMATW
    #else
    #define CHARFORMAT CHARFORMATA
    #endif // UNICODE 
    #else
    #define CHARFORMAT CHARFORMATA
    #endif // _RICHEDIT_VER >= 0x0200

    Yea, I needed richedit.h. Thanks for letting me know!


    ^_^
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:05 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.