dynamic edit control

This is a discussion on dynamic edit control within the Windows Programming forums, part of the Platform Specific Boards category; I have an edit control and i estimate it does a few thosand strcat and copies and is about 200k ...

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    dynamic edit control

    I have an edit control and i estimate it does a few thosand strcat and copies and is about 200k long, is there a way i can dynamicaly fill it, say 20 lines either side of where the scroll bar points to, does anyone know how i might get this and fill only a certain point of my edit?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    Displaying them is fine, but it bogs down the system ALOT if i fill my whole edit control.

    What i want is to only fill 20 lines, in the middle of the edit. (You can't see the text offscreen so why i should i process it )

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Well, at the risk of appearing to repeat myself I honestly believe that the answer to your question as written is given in append/insert text in an edit control.

    As I understand it, you are updating the whole contents of the edit control every time you make a small change. The above link contains discussion and code (compliments of anonytmouse) that enables you to insert text anywhere within the body of the edit control's contents without having to update the entire contents.

    If that's not what you're after, could you please explain in a little more detail what your needs are?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    I'm not changing it, i'm filling a buffer with 200k of data when i call CreateWindow("EDIT".buffer....);
    Which takes about 30 seconds, so what i'd like to do, is fill 20 lines like this
    ---------
    |meow | |
    |meow | |
    |meow |#|
    |meow | |
    ---------
    (the # is the scroll bar)

    I want that in a way that anything before the first meow, and past the last, isn't even put into the edit box.
    Otherwise it gets filled with 64000 lines. (taking 30 seconds, I'm gunna ramble on til i think i've explained it well enough)
    ie, would i ned to get the position of the scroll bar, pad with say 10000 \r\ns, then put my meows, then pad with another few thousand \r\ns.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    An edit control dosen't use a lot of strcats it uses a linked list. So modifying some line wont modify all lines following it. If you place a new line charactor in the middle of a line then a new line will be inserted afterwards containing the end of that line.
    What is buffer? something like this "line1\nline2\n..." If thats what it is then you could move a pointer thru the buffer counting lines untill you reach the line you want and use that as the start address.
    Also you don't have to set the entire text all at once when the edit control is created. You could create it with no text in it then use multiple EM_SETSEL/EM_REPLACESEL calls to add lines that way the user will at least be able to scroll what text has allready been loaded an not have a 30 second delay before he sees any text.
    Last edited by Quantum1024; 03-27-2005 at 12:50 PM.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Perhaps this article on memory mapped files will be of some interest to you.

    Here's a description of an implementation.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    *mine* uses strcats, thats the problem :P
    The buffer is generated via them.
    I might as well post the code
    Code:
    for(int base=0;base<=0xCFF0;base+=0x10){
        sprintf(temp,"%04X",base);
        strcat(memory,temp);
        strcat(memory,": ");
       for(int a=0;a<=0x0E;a+=2){
        sprintf(temp,"%02X",(unsigned char)ram[base+a]);
        strcat(memory,temp);
        sprintf(temp,"%02X",(unsigned char)ram[base+a+1]);
        strcat(memory,temp);
        strcat(memory," ");
       }
       strcat(memory,"\r\n");
       }
       memedit = CreateWindow("EDIT",memory,WS_VSCROLL | ES_MULTILINE | ES_READONLY | WS_VISIBLE | WS_CHILD | WS_TILED,0,0,512,512,hwnd,NULL,GetModuleHandle(NULL),NULL);
    It formats a char-array into an EDIT, the char array being 64k long (which is why it takes so long)
    Last edited by krappykoder; 03-27-2005 at 12:55 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    After creating the edit control, consider adding the data line by line in a seperate thread, something like this:
    Code:
    for (int Line=0;Line<MAX_LINES;Line++)
    {
       //make Line
       sprintf(LineData...
       SendMessage(memedit, EM_SETSEL, -1, -1);
       SendMessage(memedit, EM_REPLACESEL, FALSE, LineData);
    }

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    strcat is a very expensive function. It must loop through every character in the current string to find the end before adding the new content. Similar to my advice against using strlen in a loop, you should avoid using strcat in a loop. Let's do some rough calculations based on your code:
    Code:
    Outer Loop:
    (0xCFF0 / 0x10) * 3      = 9,981  strcat calls   | 26,616  characters outputted
    
    Inner Loop:
    (0xCFF0 / 0x10) * 8 * 3  = 79,848 strcat calls   | 239,544 characters outputted
    
    Total strcat calls: 89,829
    Total Characters:   266,170
    From this we can get an idea of the total number of iterations performed in the strcat calls. 89,829 strcat calls iterating over an average of 133,085 (half of 266,170) characters each time:
    Code:
    89,829 * 133,085 = 11,954,892,465
    That's over eleven billion wasted iterations! You can see why it takes a long time! Now, if we can remove those strcat calls, we will speed up the code by several orders of magnitude. We can do this by keeping our current position in a variable.

    We can use a cur_pos pointer:
    Code:
    char* cur_pos = memory;
    
    for (int base = 0; base <= 0xCFF0; base += 0x10)
    {
        sprintf(cur_pos, "%04X", base);
        cur_pos += 4;
    
        strcpy(cur_pos, ": ");
        cur_pos += 2;
    
        for (int a = 0; a <= 0x0E; a += 2)
        {
            sprintf(cur_pos,"%02X%02X", (unsigned char) ram[base + a], 
                                         (unsigned char) ram[base + a + 1]);
            cur_pos += 4;
    
            strcpy(cur_pos, " ");
            cur_pos += 1;
        }
    
        strcpy(cur_pos, "\r\n");
        cur_pos += 2;
    }
    Or, alternatively, we can use cur_pos as an array index:
    Code:
    size_t cur_pos = 0;
    
    for (int base = 0; base <= 0xCFF0; base += 0x10)
    {
        sprintf(&memory[cur_pos], "%04X", base);
        cur_pos += 4;
    
        strcpy(&memory[cur_pos], ": ");
        cur_pos += 2;
    
        for (int a = 0; a <= 0x0E; a += 2)
        {
            sprintf(&memory[cur_pos], "%02X%02X", (unsigned char) ram[base + a],
                                                  (unsigned char) ram[base + a + 1]);
            cur_pos += 4;
    
            strcpy(&memory[cur_pos], " ");
            cur_pos += 1;
        }
    
        strcpy(&memory[cur_pos], "\r\n");
        cur_pos += 2;
    }
    Last edited by anonytmouse; 03-28-2005 at 02:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I send a text string to an edit control
    By marc74 in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2005, 10:14 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 10:52 PM
  3. Restricting input to an edit control
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2003, 02:10 AM
  4. endless edit control
    By ZerOrDie in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2003, 02:51 AM
  5. Keeping focus on an edit control ...
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 02-19-2002, 02:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21