HDC

Env: WindowsXP, VC++6.0

Who can kindly tell me the different about using hDC1 or hDC2 inside 
OnDraw() below.

CView::OnDraw(CDC* pDC)
{
    HDC hDC1 = pDC->m_hDC;
    HDC hDC2 = ::GetDC(m_hWnd);
}

TIA
William 

0
port (143)
4/2/2007 12:26:09 AM
vc.mfc 33608 articles. 0 followers. Follow

11 Replies
205 Views

Similar Articles

[PageSpeed] 59

William wrote:
> Env: WindowsXP, VC++6.0
> 
> Who can kindly tell me the different about using hDC1 or hDC2 inside 
> OnDraw() below.
> 
> CView::OnDraw(CDC* pDC)
> {
>    HDC hDC1 = pDC->m_hDC;
>    HDC hDC2 = ::GetDC(m_hWnd);
> }
> 
> TIA
> William

The DC passed to OnDraw is special in several ways.  It is normally a 
CPaintDC, which is specialized to support the Windows scheme for sharing 
the screen with multiple programs.  This DC has a clipping region that 
corresponds to the part of your window that needs to be redrawn, and the 
destruction of this DC is what informs Windows that your window painting 
is now valid.  It is also used when drawing a print preview window.

See the BeginPaint/EndPaint APIs for additional info.  CPaintDC calls 
BeginPaint and EndPaint for you.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
4/2/2007 12:52:33 AM
Thanks Scott.

So, just INSIDE OnDraw(), are they same or different?

I asked it because another problem I'm facing.

My app need to deal with large DIB bitmap(256color, cx=4000, cy=4000) with 
scale(x8) and mask display function.
I did as follows,

 hdcImage = CreateCompatibleDC(hdcParent);
 hbmImage = Create256DIBitmap(hdcParent, cxImage, cyImage, FALSE );
 SelectObject(hdcImage, hbmImage);

 hdcMask = CreateCompatibleDC(hdcImage);
 hbmMask = Create256DIBitmap(hdcParent, cxImage, cyImage, TRUE);
 SelectObject(hdcMask, hbmMask);

 hdcShow = CreateCompatibleDC(hdcImage);
 hbmShow = CreateCompatibleBitmap(hdcImage, cxScale, cyScale);
 SelectObject(hdcShow, hbmShow);

To show the image, I call the following function from CMyView::OnDraw(CDC* 
pDC)
void CMyView::OnDrawScaleImage(CDC* pDC)
{
    BYTE MaskByte = 0x00~0xFF;
     memset(pvBitsMask, MaskByte, nSizeOfColorBitMask);
    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);

    StretchBlt( hdcShow, 0, 0, cxScale, cyScale, hdcMask, 0, 0, cxImage,
cyImage, SRCCOPY);

    // Copy the bits to the screen.
    BitBlt(pDC->m_hDC, 0, 0, cxScale, cyScale, hdcShow, 0, 0, SRCCOPY);
}

I find that StretchBlt() will use quite a lot of system memory when 
cxScale=cxImage*8, cyScale=cyImage*8.

On the other hand, if I edit the same bitmap(256color, cx=cy=1000) under 
MSPaint.exe, I can't find obvious increase of memory usage when I change the 
scale from x1 to x8.

To solve the problem, I am trying to StretchBlt just Window size bitmap to 
pDC->m_hDC directly(not using hdcShow) as below.

void CMyView::OnDraw(CDC* pDC)
{
    CRect rcClient;
    GetClientRect(&rcClient);

    HDC hDC1 = pDC->m_hDC;
    HDC hDC2 = ::GetDC(m_hWnd);

    //get scroll info
     SCROLLINFO si;
     si.cbSize = sizeof(SCROLLINFO);
     si.fMask = SIF_POS;
     GetScrollInfo(SB_HORZ, &si);
     m_ptShiftZ.x = -si.nPos;
     GetScrollInfo(SB_VERT, &si);
     m_ptShiftZ.y = -si.nPos;

     //move rcClient
    rcClient.OffsetRect(-1 * m_ptShiftZ.x, -1 * m_ptShiftZ.y );

    memset(pvBitsMask, BCtrlByte, nSizeOfColorBitMask);
    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);

     StretchBlt( hDC1, 0, 0, rcClient.Width(), rcClient.Height(),
    hdcMask, (rcClient.left) / iZoom, rcClient.top / iZoom,
    rcClient.right / iZoom, rcClient.bottom / iZoom, SRCCOPY);
}

But a new problem appears. That is the bitmap on the right side of window is 
not moved in when we click scrollbar button to try to shift the bitmap to 
the left. There is no such problem, however, if I use hDC2 instead of hDC1.

William


> The DC passed to OnDraw is special in several ways.  It is normally a 
> CPaintDC, which is specialized to support the Windows scheme for sharing 
> the screen with multiple programs.  This DC has a clipping region that 
> corresponds to the part of your window that needs to be redrawn, and the 
> destruction of this DC is what informs Windows that your window painting 
> is now valid.  It is also used when drawing a print preview window.
>
> See the BeginPaint/EndPaint APIs for additional info.  CPaintDC calls 
> BeginPaint and EndPaint for you.
> Scott McPhillips [VC++ MVP]

0
port (143)
4/2/2007 2:08:49 AM
In neither case should you need an HDC.  What are you doing that requires an HDC?

THere is no reason to use a ::GetDC in this case; it gives you a brand new DC, which is
not properly initialized (the OnPrepareDC has already set the CDC * up, and you are
ignoring all those settings) and in any case you failed to release hDC2.  The number of
times you need to use ::GetDC in MFC is vanishingly small, and this is not one of them.
					joe

On Mon, 2 Apr 2007 09:26:09 +0900, "William" <port@mx15.freecom.ne.jp> wrote:

>Env: WindowsXP, VC++6.0
>
>Who can kindly tell me the different about using hDC1 or hDC2 inside 
>OnDraw() below.
>
>CView::OnDraw(CDC* pDC)
>{
>    HDC hDC1 = pDC->m_hDC;
>    HDC hDC2 = ::GetDC(m_hWnd);
>}
>
>TIA
>William 
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15978)
4/2/2007 2:46:52 AM
See below...
On Mon, 2 Apr 2007 11:08:49 +0900, "William" <port@mx15.freecom.ne.jp> wrote:

>Thanks Scott.
>
>So, just INSIDE OnDraw(), are they same or different?
>
>I asked it because another problem I'm facing.
>
>My app need to deal with large DIB bitmap(256color, cx=4000, cy=4000) with 
>scale(x8) and mask display function.
>I did as follows,
>
> hdcImage = CreateCompatibleDC(hdcParent);
> hbmImage = Create256DIBitmap(hdcParent, cxImage, cyImage, FALSE );
> SelectObject(hdcImage, hbmImage);
****
Why the fascination with HDCs?  I note that you did not tell us where hdcParent comes from
or why you are using an HDC in an OnDraw handler.

CDC Image;
Image.CreateCompatibleDC(parentDC);  // parentDC is a CDC *

I have no idea what Create256DIBitmap is, but presumably it is somethingyou wrote.  Why
not use a CDC * for the parameters? What is hbmImage; I note you did not show the
declaration.

HBITMAP bmp = Create256DIBitmap(&Image, cxImage, cyImage, FALSE);

Image.SelectObject(bmp);
*****
>
> hdcMask = CreateCompatibleDC(hdcImage);
> hbmMask = Create256DIBitmap(hdcParent, cxImage, cyImage, TRUE);
> SelectObject(hdcMask, hbmMask);
>
> hdcShow = CreateCompatibleDC(hdcImage);
> hbmShow = CreateCompatibleBitmap(hdcImage, cxScale, cyScale);
> SelectObject(hdcShow, hbmShow);
>
>To show the image, I call the following function from CMyView::OnDraw(CDC* 
>pDC)
>void CMyView::OnDrawScaleImage(CDC* pDC)
>{
>    BYTE MaskByte = 0x00~0xFF;
****
~ is not an infix operator; what did you intend here?
****
>     memset(pvBitsMask, MaskByte, nSizeOfColorBitMask);
>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>
>    StretchBlt( hdcShow, 0, 0, cxScale, cyScale, hdcMask, 0, 0, cxImage,
>cyImage, SRCCOPY);
>
>    // Copy the bits to the screen.
>    BitBlt(pDC->m_hDC, 0, 0, cxScale, cyScale, hdcShow, 0, 0, SRCCOPY);
****
Still not sure why the horrid fascination with raw HDCs.    and why not just do
	pDC->BitBlt(0,0, cxScale, cyScale, &show, 0, 0, SRCCOPY);
where show is
CDC show;
?
Why program in low-level graphics primitives when you can use MFC?  Also, when using APIs,
it is good practice to prefix them with ::.
****
>}
>
>I find that StretchBlt() will use quite a lot of system memory when 
>cxScale=cxImage*8, cyScale=cyImage*8.
>
>On the other hand, if I edit the same bitmap(256color, cx=cy=1000) under 
>MSPaint.exe, I can't find obvious increase of memory usage when I change the 
>scale from x1 to x8.
>
>To solve the problem, I am trying to StretchBlt just Window size bitmap to 
>pDC->m_hDC directly(not using hdcShow) as below.
>
>void CMyView::OnDraw(CDC* pDC)
>{
>    CRect rcClient;
>    GetClientRect(&rcClient);
>
>    HDC hDC1 = pDC->m_hDC;
>    HDC hDC2 = ::GetDC(m_hWnd);
****
Lose the hDC2.   It does not appear to serve a useful purpose
****
>
>    //get scroll info
>     SCROLLINFO si;
>     si.cbSize = sizeof(SCROLLINFO);
>     si.fMask = SIF_POS;
>     GetScrollInfo(SB_HORZ, &si);
>     m_ptShiftZ.x = -si.nPos;
>     GetScrollInfo(SB_VERT, &si);
>     m_ptShiftZ.y = -si.nPos;
*****
Why isn't this in OnPrepareDC?
*****
>
>     //move rcClient
>    rcClient.OffsetRect(-1 * m_ptShiftZ.x, -1 * m_ptShiftZ.y );
>
>    memset(pvBitsMask, BCtrlByte, nSizeOfColorBitMask);
>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>
>     StretchBlt( hDC1, 0, 0, rcClient.Width(), rcClient.Height(),
>    hdcMask, (rcClient.left) / iZoom, rcClient.top / iZoom,
>    rcClient.right / iZoom, rcClient.bottom / iZoom, SRCCOPY);
****
And where, exactly, did you free up hDC2? Why do you use raw HDCs when there are already
MFC methods of the CDC class.
	pDC->StretchBlt(0,0, rcClient.Width(), rcClient.Height(), ...etc...)
would do the job perfectly well.
****
>}
>
>But a new problem appears. That is the bitmap on the right side of window is 
>not moved in when we click scrollbar button to try to shift the bitmap to 
>the left. There is no such problem, however, if I use hDC2 instead of hDC1.
*****
This is because HDC1 has a clipping region.  So you are failing to invalidate the proper
rectangle.  For that matter, why isn't your scrollbar logic using ScrollWindow to do the
scrolling?

