Using bitmaps with CPaintDC

   Is the code below the right approach to displaying bitmaps to a dialog.
And is there any obvious problems with it, such as not being consistently
displayed on other monitors. Also I would need to display a number of
(small) bitmaps side by side on rows, and will need to add scroll
functionability to this dialog. Does anyone know of the best approach to
displaying these bitmaps in rows, and then adding a scroll for just a
certain area of the dialog where the bitmaps are displayed.

The member variables bmp and cdc are declared in the class declaration.

void CDCBmpTestDlg::OnPaint()
{
 CPaintDC dc(this);

 bmp.LoadBitmap(IDB_DEMO);

 cdc.CreateCompatibleDC(&dc);
 CBitmap *pOld = cdc.SelectObject(&bmp);

 BITMAP BmpInfo;
 bmp.GetObject(sizeof(BmpInfo), &BmpInfo);
 dc.BitBlt(50, 50, BmpInfo.bmWidth, BmpInfo.bmHeight,
                &cdc, 0, 0, SRCCOPY);

 cdc.SelectObject(pOld);
 cdc.DeleteDC();
 bmp.DeleteObject();
}


0
anyone78 (134)
7/4/2006 10:20:26 PM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
1046 Views

Similar Articles

[PageSpeed] 22

You should not be using an OnPaint hander in a dialog.  Period.  Get rid of this entirely.

For a simple single bitmap, use a CStatic control, subclass it to paint the bitmap, and
draw your bitmap there.  Get rid of all silly values like "50".  Work out the value based
either on the actual bitmap size, or a computed ratio of the control, or a control of
known design time dimensions (see below)

When I needed the functionality of multiple bitmaps in a row, with scrolling, I
essentially did "word wrap" in a CWnd-derived control, which I put on the dialog, and
implemented the scrollbar functionality in that control.  Each time on OnPaint was called,
it laid the bitmaps out left-to-right, and when a bitmap wouldn't fit, it "wrapped" it to
the next line.  Unfortunately, I did this code for a client and can't post it or make it
available, but it was essentially straightforward.  It took about five hours to code it
up.

If you need everything to be the "same" size, place a little non-visible static control of
the desired size on your dialog, and when the dialog is instantiated, you can derive the
size of this in pixels.  While the results are not always perfect, you can then do
something like StretchBlt to draw all the bitmaps the same size.  In my case, the listbox
dynamically resized if the dialog was resized, so I had to do the layout each time the
OnPaint handler was called.  The scrollbar was trivial, and follows classical scrollbar
examples (when I did this, I went back to the Win32 Programming book and looked up how to
do scrollbars, but the MSDN is full of examples).
				joe

On Tue, 04 Jul 2006 22:20:26 GMT, "cdg" <anyone@anywhere.com> wrote:

>   Is the code below the right approach to displaying bitmaps to a dialog.
>And is there any obvious problems with it, such as not being consistently
>displayed on other monitors. Also I would need to display a number of
>(small) bitmaps side by side on rows, and will need to add scroll
>functionability to this dialog. Does anyone know of the best approach to
>displaying these bitmaps in rows, and then adding a scroll for just a
>certain area of the dialog where the bitmaps are displayed.
>
>The member variables bmp and cdc are declared in the class declaration.
>
>void CDCBmpTestDlg::OnPaint()
>{
> CPaintDC dc(this);
>
> bmp.LoadBitmap(IDB_DEMO);
>
> cdc.CreateCompatibleDC(&dc);
> CBitmap *pOld = cdc.SelectObject(&bmp);
>
> BITMAP BmpInfo;
> bmp.GetObject(sizeof(BmpInfo), &BmpInfo);
> dc.BitBlt(50, 50, BmpInfo.bmWidth, BmpInfo.bmHeight,
>                &cdc, 0, 0, SRCCOPY);
>
> cdc.SelectObject(pOld);
> cdc.DeleteDC();
> bmp.DeleteObject();
>}
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
7/5/2006 2:50:36 AM
cdg wrote:
>    Is the code below the right approach to displaying bitmaps to a dialog.
> And is there any obvious problems with it, such as not being consistently
> displayed on other monitors. Also I would need to display a number of
> (small) bitmaps side by side on rows, and will need to add scroll
> functionability to this dialog. Does anyone know of the best approach to
> displaying these bitmaps in rows, and then adding a scroll for just a
> certain area of the dialog where the bitmaps are displayed.
> 
> The member variables bmp and cdc are declared in the class declaration.
> 
> void CDCBmpTestDlg::OnPaint()
> {
>  CPaintDC dc(this);
> 
>  bmp.LoadBitmap(IDB_DEMO);
> 
>  cdc.CreateCompatibleDC(&dc);
>  CBitmap *pOld = cdc.SelectObject(&bmp);
> 
>  BITMAP BmpInfo;
>  bmp.GetObject(sizeof(BmpInfo), &BmpInfo);
>  dc.BitBlt(50, 50, BmpInfo.bmWidth, BmpInfo.bmHeight,
>                 &cdc, 0, 0, SRCCOPY);
> 
>  cdc.SelectObject(pOld);
>  cdc.DeleteDC();
>  bmp.DeleteObject();
> }

It is rarely a good idea to paint directly on a dialog.  The dialog does 
its own background painting and this can confuse things.

To put one or more bitmaps on a dialog you would typically put CStatic 
controls on the dialog.  They paint a bitmap for you.

But for rows of bitmaps that can be scrolled a somewhat unobvious 
approach is probably best:  Use an owner-drawn list box.  Each "item" in 
the list box can be a bitmap, and you will get all the scroll management 
for free.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
7/5/2006 3:03:56 AM
Reply:

Similar Artilces: