mfc >> Drawing on CBitmap

by lynnzyeo » Tue, 21 Mar 2006 12:57:25 GMT

Hi, I have the following code extract:

In CView constructor:
m_Bitmap.CreateBitmap( 32, 32, 1, 16, NULL );

In OnDraw():
BITMAP BM;
m_Bitmap.GetObject( sizeof( BM ), &BM );

CPoint size( BM.bmWidth, BM.bmHeight );
pDC->DPtoLP( &size );

CPoint org( 0, 0 );
pDC->DPtoLP( &org );

CDC MemDC;
MemDC.CreateCompatibleDC( NULL );
CBitmap *POldBitmap = MemDC.SelectObject( &m_Bitmap );
MemDC.SetMapMode( pDC->GetMapMode() );

pDC->BitBlt( 0, 0, 32, 32, &MemDC, 0, 0, SRCCOPY );

MemDC.SelectObject( POldBitmap );

In OnTimer:
CClientDC ClientDC( this );

CDC MemDC;

CBitmap* pOldBitmap;

MemDC.CreateCompatibleDC( &ClientDC );

pOldBitmap = MemDC.SelectObject( &m_Bitmap );

MemDC.PatBlt( 0, 0, 32, 32, BLACKNESS );

MemDC.Ellipse( 2, 2, 30, 30 );

MemDC.MoveTo( 0, 0 );
MemDC.LineTo( 100, 500 );

However, nothing is drawn. The screen remains blank. I have no idea
where is thye problem. Can someone pls help? Thanks alot!



mfc >> Drawing on CBitmap

by AliR. » Tue, 21 Mar 2006 14:13:20 GMT


There are a few things wrong with the code.

Most important one is in your OnTimer
You have to select the oldbitmap back into the Memory DC. And also you
want to probably invalidate the view so that the OnDraw method gets
called

In OnTimer:

CClientDC ClientDC( this );
CDC MemDC;
CBitmap* pOldBitmap;
MemDC.CreateCompatibleDC( &ClientDC );
pOldBitmap = MemDC.SelectObject( &m_Bitmap );
MemDC.PatBlt( 0, 0, 32, 32, BLACKNESS );
MemDC.Ellipse( 2, 2, 30, 30 );
MemDC.MoveTo( 0, 0 );
MemDC.LineTo( 100, 500 );

////////// select the old bitmap back into MemoryDC
MemDC.SelectObject(pOldBitmap);

//////// force window to redraw
Invalidate();
UpdateWindow();

The next thing is the bitmap creation, and the memory DC creation.

You are creating the Bitmap with the assumtion that the Device is in 16
bit color mode.

You might want to move the creation into OnInitialUpdate and change it
to

CDC *pDC = GetDC();
m_Bitmap.CreateCompatibleBitmap(pDC, 32, 32);
ReleaseDC(pDC);

and in your OnDraw you are creating your Memory DC with NULL parameter.
Should probably change it to:


MemDC.CreateCompatibleDC( pDC );

AliR.




Similar Threads

1. Mirror / rotate image ( System.Drawing.Graphics / System.Drawing.Drawing2D.Matrix) - CSharp/C#

2. Mirror / rotate Graphics ( System.Drawing.Graphics / System.Drawing.Drawing2D.Matrix)

Kevin Spencer schreef:
> To mirror an image, first you need to decide whether to mirror it 
> horizontally or vertically. Once you've done that, you simply draw it a 
> pixel at a time (use an unsafe code block), in reverse order of rows or 
> columns.
And i should do the rotation in this same step?
Furthermore, arent their any 3th party libs which i could use?

ps: changed subject...

3. How custam draw separator in custom draw menu

4. how can I call the default drawing in owner draw

I have a CTabCtrl and want to do owner draw on it.
But I just want to owner draw a part of the CTabCtrl, i.e. I just want to 
remove the bottom border edge of CTabCtrl, and I want the other part of 
CTabCtrl untouched.
But how can I write the code to let other part(tab, left/right border) draw 
themselves same as MS do? Can MS let us call the default drawing functions of 
CTabCtrl?

5. Custom Drawn CTreeCtrl disabling windows draw faculties

6. owner draw CListBox, how to draw "no item found"

Hello,
I have an owner draw listbox, in the DrawItem I draw the items.

If the listbox doesn't contain any item I would like to display a message
"no item found"
but this is no item, so DrawItem is the wrong place.
Where should I do this? OnPaint, OnEraseBkgnd?
I thought I would be able to do something like this for a test (just paint 
the window blue)
but it's always white
BOOL CmyListbox::OnEraseBkgnd(CDC* pDC)
{
    if (m_pItemList.GetCount() < 1)
    {
         CRect rc;
         GetClientRect(&rc);
        pDC->FillSolidRect(rc,RGB(0,0,255));
        return TRUE;
    }
}
how is this supoosed to be done?

regards
Niklas 


7. a new drawing not erase previous drawing?

8. Flicker when drawing owner-drawn control

WinCE 5.0 / EVC 4.0

I am dealing with a flicker issue when drawing an owner drawn custom 
control.  I am hopeful that someone here can help me out.

I made a subclass of the CButton class which handles all of the drawing 
functions.
The class already addresses the usual suspects:

- double-buffering
- override OnEraseBkgnd and return 1

...but I still have flicker when drawing the control, whether I call 
Invalidate or InvalidateRect on the object.

This object is redrawn in response to data received on the main dialog 
(parent), which then calls object->Invalidate().  Is there anything wrong 
with this approach?

Below is an abridged version of my DrawItem( ) method...

***************************************
this->GetClientRect(&DrawRect);
pDC = this->GetDC();
MemDC.CreateCompatibleDC(pDC);
MemBitmap.CreateCompatibleBitmap(pDC, (DrawRect.right - DrawRect.left), 
(DrawRect.bottom - DrawRect.top));

// drawing code here //

pDC->BitBlt(DrawRect.left, DrawRect.top, (DrawRect.right - DrawRect.left), 
(DrawRect.bottom - DrawRect.top), &MemDC, 0, 0, SRCCOPY);

// cleanup code here //

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

The OnPaint( ) method simply calls CButton::OnPaint( );

Originally, I had the drawing code as part of the main dialog - with no 
flicker.  It made more sense for the object to draw itself, so I went the 
owner drawn control route, but now I have flicker.

Thanks for helping,
- eepcat