If you invalidate the proper area, you will not have the problem.
					joe
*****
>
>William
>
>
>> The DC passed to OnDraw is special in several ways.  It is normally a 
>> CPaintDC, which is specialized to support the Windows scheme for sharing 
>> the screen with multiple programs.  This DC has a clipping region that 
>> corresponds to the part of your window that needs to be redrawn, and the 
>> destruction of this DC is what informs Windows that your window painting 
>> is now valid.  It is also used when drawing a print preview window.
>>
>> See the BeginPaint/EndPaint APIs for additional info.  CPaintDC calls 
>> BeginPaint and EndPaint for you.
>> Scott McPhillips [VC++ MVP]
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15978)
4/2/2007 3:01:03 AM
William wrote:
> Thanks Scott.
> 
> So, just INSIDE OnDraw(), are they same or different?
> ...
> 
> But a new problem appears. That is the bitmap on the right side of 
> window is not moved in when we click scrollbar button to try to shift 
> the bitmap to the left. There is no such problem, however, if I use hDC2 
> instead of hDC1.

Is your view a CScrollView or CFormView?  If so, the pDC passed to 
OnDraw has had its coordinate system offset to account for the scroll 
position. (by OnPrepareDC)

-- 
Scott McPhillips [VC++ MVP]

0
Scott
4/2/2007 3:12:55 AM
Thanks Joseph.

I use HDC becasue there is a problem in using CDC* pDC->m_hDC.
Please refer the thread before for details.

William

> In neither case should you need an HDC.  What are you doing that requires 
> an HDC?
>
> THere is no reason to use a ::GetDC in this case; it gives you a brand new 
> DC, which is
> not properly initialized (the OnPrepareDC has already set the CDC * up, 
> and you are
> ignoring all those settings) and in any case you failed to release hDC2. 
> The number of
> times you need to use ::GetDC in MFC is vanishingly small, and this is not 
> one of them.

0
port (143)
4/2/2007 3:13:35 AM
CMyView is based on CScrollView.

So, why does it (pDC passed to OnDraw) act correctly?

William

> Is your view a CScrollView or CFormView?  If so, the pDC passed to 
> OnDraw has had its coordinate system offset to account for the scroll 
> position. (by OnPrepareDC)

0
port (143)
4/2/2007 3:21:39 AM
hdcParent comes from

 HDC hdcParent = GetDC(m_hWnd);

I use HDC in OnDraw because pDC->m_hDC seems to me not doest work correctly
for bitmap show when scroll bar is moved.

hbmImage is a DIB bitmap created by my Create256DIBitmap() and initialized
by ny bitmap file.

CDC * is not the parameters becasue I have many other functions needing a
HDC as a parameter.

MaskByte is a BYTE variable used to control which bit in hbmImage is
displayed. MaskByte can be from 0x00 to 0xff.

I don't think there are any differents bewteen BitBlt(pDC->m_hDC) and
pDC->BitBlt(). It was not the reason of the problem I guess.

