Click here to Skip to main content
15,891,513 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Interesting results with consts and pointers... Pin
Dan5-May-08 21:31
Dan5-May-08 21:31 
AnswerRe: Interesting results with consts and pointers... Pin
CPallini5-May-08 21:50
mveCPallini5-May-08 21:50 
AnswerRe: Interesting results with consts and pointers... Pin
markkuk6-May-08 0:22
markkuk6-May-08 0:22 
AnswerRe: Interesting results with consts and pointers... Pin
Mark Salsbery6-May-08 6:05
Mark Salsbery6-May-08 6:05 
QuestionSQL statement error: update record, why? Pin
includeh105-May-08 15:22
includeh105-May-08 15:22 
AnswerRe: SQL statement error: update record, why? Pin
Rajesh R Subramanian5-May-08 19:37
professionalRajesh R Subramanian5-May-08 19:37 
AnswerRe: SQL statement error: update record, why? Pin
toxcct5-May-08 21:51
toxcct5-May-08 21:51 
Questioncustom CSatusBar and double-buffering Pin
blamond5-May-08 15:02
blamond5-May-08 15:02 
Hi,

Wondering if anyone has any good ideas here...

I have written a custom CStatusBar control which basically updates
some panes in color as the mouse cursor is moved around. The panes
were flickering due to the amount of redrawing required so I added
some code that uses double-buffering to draw the panes off-screen
first, then blits to screen. I used a well-known piece of code from
Keith Rule on CodeProject in the CMemDC class for this
(http://www.codeproject.com/KB/GDI/flickerfree.aspx?msg=2531592#xx2531592xx).
Here's the code for that:

***********************************************************************************
<br />
class CMemDC : public CDC {<br />
private:<br />
        CBitmap         m_bitmap;               // Offscreen bitmap<br />
        CBitmap*        m_oldBitmap;    // bitmap originally found in CMemDC<br />
        CDC*            m_pDC;                  // Saves CDC passed in constructor<br />
        CRect           m_rect;                 // Rectangle of drawing area.<br />
        BOOL            m_bMemDC;               // TRUE if CDC really is a Memory DC.<br />
public:<br />
<br />
        CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()<br />
        {<br />
                ASSERT(pDC != NULL);<br />
<br />
                // Some initialization<br />
                m_pDC = pDC;<br />
                m_oldBitmap = NULL;<br />
                m_bMemDC = !pDC->IsPrinting();<br />
<br />
                // Get the rectangle to draw<br />
                if (pRect == NULL) {<br />
                        pDC->GetClipBox(&m_rect);<br />
                } else {<br />
                        m_rect = *pRect;<br />
                }<br />
<br />
                if (m_bMemDC) {<br />
                        // Create a Memory DC<br />
                        CreateCompatibleDC(pDC);<br />
                        pDC->LPtoDP(&m_rect);<br />
<br />
                        m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(),<br />
m_rect.Height());<br />
                        m_oldBitmap = SelectObject(&m_bitmap);<br />
<br />
                        SetMapMode(pDC->GetMapMode());<br />
<br />
                        SetWindowExt(pDC->GetWindowExt());<br />
                        SetViewportExt(pDC->GetViewportExt());<br />
<br />
                        pDC->DPtoLP(&m_rect);<br />
                        SetWindowOrg(m_rect.left, m_rect.top);<br />
                } else {<br />
                        // Make a copy of the relevent parts of the current DC for printing<br />
                        m_bPrinting = pDC->m_bPrinting;<br />
                        m_hDC       = pDC->m_hDC;<br />
                        m_hAttribDC = pDC->m_hAttribDC;<br />
                }<br />
<br />
                // Fill background<br />
                FillSolidRect(m_rect, pDC->GetBkColor());<br />
        }<br />
<br />
        ~CMemDC()<br />
        {<br />
                if (m_bMemDC) {<br />
                        // Copy the offscreen bitmap onto the screen.<br />
                        m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(),<br />
m_rect.Height(),<br />
                                this, m_rect.left, m_rect.top, SRCCOPY);<br />
<br />
                        //Swap back the original bitmap.<br />
                        SelectObject(m_oldBitmap);<br />
                } else {<br />
                        // All we need to do is replace the DC with an illegal value,<br />
                        // this keeps us from accidently deleting the handles associated<br />
with<br />
                        // the CDC that was passed to the constructor.<br />
                        m_hDC = m_hAttribDC = NULL;<br />
                }<br />
        }<br />
