Flickering: used a cstatic subclass and overide Onpaint
I'm using a CStatic control as type bitmap and made a subclass in it where I overide the OnPaint function where I will be able to output a text. The whole program is that the user can choose either to display a bitmap or to display a text. The flow of the program is that in the Main dialog, I've already created code which will be able to load an image and used setbitmap to display it and if the user wishes to display a text instead then I point it to the subclass. But flickering occurs when that happen and the setbitmap function seems not working anymore once I overide the OnPaint function. Need help....
Here is some of the code:
Code:
On the main dialog:
void MainDlg::LoadAnImage(void)
hBitmap = (HBITMAP)LoadImage( NULL, "C:\\" + strBmp, // strBmp is the filename of the bitmap
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION |
LR_DEFAULTSIZE );
// Load bitmap
if( bIsImg == TRUE ) {
// m_Img is the member variable for the cstatic control
m_Img.SetBitmap( hBitmap ); // set bitmap
} else {
m_Img.DisplayTxt( 0 );
}
On the Subclass:
void CStaticSub::DisplayTxt( BOOL bFg )
{
m_bFlg = bFg
}
void CStaticSub::OnPaint()
{
CPaintDC dc(this);
if( m_bFlg == 0 ) {
m_cBrush.CreateSolidBrush( WHITE );
m_cRect.right =-3;
m_cRect.left = 195;
m_cRect.top = -3;
m_cRect.bottom = 197;
m_bFlg = 1;
}
dc.Rectangle( m_cRect ); // Change the background of the control
dc.SelectObject( m_cBrush );
dc.SelectObject( &m_cFnt ); // Select new font
dc.GetCurrentFont( );
dc.SetBkColor( WHITE );
dc.SetTextColor( BLACK );
dc.TextOut( 3, 60, "Text is Displayed.");
Invalidate();
}
What is wrong with my program? Where should I place the setbitmap function so that it would still work even if I overide the OnPaint function?
Re: Flickering: used a cstatic subclass and overide Onpaint
You should not call Invalidate() inside OnPaint handler, it makes the client region get marked for redraw again even though it is not necessory, which must be the cause of flicker.
on another nore you can use stock objects(already created by GDI) when you want to use standard solid white brush, use
Code:
GetStockObject(DC_BRUSH);
Re: Flickering: used a cstatic subclass and overide Onpaint
ei thanks.. :)
That is also what I just did. I remove the Invalidate from the OnPaint function then the problem that occured next is that the image was getting erased. The cause of it was that the CStatic control that I used was quite small that only the portion that is covered are retained everytime OnPaint function is entered. Thanks for the reply. :)
Re: Flickering: used a cstatic subclass and overide Onpaint
You are welcome ! :thumb:
Anything which works, is great. :D