Board index » Visual Studio » Bitmap and Picture Control ???

Bitmap and Picture Control ???

Visual Studio94
I'm trying to write a simple example program to learn more about using

bitmaps and picture controls. It is a dialog-based program with only two

buttons and one picture control. One button is to display a bitmap, and the

other is to erase the bitmap.

The bitmap is displayed correctly and does erase with the other button,

but I am not able to click on the display button again without an Assertion

Error. Although I have found that if I declare the bitmap locally in the

display button handler and put another bitmap declaration in the erase

button handler, it is possible to click the display button after the erase

button and the bitmap is redisplayed. However when declared locally, the

bitmap is lost if the dialog is covered and then does not repaint the

bitmap. It does repaint when the bitmap is declared in the class

declaration.

So, I would prefer to declare the bitmap in the class declaration, but

what is required to be able to click on the display button again. Code is

below:



void CBitmapTestDlg::OnDisplay()

{

// CBitmap bitmap1; ***remove this***

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

// CBitmap bitmap1; ***remove this***

DeleteObject(bitmap1);



Invalidate();

UpdateWindow();

}


-
 

Re:Bitmap and Picture Control ???

Try this



Quote


void CBitmapTestDlg::OnDisplay()

{

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

m_bmp1.SetBitmap(NULL);

bitmap1.DeleteObject();



Invalidate();

UpdateWindow();

}





"cdg" <anyone@anywhere.com>wrote in message

Quote
I'm trying to write a simple example program to learn more about using

bitmaps and picture controls. It is a dialog-based program with only two

buttons and one picture control. One button is to display a bitmap, and

the

other is to erase the bitmap.

The bitmap is displayed correctly and does erase with the other button,

but I am not able to click on the display button again without an

Assertion

Error. Although I have found that if I declare the bitmap locally in the

display button handler and put another bitmap declaration in the erase

button handler, it is possible to click the display button after the erase

button and the bitmap is redisplayed. However when declared locally, the

bitmap is lost if the dialog is covered and then does not repaint the

bitmap. It does repaint when the bitmap is declared in the class

declaration.

So, I would prefer to declare the bitmap in the class declaration, but

what is required to be able to click on the display button again. Code is

below:



void CBitmapTestDlg::OnDisplay()

{

// CBitmap bitmap1; ***remove this***

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

// CBitmap bitmap1; ***remove this***

DeleteObject(bitmap1);



Invalidate();

UpdateWindow();

}













-

Re:Bitmap and Picture Control ???

A CBitmap is a "wrapper class" around an HBITMAP handle. Once the association has been

made, you have to break it to allow the CBitmap to be re-associated with a different

bitmap. Your assertion failure is probably because there is still an association. When

reporting an assertion error, it is essential to tell us what file/line it occurred in and

what version VS you are using.



Because the class is a wrapper class, its destructors will delete the associated object,

so when the OnPaint handler is executed, the bitmap is lost. You can break the

association explicitly by using Detach().



See the essays on my MVP Tips site on using bitmaps, and on Attach/Detach.

joe

On Tue, 24 Oct 2006 20:52:23 GMT, "cdg" <anyone@anywhere.com>wrote:



Quote
I'm trying to write a simple example program to learn more about using

bitmaps and picture controls. It is a dialog-based program with only two

buttons and one picture control. One button is to display a bitmap, and the

other is to erase the bitmap.

The bitmap is displayed correctly and does erase with the other button,

but I am not able to click on the display button again without an Assertion

Error. Although I have found that if I declare the bitmap locally in the

display button handler and put another bitmap declaration in the erase

button handler, it is possible to click the display button after the erase

button and the bitmap is redisplayed. However when declared locally, the

bitmap is lost if the dialog is covered and then does not repaint the

bitmap. It does repaint when the bitmap is declared in the class

declaration.

So, I would prefer to declare the bitmap in the class declaration, but

what is required to be able to click on the display button again. Code is

below:



void CBitmapTestDlg::OnDisplay()

{

// CBitmap bitmap1; ***remove this***

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

// CBitmap bitmap1; ***remove this***

DeleteObject(bitmap1);



Invalidate();

UpdateWindow();

}







Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:Bitmap and Picture Control ???

I tried this and it worked perfectly.



void CBitmapTestDlg::OnErase()

{

DeleteObject(bitmap1);

bitmap1.Detach();



Invalidate();

UpdateWindow();

}





-

Re:Bitmap and Picture Control ???

CStatic::SetBitmap doesn't take ownership of the HBitmap. If you do



CBitmap Bmp;

Bmp.LoadBitmap(IDB_BITMAP1);