<br />
        // Allow usage as a pointer<br />
        CMemDC* operator->()<br />
        {<br />
                return this;<br />
        }<br />
<br />
        // Allow usage as a pointer<br />
        operator CMemDC*()<br />
        {<br />
                return this;<br />
        }<br />
};<br />

***********************************************************************************

Applying this code requires overriding the OnPaint & OnEraseBkgnd
methods in the custom CStatusBar as follows:

<br />
void MyStatusBar::OnPaint()<br />
{<br />
        CPaintDC dc(this); // device context for painting<br />
        // TODO: Add your message handler code here<br />
        // Do not call CStatusBar::OnPaint() for painting messages<br />
        CRect rect;<br />
        GetClientRect(&rect);<br />
        CMemDC memDC(&dc, &rect);<br />
<br />
        DefWindowProc(WM_PAINT, (WPARAM)memDC->m_hDC, (LPARAM)0);<br />
<br />
}<br />
<br />
BOOL MyStatusBar::OnEraseBkgnd(CDC* pDC)<br />
{<br />
        // TODO: Add your message handler code here and/or call default<br />
        //return CStatusBar::OnEraseBkgnd(pDC);<br />
        return TRUE;<br />
<br />
}<br />
<br />


This reduces the flickering, but now the pane borders don't get drawn
at all - they just appear as blank white areas. I have tried
overriding the OnNcPaint function to avoid the call to
CControlBar::EraseNonClient(). No effect. I have also tried calling
DrawBorders explicitly with likewise no effect.

Does anyone have any idea what I'm missing here?

Any suggestions would be much appreciated.

Best,
Bruce
AnswerRe: custom CSatusBar and double-buffering Pin
Rajkumar R5-May-08 21:51
Rajkumar R5-May-08 21:51 
GeneralRe: custom CSatusBar and double-buffering Pin
blamond6-May-08 8:46
blamond6-May-08 8:46 
QuestionHow to implement font size change feature of Office 2007? Pin
Rong Yao5-May-08 11:36
Rong Yao5-May-08 11:36 
AnswerRe: How to implement font size change feature of Office 2007? Pin
thammadi6-May-08 1:02
thammadi6-May-08 1:02 
QuestionDetect Print Preview without a DC [modified] Pin
bob169725-May-08 10:00
bob169725-May-08 10:00 
AnswerRe: Detect Print Preview without a DC [modified] Pin
Ozer Karaagac5-May-08 10:48
professionalOzer Karaagac5-May-08 10:48 
GeneralRe: Detect Print Preview without a DC Pin
bob169725-May-08 10:59
bob169725-May-08 10:59 
GeneralRe: Detect Print Preview without a DC Pin
Ozer Karaagac5-May-08 11:12
professionalOzer Karaagac5-May-08 11:12 
GeneralRe: Detect Print Preview without a DC Pin
bob169725-May-08 11:26
bob169725-May-08 11:26 
Questionwebbrowser ExecWB printing crashes in release Pin
AmVal5-May-08 9:40
AmVal5-May-08 9:40 
AnswerRe: webbrowser ExecWB printing crashes in release Pin
shahshi7-Feb-10 23:09
shahshi7-Feb-10 23:09 
QuestionFormatting a partition Pin
RomTibi5-May-08 8:54
RomTibi5-May-08 8:54 
AnswerRe: Formatting a partition Pin
Joan M5-May-08 9:15
professionalJoan M5-May-08 9:15 
GeneralRe: Formatting a partition Pin
RomTibi6-May-08 7:04
RomTibi6-May-08 7:04 
AnswerRe: Formatting a partition Pin
Mukesh Kumar5-May-08 18:34
Mukesh Kumar5-May-08 18:34 
GeneralRe: Formatting a partition Pin
RomTibi6-May-08 7:05
RomTibi6-May-08 7:05 
AnswerRe: Formatting a partition PinPopular
Hamid_RT5-May-08 18:37
Hamid_RT5-May-08 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.