BitBlt on Win98 / WinNT
-
Hi there, can someone give me an idea as to why this code: /*****************************************************/ /* draw some text */ /*****************************************************/ CRect rect; CFont TextFont, *oldFont; CWnd *wnd = GetDlgItem(IDC_PLACEHOLDER); // find where to put the text (a static control) ASSERT(wnd != NULL); ASSERT(IsWindow(wnd->m_hWnd) != FALSE); wnd->GetWindowRect(&rect); ScreenToClient(&rect); CPaintDC dc(this) ; // device context for painting if (m_dc.GetSafeHdc() == NULL) { m_dc.CreateCompatibleDC(&dc) ; m_bitmapTitle.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()) ; m_pbitmapOldTitle = m_dc.SelectObject(&m_bitmapTitle) ; } TextFont.CreateFont (40, 0, 0, 0, 300, // Create the font TRUE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH|FF_SWISS, "Arial") ; oldFont = m_dc.SelectObject(&TextFont); // select the font m_dc.SetTextColor (RGB(128, 0, 0)) ; // set a bunch of attributes of the dc m_dc.SetBkMode(OPAQUE); m_dc.SetBkColor(::GetSysColor(COLOR_3DFACE)); m_dc.SetTextAlign (TA_LEFT); CString Title = "Test "; m_dc.TextOut(0, 0, Title); m_dc.SelectObject(oldFont) ; CDC memDC ; CBitmap memBitmap ; CBitmap* oldBitmap ; // bitmap originally found in CMemDC // create a memory dc memDC.CreateCompatibleDC(&m_dc) ; memBitmap.CreateCompatibleBitmap(&m_dc, rect.Width(), rect.Height()) ; oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ; if (memDC.GetSafeHdc() != NULL) { // draw the text on the memory dc memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dc, 0, 0, SRCPAINT) ; // bitblt the result to the display DC dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY) ; } memDC.SelectObject(oldBitmap) ; /*****************************************************/ /* done... */ /*****************************************************/ produces perfectly fine output on Win NT but garbage on win 95/98/Me?? I'm stuck!! :(( to understand what I mean about garbage, please see Win NT and Win98 Thanks in advance for any help! Liam O'Hagan Thanks :-D Senior Test Engineer GLI Australia www.gli.com.au
-
Hi there, can someone give me an idea as to why this code: /*****************************************************/ /* draw some text */ /*****************************************************/ CRect rect; CFont TextFont, *oldFont; CWnd *wnd = GetDlgItem(IDC_PLACEHOLDER); // find where to put the text (a static control) ASSERT(wnd != NULL); ASSERT(IsWindow(wnd->m_hWnd) != FALSE); wnd->GetWindowRect(&rect); ScreenToClient(&rect); CPaintDC dc(this) ; // device context for painting if (m_dc.GetSafeHdc() == NULL) { m_dc.CreateCompatibleDC(&dc) ; m_bitmapTitle.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()) ; m_pbitmapOldTitle = m_dc.SelectObject(&m_bitmapTitle) ; } TextFont.CreateFont (40, 0, 0, 0, 300, // Create the font TRUE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH|FF_SWISS, "Arial") ; oldFont = m_dc.SelectObject(&TextFont); // select the font m_dc.SetTextColor (RGB(128, 0, 0)) ; // set a bunch of attributes of the dc m_dc.SetBkMode(OPAQUE); m_dc.SetBkColor(::GetSysColor(COLOR_3DFACE)); m_dc.SetTextAlign (TA_LEFT); CString Title = "Test "; m_dc.TextOut(0, 0, Title); m_dc.SelectObject(oldFont) ; CDC memDC ; CBitmap memBitmap ; CBitmap* oldBitmap ; // bitmap originally found in CMemDC // create a memory dc memDC.CreateCompatibleDC(&m_dc) ; memBitmap.CreateCompatibleBitmap(&m_dc, rect.Width(), rect.Height()) ; oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ; if (memDC.GetSafeHdc() != NULL) { // draw the text on the memory dc memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dc, 0, 0, SRCPAINT) ; // bitblt the result to the display DC dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY) ; } memDC.SelectObject(oldBitmap) ; /*****************************************************/ /* done... */ /*****************************************************/ produces perfectly fine output on Win NT but garbage on win 95/98/Me?? I'm stuck!! :(( to understand what I mean about garbage, please see Win NT and Win98 Thanks in advance for any help! Liam O'Hagan Thanks :-D Senior Test Engineer GLI Australia www.gli.com.au
Without a real context (not a DC context) it's pretty hard to tell. I can however tell you that you're in error to not check the return codes from each and every function above why I'd say you deserve the errors. After you've added those checks, perhaps you can see for yourself what went wrong? If not, that would be a good point to ask this question. P.S. If you're the "Senior Test Engineer" for that company, would you mind telling me what products you sell so I know what to stay clear of?
-
Hi there, can someone give me an idea as to why this code: /*****************************************************/ /* draw some text */ /*****************************************************/ CRect rect; CFont TextFont, *oldFont; CWnd *wnd = GetDlgItem(IDC_PLACEHOLDER); // find where to put the text (a static control) ASSERT(wnd != NULL); ASSERT(IsWindow(wnd->m_hWnd) != FALSE); wnd->GetWindowRect(&rect); ScreenToClient(&rect); CPaintDC dc(this) ; // device context for painting if (m_dc.GetSafeHdc() == NULL) { m_dc.CreateCompatibleDC(&dc) ; m_bitmapTitle.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()) ; m_pbitmapOldTitle = m_dc.SelectObject(&m_bitmapTitle) ; } TextFont.CreateFont (40, 0, 0, 0, 300, // Create the font TRUE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH|FF_SWISS, "Arial") ; oldFont = m_dc.SelectObject(&TextFont); // select the font m_dc.SetTextColor (RGB(128, 0, 0)) ; // set a bunch of attributes of the dc m_dc.SetBkMode(OPAQUE); m_dc.SetBkColor(::GetSysColor(COLOR_3DFACE)); m_dc.SetTextAlign (TA_LEFT); CString Title = "Test "; m_dc.TextOut(0, 0, Title); m_dc.SelectObject(oldFont) ; CDC memDC ; CBitmap memBitmap ; CBitmap* oldBitmap ; // bitmap originally found in CMemDC // create a memory dc memDC.CreateCompatibleDC(&m_dc) ; memBitmap.CreateCompatibleBitmap(&m_dc, rect.Width(), rect.Height()) ; oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ; if (memDC.GetSafeHdc() != NULL) { // draw the text on the memory dc memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dc, 0, 0, SRCPAINT) ; // bitblt the result to the display DC dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY) ; } memDC.SelectObject(oldBitmap) ; /*****************************************************/ /* done... */ /*****************************************************/ produces perfectly fine output on Win NT but garbage on win 95/98/Me?? I'm stuck!! :(( to understand what I mean about garbage, please see Win NT and Win98 Thanks in advance for any help! Liam O'Hagan Thanks :-D Senior Test Engineer GLI Australia www.gli.com.au
I've rewritten your function, I hope you find it a little neater :) /*****************************************************/ /* draw some text */ /*****************************************************/ CRect rcWnd; CFont TextFont, *oldFont; CWnd *wnd = GetDlgItem(IDC_PLACEHOLDER); // find where to put the text (a static control) ASSERT(wnd != NULL); ASSERT(IsWindow(wnd->m_hWnd) != FALSE); wnd->GetWindowRect(&rcWnd); ScreenToClient(&rcWnd); CDC placeDC; if (!placeDC.Attach(wnd->GetDC()->GetSafeHdc())) ASSERT(FALSE); if (m_dc.GetSafeHdc() == NULL) { if (!m_dc.CreateCompatibleDC(&placeDC)) ASSERT(FALSE); if (!m_bitmapTitle.CreateCompatibleBitmap(&placeDC, rcWnd.Width(), rcWnd.Height())) ASSERT(FALSE); m_pbitmapOldTitle = m_dc.SelectObject(&m_bitmapTitle) ; m_dc.FillSolidRect(0, 0, rcWnd.Width(), rcWnd.Height(), ::GetSysColor(COLOR_3DFACE)); } if (!TextFont.CreateFont (40, 0, 0, 0, 300, // Create the font TRUE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH|FF_SWISS, "Arial")) ASSERT(FALSE); oldFont = m_dc.SelectObject(&TextFont); // select the font m_dc.SetTextColor (RGB(128, 0, 0)) ; // set a bunch of attributes of the dc m_dc.SetBkMode(OPAQUE); m_dc.SetBkColor(::GetSysColor(COLOR_3DFACE)); m_dc.SetTextAlign (TA_LEFT); CString Title = "Test "; m_dc.TextOut(0, 0, Title); m_dc.SelectObject(oldFont) ; placeDC.BitBlt(0, 0, rcWnd.Width(), rcWnd.Height(), &m_dc, 0, 0, SRCCOPY); Basically your problem was that a CBitmap created under 98 using CreateCompatibleBitmap is full of random garbage. The line m_dc.FillSolidRect(0, 0, rcWnd.Width(), rcWnd.Height(), ::GetSysColor(COLOR_3DFACE)); fixes this. You should have had text in the top corner though, but I'm not sure why you did not as I did a partial rewrite to get rid of unnecessary DC's, etc. Where in Australia are you ? ( I'm in Tassie ) Christian The content of this post is not necessarily the opinion of my yadda yadda yadda. To understand recursion, we must first understand recursion.
-
Without a real context (not a DC context) it's pretty hard to tell. I can however tell you that you're in error to not check the return codes from each and every function above why I'd say you deserve the errors. After you've added those checks, perhaps you can see for yourself what went wrong? If not, that would be a good point to ask this question. P.S. If you're the "Senior Test Engineer" for that company, would you mind telling me what products you sell so I know what to stay clear of?
The code I posted was a cut and paste into a demo project, I removed a lot of stuff to make it more readable and easy to see what I was trying to do. We don't sell any products, so rest assured you need not live in fear of coming across any software that might hurt your pwetty wittle compewter.... :-D I'd be more than happy to tell you the products we test, but wait, what's that?? you're anonymous, silly me... Thanks for the constructive criticism... Senior Test Engineer GLI Australia www.gli.com.au
-
I've rewritten your function, I hope you find it a little neater :) /*****************************************************/ /* draw some text */ /*****************************************************/ CRect rcWnd; CFont TextFont, *oldFont; CWnd *wnd = GetDlgItem(IDC_PLACEHOLDER); // find where to put the text (a static control) ASSERT(wnd != NULL); ASSERT(IsWindow(wnd->m_hWnd) != FALSE); wnd->GetWindowRect(&rcWnd); ScreenToClient(&rcWnd); CDC placeDC; if (!placeDC.Attach(wnd->GetDC()->GetSafeHdc())) ASSERT(FALSE); if (m_dc.GetSafeHdc() == NULL) { if (!m_dc.CreateCompatibleDC(&placeDC)) ASSERT(FALSE); if (!m_bitmapTitle.CreateCompatibleBitmap(&placeDC, rcWnd.Width(), rcWnd.Height())) ASSERT(FALSE); m_pbitmapOldTitle = m_dc.SelectObject(&m_bitmapTitle) ; m_dc.FillSolidRect(0, 0, rcWnd.Width(), rcWnd.Height(), ::GetSysColor(COLOR_3DFACE)); } if (!TextFont.CreateFont (40, 0, 0, 0, 300, // Create the font TRUE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH|FF_SWISS, "Arial")) ASSERT(FALSE); oldFont = m_dc.SelectObject(&TextFont); // select the font m_dc.SetTextColor (RGB(128, 0, 0)) ; // set a bunch of attributes of the dc m_dc.SetBkMode(OPAQUE); m_dc.SetBkColor(::GetSysColor(COLOR_3DFACE)); m_dc.SetTextAlign (TA_LEFT); CString Title = "Test "; m_dc.TextOut(0, 0, Title); m_dc.SelectObject(oldFont) ; placeDC.BitBlt(0, 0, rcWnd.Width(), rcWnd.Height(), &m_dc, 0, 0, SRCCOPY); Basically your problem was that a CBitmap created under 98 using CreateCompatibleBitmap is full of random garbage. The line m_dc.FillSolidRect(0, 0, rcWnd.Width(), rcWnd.Height(), ::GetSysColor(COLOR_3DFACE)); fixes this. You should have had text in the top corner though, but I'm not sure why you did not as I did a partial rewrite to get rid of unnecessary DC's, etc. Where in Australia are you ? ( I'm in Tassie ) Christian The content of this post is not necessarily the opinion of my yadda yadda yadda. To understand recursion, we must first understand recursion.
Thanks Christian, it's good to see that some people are still considerate enough to help another person with their work. Regards Liam Senior Test Engineer GLI Australia www.gli.com.au