m_Static.SetBitmap((HBITMAP)Bmp.Detach());



you are going to end up with a resource leak, because CStatic doesn't delete

the object.



AliR.



"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

Quote
A CBitmap is a "wrapper class" around an HBITMAP handle. Once the

association has been

made, you have to break it to allow the CBitmap to be re-associated with a

different

bitmap. Your assertion failure is probably because there is still an

association. When

reporting an assertion error, it is essential to tell us what file/line it

occurred in and

what version VS you are using.



Because the class is a wrapper class, its destructors will delete the

associated object,

so when the OnPaint handler is executed, the bitmap is lost. You can

break the

association explicitly by using Detach().



See the essays on my MVP Tips site on using bitmaps, and on Attach/Detach.

joe

On Tue, 24 Oct 2006 20:52:23 GMT, "cdg" <anyone@anywhere.com>wrote:



>I'm trying to write a simple example program to learn more about using

>bitmaps and picture controls. It is a dialog-based program with only two

>buttons and one picture control. One button is to display a bitmap, and

the

>other is to erase the bitmap.

>The bitmap is displayed correctly and does erase with the other

button,

>but I am not able to click on the display button again without an

Assertion

>Error. Although I have found that if I declare the bitmap locally in the

>display button handler and put another bitmap declaration in the erase

>button handler, it is possible to click the display button after the

erase

>button and the bitmap is redisplayed. However when declared locally, the

>bitmap is lost if the dialog is covered and then does not repaint the

>bitmap. It does repaint when the bitmap is declared in the class

>declaration.

>So, I would prefer to declare the bitmap in the class declaration, but

>what is required to be able to click on the display button again. Code is

>below:

>

>void CBitmapTestDlg::OnDisplay()

>{

>// CBitmap bitmap1; ***remove this***

>bitmap1.LoadBitmap(IDB_BITMAP1);

>m_bmp1.SetBitmap(bitmap1);

>}

>

>void CBitmapTestDlg::OnErase()

>{

>// CBitmap bitmap1; ***remove this***

>DeleteObject(bitmap1);

>

>Invalidate();

>UpdateWindow();

>}

>

>

>

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm





-

Re:Bitmap and Picture Control ???

I don't mean to be stickler here but



Why are using,



DeleteObject(bitmap1);



instead of



bitmap1.DeleteObject();



AliR.



"cdg" <anyone@anywhere.com>wrote in message

Quote
I tried this and it worked perfectly.



void CBitmapTestDlg::OnErase()

{

DeleteObject(bitmap1);

bitmap1.Detach();



Invalidate();

UpdateWindow();

}









-

Re:Bitmap and Picture Control ???

by the way, calling Detach after DeleteObject does not do anything.



And if you want to do this without repainting the entire window do it like

this



void CBitmapTestDlg::OnDisplay()

{

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

m_bmp1.SetBitmap(NULL);

bitmap1.DeleteObject();

}





AliR.



"cdg" <anyone@anywhere.com>wrote in message

Quote
I tried this and it worked perfectly.



void CBitmapTestDlg::OnErase()

{

DeleteObject(bitmap1);

bitmap1.Detach();



Invalidate();

UpdateWindow();

}









-

Re:Bitmap and Picture Control ???

I just read your post, but if that is the correct way I'll write it with

the dot operator.





-

Re:Bitmap and Picture Control ???

You are right. I tried it and it worked perfectly. And it is a probably a

better approach for this.





"AliR (VC++ MVP)" <AliR@online.nospam>wrote in message

Quote
by the way, calling Detach after DeleteObject does not do anything.



And if you want to do this without repainting the entire window do it like

this



void CBitmapTestDlg::OnDisplay()

{

bitmap1.LoadBitmap(IDB_BITMAP1);

m_bmp1.SetBitmap(bitmap1);

}



void CBitmapTestDlg::OnErase()

{

m_bmp1.SetBitmap(NULL);

bitmap1.DeleteObject();

}





AliR.



"cdg" <anyone@anywhere.com>wrote in message

news:p7x%g.346644$QM6.318184@bgtnsc05-news.ops.worldnet.att.net...

>I tried this and it worked perfectly.

>

>void CBitmapTestDlg::OnErase()

>{

>DeleteObject(bitmap1);

>bitmap1.Detach();

>

>Invalidate();

>UpdateWindow();

>}

>

>









-

Re:Bitmap and Picture Control ???

If you are using a CGdiObject derived class like CBitmap, then

CGdiObject::DeleteObject() would be perfered.



AliR.



"cdg" <anyone@anywhere.com>wrote in message

Quote
I just read your post, but if that is the correct way I'll write it

with

the dot operator.









-