> Why the fascination with HDCs?  I note that you did not tell us where
> hdcParent comes from
> or why you are using an HDC in an OnDraw handler.
>
> CDC Image;
> Image.CreateCompatibleDC(parentDC);  // parentDC is a CDC *
>
> I have no idea what Create256DIBitmap is, but presumably it is
> somethingyou wrote.  Why
> not use a CDC * for the parameters? What is hbmImage; I note you did not
> show the
> declaration.
>
> HBITMAP bmp = Create256DIBitmap(&Image, cxImage, cyImage, FALSE);
>
> Image.SelectObject(bmp);
> *****
>>
>> hdcMask = CreateCompatibleDC(hdcImage);
>> hbmMask = Create256DIBitmap(hdcParent, cxImage, cyImage, TRUE);
>> SelectObject(hdcMask, hbmMask);
>>
>> hdcShow = CreateCompatibleDC(hdcImage);
>> hbmShow = CreateCompatibleBitmap(hdcImage, cxScale, cyScale);
>> SelectObject(hdcShow, hbmShow);
>>
>>To show the image, I call the following function from CMyView::OnDraw(CDC*
>>pDC)
>>void CMyView::OnDrawScaleImage(CDC* pDC)
>>{
>>    BYTE MaskByte = 0x00~0xFF;
> ****
> ~ is not an infix operator; what did you intend here?
> ****
>>     memset(pvBitsMask, MaskByte, nSizeOfColorBitMask);
>>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>>
>>    StretchBlt( hdcShow, 0, 0, cxScale, cyScale, hdcMask, 0, 0, cxImage,
>>cyImage, SRCCOPY);
>>
>>    // Copy the bits to the screen.
>>    BitBlt(pDC->m_hDC, 0, 0, cxScale, cyScale, hdcShow, 0, 0, SRCCOPY);
> ****
> Still not sure why the horrid fascination with raw HDCs.    and why not
> just do
> pDC->BitBlt(0,0, cxScale, cyScale, &show, 0, 0, SRCCOPY);
> where show is
> CDC show;
> ?
> Why program in low-level graphics primitives when you can use MFC?  Also,
> when using APIs,
> it is good practice to prefix them with ::.
> ****
>>}
>>
>>I find that StretchBlt() will use quite a lot of system memory when
>>cxScale=cxImage*8, cyScale=cyImage*8.
>>
>>On the other hand, if I edit the same bitmap(256color, cx=cy=1000) under
>>MSPaint.exe, I can't find obvious increase of memory usage when I change
>>the
>>scale from x1 to x8.
>>
>>To solve the problem, I am trying to StretchBlt just Window size bitmap to
>>pDC->m_hDC directly(not using hdcShow) as below.
>>
>>void CMyView::OnDraw(CDC* pDC)
>>{
>>    CRect rcClient;
>>    GetClientRect(&rcClient);
>>
>>    HDC hDC1 = pDC->m_hDC;
>>    HDC hDC2 = ::GetDC(m_hWnd);
> ****
> Lose the hDC2.   It does not appear to serve a useful purpose
> ****
>>
>>    //get scroll info
>>     SCROLLINFO si;
>>     si.cbSize = sizeof(SCROLLINFO);
>>     si.fMask = SIF_POS;
>>     GetScrollInfo(SB_HORZ, &si);
>>     m_ptShiftZ.x = -si.nPos;
>>     GetScrollInfo(SB_VERT, &si);
>>     m_ptShiftZ.y = -si.nPos;
> *****
> Why isn't this in OnPrepareDC?
> *****
>>
>>     //move rcClient
>>    rcClient.OffsetRect(-1 * m_ptShiftZ.x, -1 * m_ptShiftZ.y );
>>
>>    memset(pvBitsMask, BCtrlByte, nSizeOfColorBitMask);
>>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>>
>>     StretchBlt( hDC1, 0, 0, rcClient.Width(), rcClient.Height(),
>>    hdcMask, (rcClient.left) / iZoom, rcClient.top / iZoom,
>>    rcClient.right / iZoom, rcClient.bottom / iZoom, SRCCOPY);
> ****
> And where, exactly, did you free up hDC2? Why do you use raw HDCs when
> there are already
> MFC methods of the CDC class.
> pDC->StretchBlt(0,0, rcClient.Width(), rcClient.Height(), ...etc...)
> would do the job perfectly well.
> ****
>>}
>>
>>But a new problem appears. That is the bitmap on the right side of window
>>is
>>not moved in when we click scrollbar button to try to shift the bitmap to
>>the left. There is no such problem, however, if I use hDC2 instead of
>>hDC1.
> *****
> This is because HDC1 has a clipping region.  So you are failing to
> invalidate the proper
> rectangle.  For that matter, why isn't your scrollbar logic using
> ScrollWindow to do the
> scrolling?
>
> If you invalidate the proper area, you will not have the problem.
> joe
> *****
>>
>>William
>>
>>
>>> The DC passed to OnDraw is special in several ways.  It is normally a
>>> CPaintDC, which is specialized to support the Windows scheme for sharing
>>> the screen with multiple programs.  This DC has a clipping region that
>>> corresponds to the part of your window that needs to be redrawn, and the
>>> destruction of this DC is what informs Windows that your window painting
>>> is now valid.  It is also used when drawing a print preview window.
>>>
>>> See the BeginPaint/EndPaint APIs for additional info.  CPaintDC calls
>>> BeginPaint and EndPaint for you.
>>> Scott McPhillips [VC++ MVP]
> Joseph M. Newcomer [MVP]
> email: newcomer@flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm

0
port (143)
4/2/2007 5:05:55 AM
Well, I just try CDC::GetClipBox() inside CMyView::OnDraw(CDC* pDC) as 
below.

void CDIBView::OnDraw(CDC* pDC)
{
     CRect rcClient;
     int iResult;
     iResult = pDC->GetClipBox(&rcClient);
}

But why the return is  NULLREGION? why rcClient is empty?

William

0
port (143)
4/2/2007 5:09:27 AM
See below...
On Mon, 2 Apr 2007 14:05:55 +0900, "William" <port@mx15.freecom.ne.jp> wrote:

>hdcParent comes from
>
> HDC hdcParent = GetDC(m_hWnd);
****
Another example of a horrible fascination with HDCs.  Why aren't you writing
CClientDC dc(this);?
But you don't need to do this at all; you should be using the CDC passed in from the
OnPaint handler, which has the correct clipping region already set.  Of course, this DOES
presume that you did the correct InvalidateRect; if you got this wrong, then nothing will
work right.  Key here is to fix the real problems at the place they occurred, not create
kludges to compensate for bugs which are elsewhere.
*****
>
>I use HDC in OnDraw because pDC->m_hDC seems to me not doest work correctly
>for bitmap show when scroll bar is moved.
****
Probably because you are using the wrong DC.  If you are in OnDraw, you should be using
the CDC * passed in, and you should be handling the scrollbar processing in OnPrepareDC.
It looks like you failed to do this, so the incoming DC doesn'thave the proper setup.  The
correct solution is not to use HDCs, but to put the correct code in OnPrepareDC so the
incoming DC is correct
*****
>
>hbmImage is a DIB bitmap created by my Create256DIBitmap() and initialized
>by ny bitmap file.
>
>CDC * is not the parameters becasue I have many other functions needing a
>HDC as a parameter.
****
This is probably a design error.  Rethink what you are doing.
****
>
>MaskByte is a BYTE variable used to control which bit in hbmImage is
>displayed. MaskByte can be from 0x00 to 0xff.
>
>I don't think there are any differents bewteen BitBlt(pDC->m_hDC) and
>pDC->BitBlt(). It was not the reason of the problem I guess.
****
You are exhibiting a tendency to try to bypass the MFC logic, and this leads to all kinds
of situations in which you get the wrong kinds of interactions as you fight the built-in
logic already present in MFC.  Your problem with the CDC * parameter is an exemplar of
this.  Instead of fixing the problem correctly, you create a weird workaround that doesn't
work either, and this forces you to make cascading decisions for additional weird
workarounds.
*****
>
>> Why the fascination with HDCs?  I note that you did not tell us where
>> hdcParent comes from
>> or why you are using an HDC in an OnDraw handler.
>>
>> CDC Image;
>> Image.CreateCompatibleDC(parentDC);  // parentDC is a CDC *
>>
>> I have no idea what Create256DIBitmap is, but presumably it is
>> somethingyou wrote.  Why
>> not use a CDC * for the parameters? What is hbmImage; I note you did not
>> show the
>> declaration.
>>
>> HBITMAP bmp = Create256DIBitmap(&Image, cxImage, cyImage, FALSE);
>>
>> Image.SelectObject(bmp);
>> *****
>>>
>>> hdcMask = CreateCompatibleDC(hdcImage);
>>> hbmMask = Create256DIBitmap(hdcParent, cxImage, cyImage, TRUE);
>>> SelectObject(hdcMask, hbmMask);
>>>
>>> hdcShow = CreateCompatibleDC(hdcImage);
>>> hbmShow = CreateCompatibleBitmap(hdcImage, cxScale, cyScale);
>>> SelectObject(hdcShow, hbmShow);
>>>
>>>To show the image, I call the following function from CMyView::OnDraw(CDC*
>>>pDC)
>>>void CMyView::OnDrawScaleImage(CDC* pDC)
>>>{
>>>    BYTE MaskByte = 0x00~0xFF;
>> ****
>> ~ is not an infix operator; what did you intend here?
>> ****
>>>     memset(pvBitsMask, MaskByte, nSizeOfColorBitMask);
>>>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>>>
>>>    StretchBlt( hdcShow, 0, 0, cxScale, cyScale, hdcMask, 0, 0, cxImage,
>>>cyImage, SRCCOPY);
>>>
>>>    // Copy the bits to the screen.
>>>    BitBlt(pDC->m_hDC, 0, 0, cxScale, cyScale, hdcShow, 0, 0, SRCCOPY);
>> ****
>> Still not sure why the horrid fascination with raw HDCs.    and why not
>> just do
>> pDC->BitBlt(0,0, cxScale, cyScale, &show, 0, 0, SRCCOPY);
>> where show is
>> CDC show;
>> ?
>> Why program in low-level graphics primitives when you can use MFC?  Also,
>> when using APIs,
>> it is good practice to prefix them with ::.
>> ****
>>>}
>>>
>>>I find that StretchBlt() will use quite a lot of system memory when
>>>cxScale=cxImage*8, cyScale=cyImage*8.
>>>
>>>On the other hand, if I edit the same bitmap(256color, cx=cy=1000) under
>>>MSPaint.exe, I can't find obvious increase of memory usage when I change
>>>the
>>>scale from x1 to x8.
>>>
>>>To solve the problem, I am trying to StretchBlt just Window size bitmap to
>>>pDC->m_hDC directly(not using hdcShow) as below.
>>>
>>>void CMyView::OnDraw(CDC* pDC)
>>>{
>>>    CRect rcClient;
>>>    GetClientRect(&rcClient);
>>>
>>>    HDC hDC1 = pDC->m_hDC;
>>>    HDC hDC2 = ::GetDC(m_hWnd);
>> ****
>> Lose the hDC2.   It does not appear to serve a useful purpose
>> ****
>>>
>>>    //get scroll info
>>>     SCROLLINFO si;
>>>     si.cbSize = sizeof(SCROLLINFO);
>>>     si.fMask = SIF_POS;
>>>     GetScrollInfo(SB_HORZ, &si);
>>>     m_ptShiftZ.x = -si.nPos;
>>>     GetScrollInfo(SB_VERT, &si);
>>>     m_ptShiftZ.y = -si.nPos;
>> *****
>> Why isn't this in OnPrepareDC?
>> *****
>>>
>>>     //move rcClient
>>>    rcClient.OffsetRect(-1 * m_ptShiftZ.x, -1 * m_ptShiftZ.y );
>>>
>>>    memset(pvBitsMask, BCtrlByte, nSizeOfColorBitMask);
>>>    BitBlt( hdcMask, 0, 0, cxImage, cyImage, hdcImage, 0, 0, SRCAND);
>>>
>>>     StretchBlt( hDC1, 0, 0, rcClient.Width(), rcClient.Height(),
>>>    hdcMask, (rcClient.left) / iZoom, rcClient.top / iZoom,
>>>    rcClient.right / iZoom, rcClient.bottom / iZoom, SRCCOPY);
>> ****
>> And where, exactly, did you free up hDC2? Why do you use raw HDCs when
>> there are already
>> MFC methods of the CDC class.
>> pDC->StretchBlt(0,0, rcClient.Width(), rcClient.Height(), ...etc...)
>> would do the job perfectly well.
>> ****
>>>}
>>>
>>>But a new problem appears. That is the bitmap on the right side of window
>>>is
>>>not moved in when we click scrollbar button to try to shift the bitmap to
>>>the left. There is no such problem, however, if I use hDC2 instead of
>>>hDC1.
>> *****
>> This is because HDC1 has a clipping region.  So you are failing to
>> invalidate the proper
>> rectangle.  For that matter, why isn't your scrollbar logic using
>> ScrollWindow to do the
>> scrolling?
>>
>> If you invalidate the proper area, you will not have the problem.
>> joe
>> *****
>>>
>>>William
>>>
>>>
>>>> The DC passed to OnDraw is special in several ways.  It is normally a
>>>> CPaintDC, which is specialized to support the Windows scheme for sharing
>>>> the screen with multiple programs.  This DC has a clipping region that
>>>> corresponds to the part of your window that needs to be redrawn, and the
>>>> destruction of this DC is what informs Windows that your window painting
>>>> is now valid.  It is also used when drawing a print preview window.
>>>>
>>>> See the BeginPaint/EndPaint APIs for additional info.  CPaintDC calls
>>>> BeginPaint and EndPaint for you.
>>>> Scott McPhillips [VC++ MVP]
>> Joseph M. Newcomer [MVP]
>> email: newcomer@flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15978)
4/2/2007 5:36:45 PM
Probably because you have bugs elsewhere in your code.
				joe

