DrawText and different backgounr colors

This is a discussion on DrawText and different backgounr colors within the Windows Programming forums, part of the Platform Specific Boards category; Hi I' changing the main windows backgoundcolor to grea with this class definition: Code: wndclass.style = CS_HREDRAW |CS_VREDRAW; wndclass.lpfnWndProc = ...

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    DrawText and different backgounr colors

    Hi

    I' changing the main windows backgoundcolor to grea with this class definition:

    Code:
    wndclass.style                  = CS_HREDRAW |CS_VREDRAW;
    wndclass.lpfnWndProc      = WndProc;
    wndclass.cbClsExtra         = 0;
    wndclass.cbWndExtra       = 0;
    wndclass.hInstance	         = hInstance;
    wndclass.hIcon                 = LoadIcon(hInstance, szAppName);
    wndclass.hCursor             = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground  = (HBRUSH)(COLOR_BTNFACE+1);
    wndclass.lpszMenuName  = szAppName;
    wndclass.lpszClassName  = szAppName;
    But when i use DrawText to write something into the window, the backgound of the characters is still white. changing the character backgound to transparent doesn't help,because while scrolling up they overleap somehow. how can i solve that (i would like that it still works if i change the main windows background to red, i.e.

    thanks

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Look up SetTextColor() and SetBkColor().
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Or GetDC() the client area and fill this with the colour you want before drawing to it (to clear the previous text as the DC scrolls)

    Code:
    //I like to define the colours
    #define   WHITE       RGB(255,255,255)
    
    //get the DC
    hdc=GetDC(NULL);
    //Create a brush
    hBrush=GetStockObject(WHITE_BRUSH);
    //or
    hBrush=CreateSolidBrush(WHITE);
    //Get the area 
    GetClient Rect(hWnd,&ClientRect);
    //Fill the area
    FillRect(hdc ,&ClientRect, hBrush);
    //Delete the brush 
    DeleteObject(hBrush);
    "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

  4. #4
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    thank you two. another question, that's slightly connected to this one, i think.

    reading petzolds book "Windows Programming 5th Edition", i saw he's always using (this far) CS_VREDRAW | CS_HREDRAW in the window class settings for the main window. reading in the documentation i figured out, that this simply send a wm_paint message for the whole window each time the window is resized or moved. with all of my programms i get this flickering effect. so i just decided to set this in the wndclass struct to 0. No flickering effect anymore. the windwo behalfes normal, because with every message wm_size i calculate which areas have to be repainted and send InvalidateRect, for that specific area.
    my question now: is that dangerous or even incorrect? i don't think so, becaus if using V_REDRAW | HREDRAW you don't even need to react in that way in wm_size, because the whole window is completly redrawn. so is that just really usable for application that need to redraw the whole window wenn a resize is done? (e.g. when centering text). isn't it better to set that param to 0 and care myself for the areas that have to be updatet (which is usaly just a little square with status messages, the rest of the window is an edit window and some buttons.)

    thanks

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Try setting the Erase background flag in InvalidateRect() to FALSE

    Returning FALSE to WM_ERASEBKGND msg's

    Using GetUpdateRect() in your WM_PAINT handler (before calling BeginPaint() )

    Code:
    //get invalidated rect to use in the drawing
    GetUpdateRect(hWnd, &theRect, 0);
    if (IsRectEmpty(&theRect))
       GetClientRect(hWnd,&theRect);
    //begin paint will validate the drawing area so call beforehand
    "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

Popular pages Recent additions subscribe to a feed

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