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
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).
On Tue, 04 Jul 2006 22:20:26 GMT, "cdg" <email@example.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.
> CPaintDC dc(this);
> CBitmap *pOld = cdc.SelectObject(&bmp);
> BITMAP BmpInfo;
> bmp.GetObject(sizeof(BmpInfo), &BmpInfo);
> dc.BitBlt(50, 50, BmpInfo.bmWidth, BmpInfo.bmHeight,
> &cdc, 0, 0, SRCCOPY);
Joseph M. Newcomer [MVP]
MVP Tips: http://www.flounder.com/mvp_tips.htm