On Mon, 2 Apr 2007 12:13:35 +0900, "William" <port@mx15.freecom.ne.jp> wrote:

>Thanks Joseph.
>
>I use HDC becasue there is a problem in using CDC* pDC->m_hDC.
>Please refer the thread before for details.
>
>William
>
>> In neither case should you need an HDC.  What are you doing that requires 
>> an HDC?
>>
>> THere is no reason to use a ::GetDC in this case; it gives you a brand new 
>> DC, which is
>> not properly initialized (the OnPrepareDC has already set the CDC * up, 
>> and you are
>> ignoring all those settings) and in any case you failed to release hDC2. 
>> The number of
>> times you need to use ::GetDC in MFC is vanishingly small, and this is not 
>> one of them.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15978)
4/2/2007 5:37:43 PM
Reply:

Similar Artilces:

how to get hwnd from hdc
Hi, I know how to get a HDC from HWND, but I am given a HDC from window media player and I want to correlate the mouse position with objects on my HDC. In order to use ScreenToClient I need the HDC's hwnd. How is that possible? If I use GetActiveWindow, I have an offset equal to the media player's menu and button panes. -- ------------------------------------------------------------------------- Music, Recipes, Photos, and more: http://www.sequoiagrove.dk "You don�t frighten us, English pig-dogs! Go and boil your bottoms, sons of a silly person. I blow my nose at you, so...

