Step 6: Writing to the Surface

The first half of the WM_TIMER message in DDEX1 is devoted to writing to the back buffer, as shown in the following example:

case WM_TIMER: 
    // Flip surfaces. 
    if(bActive) 
    { 
        if (lpDDSBack->GetDC(&hdc) == DD_OK) 
        { 
            SetBkColor(hdc, RGB(0, 0, 255)); 
            SetTextColor(hdc, RGB(255, 255, 0)); 
            if(phase) 
            { 
                TextOut(hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg)); 
                phase = 0; 
            } 
            else 
            { 
                TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg)); 
                phase = 1; 
            } 
            lpDDSBack->ReleaseDC(hdc); 
        } 
 

The line of code that calls the IDirectDrawSurface3::GetDC method locks the back buffer in preparation for writing. The SetBkColor and SetTextColor functions set the colors of the background and text.

Next, the phase variable determines whether the primary buffer message or the back buffer message should be written. If phase equals 1, the primary surface message is written, and phase is set to 0. If phase equals 0, the back buffer message is written, and phase is set to 1. Note, however, that in both cases the messages are written to the back buffer.

After the message is written to the back buffer, the back buffer is unlocked by using the IDirectDrawSurface3::ReleaseDC method.