Dynamically Loading a Bitmap on CStatic
Hi,
I am having a property page ( Dialog resource) and it contains a Cstatic Instance. I need to load a bitmap on it dynamically and can anybody help me which of Override or Message should I handle to do it..
* The bitmap is in the Resource form IDB_ANYBMP
* After Loading the Bitmap into the CBitmap Instance and calling the CStatic::SetBitmap Function.
The above Algo is working once the dialog is displayed. But I need to display the bitmap before the dialog get displayed.
Thanks in Advance
[534 byte] By [
Sirama12] at [2007-11-20 1:28:21]

# 1 Re: Dynamically Loading a Bitmap on CStatic
Hello,
You can display the bitmap in the static object in WM_PAINT message handler of the dialog. For example, add the following code at the end of the OnPaint function of the dialog class and you will see the bitmap diaplayed when dialog is visible.
CBitmap BM;
BM.LoadBitmap(IDB_ANYBMP);
CDC MemDC;
CClientDC DC(GetDlgItem(IDC_ANYBMP));
if (MemDC.CreateCompatibleDC(&DC))
{
CBitmap* pBM = MemDC.SelectObject(&BM);
BITMAP bmObj;
BM.GetBitmap(&bmObj);
DC.BitBlt(0, 0, bmObj.bmWidth, bmObj.bmHeight, &MemDC, 0, 0, SRCCOPY);
MemDC.SelectObject(pBM);
MemDC.DeleteDC();
}
BM.DeleteObject();
I have chosen the ID of the static object as IDC_ANYBMP in this case.
Regards.
Pravin.
# 2 Re: Dynamically Loading a Bitmap on CStatic
...
* The bitmap is in the Resource form IDB_ANYBMP
* After Loading the Bitmap into the CBitmap Instance and calling the CStatic::SetBitmap Function.
The above Algo is working once the dialog is displayed. But I need to display the bitmap before the dialog get displayed.1. Could you explain the bold text? :confused:
No child control on the dialog cannot be displayed "before the dialog get displayed"...
2. The steps you described (your "Algo") seem to be OK. Don't forget to declare the CBitmap variable as a class member of the property page.
# 3 Re: Dynamically Loading a Bitmap on CStatic
1. Could you explain the bold text? :confused:
No child control on the dialog cannot be displayed "before the dialog get displayed"...
2. The steps you described (your "Algo") seem to be OK. Don't forget to declare the CBitmap variable as a class member of the property page.
I mean, I need to Load the Bitmap into the static control at runtime before the dialog is visible.
Algo is my spelling mistake.. Please read it as also :-)
# 4 Re: Dynamically Loading a Bitmap on CStatic
I mean, I need to Load the Bitmap into the static control at runtime before the dialog is visible.
Create CStatic control member variable (say, c_myStatic) for the static control displaying the bitmap.
Create CBitmap member variable (say, m_myBitmap).
In OnInitDialog message handler load the bitmap to m_myBitmap and set it to the _myStatic:CSomePropertyPage::OnInitDialog()
{
CPropertyPage::OnInitDialog();
if(m_myBitmap.LoadBitmap(IDB_ANYBMP))
{
// optional, if the style SS_BITMAP was not set in resource editor:
c_myStatic.ModifyStyle(0, SS_BITMAP);
// set bitmap
c_myStatic.SetBitmap((HBITMAP)m_myBitmap);
}
// some othe initializations
.......
}
Algo is my spelling mistake.. Please read it as also :-) :D
# 5 Re: Dynamically Loading a Bitmap on CStatic
Hi VictorN,
Thanks a Lot and it resolves my problem. Thanks to praveen for the reply and mmm.. Praveen! Since I am using Propertypage the OnPaint handler is not that much useful for me.
Victor,
I need to change bitmap display based on the mouse click on the propery page controls, but I can't able to reuse the c_myStatic variable to load different Image. Currently I used 3 different CBitmap instances at the class scope for changing the bitmap in the CStatic control.
Is it possible to reuse the same variable ( In our case c_myStatic) to load difefrent bitmaps at runtime.
Thanks again VictorN
# 6 Re: Dynamically Loading a Bitmap on CStatic
Hi VictorN,
....
Is it possible to reuse the same variable ( In our case c_myStatic) to load difefrent bitmaps at runtime.Yes, of course!
Just call c_myStatic.SetBitmap((HBITMAP)m_myBitmap); // or use other bitmap instances where and when you need it!