DrawTextW( hdc, strText, -1, rectB, DT_SINGLELINE); #2
Hi, I have a peice of a code for loading a icon and draw the text below the icon on the same button... i was sucessful in loading an icon but when tried with the DrawText() to insert a text, its not working and the problem is with the hdc parameter of DrawText function. void CNewButtonDlg::CallButton() { CString strText(""); //To place the text on the button RECT rectA = {180, 20, 270, 90}; //left, top, right, bottom //To nullify the error that parameter must be a LPRECT and not RECT type i have done so LPRECT rectB = &rectA; CDC my_newButto...

HDC
Env: WindowsXP, VC++6.0 Who can kindly tell me the different about using hDC1 or hDC2 inside OnDraw() below. CView::OnDraw(CDC* pDC) { HDC hDC1 = pDC->m_hDC; HDC hDC2 = ::GetDC(m_hWnd); } TIA William William wrote: > Env: WindowsXP, VC++6.0 > > Who can kindly tell me the different about using hDC1 or hDC2 inside > OnDraw() below. > > CView::OnDraw(CDC* pDC) > { > HDC hDC1 = pDC->m_hDC; > HDC hDC2 = ::GetDC(m_hWnd); > } > > TIA > William The DC passed to OnDraw is special in several ways. It is normally a CPaintDC, which i...

HDC to HDIB
I need to convert hdc to hdib data of 256 colors of size 180*120 But i didn't get output as required plz help me { BITMAPINFO Binf; BITMAPINFOHEADER bi;int ki; HDC hMemDC = CreateCompatibleDC(hDC); int DCWidth = GetDeviceCaps( hDC, HORZRES ); int DCHeight = GetDeviceCaps( hDC, VERTRES ); StretchBlt(hMemDC,0,0,180,120,hDC,0,0,DCWidth,DCHeight,SRCCOPY); bi.biSize = sizeof(BITMAPINFOHEADER);//40; bi.biWidth = 180;//Let it be constant width for our saving bitmaps bi.biHeight = 120;//Let it be constant height for our saving bitmaps bi.biPlanes = 1; bi.biBitCount = 8; bi.b...

get font from HDC
Hi, I would like to modify a function used to render some HTML and use the font I have choosen : // C functions int __stdcall DrawHTML( HDC hdc, // handle of device context LPCTSTR lpString, // address of string to draw int nCount, // string length, in characters LPRECT lpRect, // address of structure with formatting dimensions UINT uFormat // text-drawing flags ) { .... /* get the "default" font from the DC */ SavedD...

HDC and CDC
I am attempting for the first time to use CreateDibSection and am puzzled as to how to use HDC within the MFC framework. I can't see that CDC returns a handle, so I am wondering if these two are mutually exclusive. Is there a bridge between the handle and the class? "Steve Russell" <srussell@removethisinnernet.net> wrote in message news:uQoa24INEHA.624@TK2MSFTNGP11.phx.gbl... > I am attempting for the first time to use CreateDibSection and am puzzled as > to how to use HDC within the MFC framework. I can't see that CDC returns a > handle, so I am wonderin...

HDC Clipping issues
Hi, I am drawing some things in my view. I wish to draw stuff all over the client window, then set a clip rect, such as draw a line from one corner of the screen to another, but make sure that what I draw is only visible on a portion of the view. CMyView::OnDraw(CDC* pDC) { CMyDrawRoutines d; d.BeginDraw(pDC->m_hDC); d.DrawSomeStuffInEntireClientWindow(); CRect rc; GetClientRect(&rc); rc.DelfateRect(100,100,100,100); CRgn rgn; rgn.CreateRectRgnIndirect(&rc); d.SelectClipRgn(&rc); d.MoveTo(0,0); d.LineTo(rcClient.right, rcClient...

CDC/HDC????
I am trying to develop a print screen utility for a windows CE program. I am writing it in Embedded VC++ and running into some weirdness. My plan of action is to use GetClientRect() to find the location of the window, then loop through each pixel in that region and perform a GetPixel() to get the RGB value. My problem is that I need a HDC in order to perform GetPixel(). I used GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd pointer. I then tried the following line to get an HDC. HDC hdc=pWnd->GetWindowDC(); When I compile though I get the following error. erro...

Get BItmap From HDC
I'm stuck with getting CBitmap object from HDC. I'm using ecw SDK to view ecw files, and I need my soft to return CBitmap object of this .ecw file. in SDK there is a method: DrawImage( HDC DeviceContext, LPRECT pClipRect, IEEE8 dWorldTLX, IEEE8 dWorldTLY, IEEE8 dWorldBRX, IEEE8 dWorldBRY ); wich works just fine, it draws picture into givet device context. DrawImage(hdc, &m_Rect, m_dTLX, m_dTLY, m_dBRX, m_dBRY); CBitmap bitmap; CBitmap * bmp=bitmap.FromHandle((HBITMAP)hdc); is this right?? if DrawImage method drawed picture into HDC already?. If you have an HDC, and it has a bi...

how to make a cdc from a hdc ?
I have a HDC, is it possible to make a CDC from it? thanks, bn You can do this: CDC cDC; cDC.Attach(hdc); tankc "bn" <b@b.com> wrote in message news:%23tSzyNJ$EHA.3372@TK2MSFTNGP10.phx.gbl... > I have a HDC, is it possible to make a CDC from it? > > thanks, > bn > > tthere aretwo ways CDC dc; dc.Attach(hdc); //do ur stuff dc.Detach(); or CDC dc=CDC::FromHandle(hdc); //no need to detach. "bn" wrote: > I have a HDC, is it possible to make a CDC from it? > > thanks, > bn > > > The second one sho...

Rookie Q: Release HDC?
Hi, Imagine a little class like this: class CMyClass { public: CMyClass(HDC hDC) { m_hDC = hDC; } HDC GetHDC() { return m_hDC; } private: HDC m_hDC; } Now, in my CMyApp::OnView I do this: void CMyApp::OnView(CDC* pDC) { CMyClass mc(pDC->m_hDC); } Now, HDC is just an UINT, and all I am passing is this number. I shouldn't have to do any relasing of anything, do I? Or does my CMyClass class destructor have to do a DeleteObject(m_hDC) or something? Lisa The HDC is part of the CDC class. Be sure not to delete it, or even store it (except i...

How do I tell what HDC has a HBITMAP selected?
How do I tell which HDC has a specific HBITMAP selected? I am using the ImageList_GetImageInfo() function to get direct access to the HBITMAP in the imagelist. The problem is the system keeps this bitmap selected into a memory DC, so I can't select it into my DC to perform a BitBlt from it. There is a KB article that tells me to use the CopyImage function. CopyImage is terribly slow. Any ideas? ...

Load Tif file in memory hDC
How do I load a TIF file in memory hDC and save it back to another file after some processing using DrawText. I want to use the memory hDC because I dont want to display the image. Is there an example for doing this. Asfar wrote: > How do I load a TIF file in memory hDC and save it back to another file > after some processing using DrawText. > > I want to use the memory hDC because I dont want to display the image. > > Is there an example for doing this. > > I am using Cximage to do this. See here http://www.codeproject.com/bitmap/cximage.asp ...

DrawTextW( hdc, strText, -1, rectB, DT_SINGLELINE);
Hi, I have a peice of a code for loading a icon and draw the text below the icon on the same button... i was sucessful in loading an icon but when tried with the DrawText() to insert a text, its not working and the problem is with the hdc parameter of DrawText function. void CNewButtonDlg::CallButton() { CString strText(""); //To place the text on the button RECT rectA = {180, 20, 270, 90}; //left, top, right, bottom //To nullify the error that parameter must be a LPRECT and not RECT type i have done so LPRECT rectB = &rectA; CDC my_newButto...

slower BitBlt from unmanaged mem into C# hDC than old VB6
BitBlt'ing from unmanaged memory into an hDC obtained in a managed (C#) program takes 50-100% longer than the same BitBlt into an hDC obtained in an unmanaged (old VB6) program. My ShowPic() function resides in a DLL written in straight C. It reads a JPEG into memory allocated within the unmanaged DLL and then StretchDIBits()s it to the hDC passed. Some ancient VB6 code called it in a Paint event: Private Sub Picture1_Paint() ShowPic(Picture1.hdc, ...) End Sub Here is the C# code that is almost 100% slower: private void picObj1_Paint(object sender, System.Windows....

Panasonic HDC-SD5 AVCHD 3CCD Flash Memory High Definition Camcorder with 10x Optical Image Stabilization w/DVD Burner
Price:Too low to display Image: http://thediscountguru.info/image.php?id=B000YT5R28 Best deal: http://thediscountguru.info/index.php?id=B000YT5R28 Works great but in low light conditions the video isn't that great. Well lit and sunny areas the HandyCam captures great HD video. I have owned the camera for 2 weeks now and am returning it. I currently own 3 other video cameras, but this is my forst HD camera. Everything about the camera is wonderful except for one MAJOR flaw, there is a high pitched whine when I play back the vodeo through the camera. It is quite noticeable to say th...