Updating

This is a discussion on Updating within the Windows Programming forums, part of the Platform Specific Boards category; I'm using drawtext to display strings of text, when ever you load a string of text thats shorter that the ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Updating

    I'm using drawtext to display strings of text, when ever you load a string of text thats shorter that the pevious string it partially overwrites it but still part of it is left, how would i have it refresh or clear the screen so that wouldn't show up when the new string is loaded, each string is loaded on the press of a menu option.

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    RECT rect;
    ...
    GetWindowRect(hwnd, &rect);
    InvalidateRect(hwnd, &rect, true);

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    I must be putting that in the wrong spot because it dectroys my rect as soon as the writing comes up here the hode that i'm trying to get it to work for if thats any help.

    switch(msg)
    {
    case WM_CREATE:
    {
    return(0);
    }
    case WM_COMMAND:
    {
    switch(wparam)
    {
    case ID_FILE_EXIT:
    {
    PostMessage(hwnd,WM_DESTROY,0,0);
    } break;
    case ID_NAVIGATION:
    {
    hdc = GetDC(hwnd);
    SetTextColor(hdc,RGB(TCR,TCG,TCB));
    SetBkColor(hdc,RGB(0,0,0));
    RECT myRect = {1, 1, 635, 470};
    DrawText(hdc,"Text 1,strlen("Test 1"),&myRect,DT_WORDBREAK);
    } break;
    case ID_NAVIGATION_2:
    {
    hdc = GetDC(hwnd);
    SetTextColor(hdc,RGB(TCR,TCG,TCB));
    SetBkColor(hdc,RGB(0,0,0));
    DrawText(hdc,"Text Number 2",strlen("Text Number 2"),&myRect,DT_WORDBREAK);
    } break;

  4. #4
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    You never release the DC!
    Please excuse my poor english...

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    A slight change to make it work just fine with -KEN-'s great suggestion ( and larry is true too, but following this method you don't have to worry about that); i've only altered/added relevant parts:

    Code:
    char cText01[]="Text 1";
    char cText02[]="Text Number 2";
    char cTheText[20];
    RECT myRect = {1, 1, 635, 470}; 
    .
    .
    case ID_NAVIGATION: 
        strcpy(cTheText,cText01);
        InvalidateRect(hwnd,&myRect,1);
        break;
    case ID_NAVIGATION_2: 
        strcpy(cTheText,cText02);
        InvalidateRect(hwnd,&myRect,1);
        break;
    .
    .
    case WM_PAINT:
        {
        PAINTSTRUCT ps;
        BeginPaint(hwnd,&ps);
            SetTextColor(ps.hdc,RGB(TCR,TCG,TCB)); 
            SetBkColor(ps.hdc,RGB(0,0,0));
            DrawText(ps.hdc,cTheText,strlen(cTheText),&myRect,DT_WORDBREAK);  
        EndPaint(hwnd,&ps);
        }
    If you would prefer to get your own dc then perhaps:
    Code:
    void MyDrawTextFunction(char *cTxt)
    {
    hdc = GetDC(hwnd); 
    SetTextColor(hdc,RGB(TCR,TCG,TCB)); 
    SetBkColor(hdc,RGB(0,0,0)); 
    RECT myRect = {1, 1, 635, 470}; 
    DrawText(hdccTxt,strlen(ctxt),&myRect,DT_WORDBREAK); 
    ReleaseDC(hwnd,hdc); //as larry pointed out
    }
    .
    .
    case ID_NAVIGATION: 
      MyDrawTextFunction("Text 1");
      break; 
    case ID_NAVIGATION_2:
      MyDrawTextFunction("Text Number 2");
      break;
    Although the principle of having a fn do this for you is sound enough I suspect that the wnd may get scrubbed anyway when ever a WM_PAINT occurs; using ValidateRect(hwnd,&myRect); might help prevent this.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    The refresh works now but when i first load the programm i'm getting wierd random text sometimes is just aasgsdg or some of the special characters. any of you know why its doing this?
    Last edited by ColdFire; 02-17-2002 at 10:14 AM.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    The way I do it is to repaint the area with FillRect(), FrameRect() or Rectangle() BEFORE rewritting the text.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    315
    InvalidateRect(hwnd, &rect, true);

    does anyone think InvalidateRect is a stupid name for this ?

    why didnt they call it UpdateRect , because when you say something is invalid
    it means its wrong?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Part of the forum rules is to "not wake old threads". A thread that hasn't had a comment for 6 years is definitely OLD by _ALL_ possible measures in this forum.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by Anddos View Post
    does anyone think InvalidateRect is a stupid name for this ?

    why didnt they call it UpdateRect , because when you say something is invalid
    it means its wrong?
    No. Why?
    Because it invalidates the region (it tells Windows it should be repainted, not updated). It does not update it.
    That's done later by Windows.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by Anddos View Post
    InvalidateRect(hwnd, &rect, true);

    does anyone think InvalidateRect is a stupid name for this ?

    why didnt they call it UpdateRect , because when you say something is invalid
    it means its wrong?
    Exactly! Thats what the meaning is. Wrong. They mean that the rect is "wrong" "bad" "crap" now!
    So please change it!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    That's not what the meaning is.
    InvalidateRect does not update the specified region.
    It simple tells Windows that the specified region should be repainted (whenever Windows decides to do that).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by Elysia View Post
    That's not what the meaning is.
    InvalidateRect does not update the specified region.
    It simple tells Windows that the specified region should be repainted (whenever Windows decides to do that).
    Although it hardly matters, whatever they name it. I should not argue about Microsoft not being called Nanosoft either.

    But as you said, the specified region is marked for update, because it is invalid. Hence the name InvalidateRect.

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    315
    alright cheers guys

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    If you're drawing text to a white background, t'is much easier to use a simple static along with SS_NOPREFIX STYLE. All of that repainting crap is done for you -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constant updating
    By Wick in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2003, 09:00 PM
  2. updating database
    By datainjector in forum C# Programming
    Replies: 2
    Last Post: 07-11-2003, 01:01 AM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  4. continuously updating timer on console display
    By revelation437 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2003, 11:28 AM
  5. Updating a file
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-29-2002, 03:03 AM

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