Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello everyone!

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.

I have a bitmap that I load from resource file via
C++
LoadBitmap( ... )
function.

I have also created button with
C++
BS_BITMAP
style.

The bitmap is loaded into button via
C++
SendMessage( ..., BM_SETIMAGE , (WPARAM)IMAGE_BITMAP, ... );
but bitmap's size doesn't correspond to buttons client area.

What should I do to size it properly?

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using WIN32 API.
If any other information is required ( source code or something similar ), please ask for it, I will more than gladly supply it.
Posted
Comments
Sergey Alexandrovich Kryukov 12-May-13 13:23pm    
Why wouldn't you make your resource bitmap of a required size in first place? For small images, like those of buttons, resizing (resample) of images even down to a smaller size is a bad thing, giving low quality results. Don't do it.
—SA
MyOldAccount 12-May-13 14:41pm    
But what will happen when I resize button?
Will it stretch bitmap accordingly?
Or will I have to do some owner draw stuff?
If resizing will automaticly stretch bitmap, then your solution is the best in my oppinion, but I just want to know if I need to do owner draw or something similar if I resize button ( my client may ask of me to resize button in response to WM_SIZE message).
Anyway, thank you for response!

Bite the bullet and use owner draw buttons. You'll enjoy them. If you don't want to write one from scratch then know that in my project Visualizing the Mandelbrot set[^] are two files ImageButton.cpp and ImageButton.h which implement various kinds of owner draw buttons with support for classic or aero theme. They rely on two other files BitmapIcon.cpp and BitmapIcon.h. One of the types lets you place a bitmap into the button. Another supporting class converts a bitmap into an icon so that the background color can be transparent and make the bitmap look much better on the button. The class is called CImageButton. Look for objects of this type in the code to see how they are used.

Hope this helps
 
Share this answer
 
Comments
MyOldAccount 13-May-13 17:13pm    
I guess I will bite the bullet!
Thank you for advice.
I have downloaded your project ( it is amazing! ) and will start to study it.
Thank you so much!
Use smaller bitmap size in first place or bigger buttons. Don't resize (resample) any bitmaps of small size, as the qualify of this operation can be very poor. Just think about it.

—SA
 
Share this answer
 
Comments
MyOldAccount 12-May-13 16:17pm    
I have tried it, and it works, but I have small side effect:

After I click on it, infrequent flicker appears in view of few thin horizontal lines colored with the background color of a button.

Should I handle WM_CLCCOLORBTN, and if that is the case, how?

If I would post code, would that be helpful to you?
Sergey Alexandrovich Kryukov 12-May-13 22:02pm    
Hard to say. Usually, if the image is being changed during run time, the problem is solved with double buffering. If the control shows, say, empty background first, and then the image on top of it, it is perceived as a flicker; and with double buffering, it looks like one whole image changes another one...
—SA
MyOldAccount 13-May-13 13:49pm    
Perhaps this set of information could shed some light:

- Bitmaps were made in Paint, and are large, in my oppinion;

- Button's dimensions are large;

- Button is created in WM_CREATE via CreateWindowEx( .. );

- Button has BS_BITMAP style;

- Bitmap is set in WM_CREATE just after button is created ( with BM_SETIMAGE message );

- I haven't handled WM_CTLCOLORBTN message;

Hope that this will help you to provide some useful advice since I am not that experienced, and just can't figure this out.

As for double buffering part, should I try using it to paint buttons in response to WM_PAINT or maybe is the best idea to owner draw button?
Sergey Alexandrovich Kryukov 13-May-13 15:27pm    
You are trying to be not precise, by some reason? Who says like "are large"? What's the image sized, original and required, in pixels?..
—SA
MyOldAccount 13-May-13 16:19pm    
I do not try to be imprecise, I am just inexperienced, and to be honest, a bit scared, so I guess I have started to panic a little.
This is my first time that somebody asked me to do something like this, and I just do not know how to handle it.

Bitmap's size is 152 x 152, and I have passed the same values in CreateWindowEx() for length and width, so button's dimensions can be the same as bitmap's.
The image displays properly but there is some flicker involved, in view that horizontal stripes appear from time to time, after the button is clicked.
Again, I am sorry, I didn't intend to make confusion.
make a member variable for button let say

in .h file
CButton m_testBitmap;

CBitmap m_bitTest;

in .cpp(OnInitDialog)

m_bitTest.LoadBitmap(IDB_Voice);
HBITMAP hBitmap= (HBITMAP) m_bitTest.GetSafeHandle();
m_testBitmap.SetBitmap(hBitmap);// resize buton for bitmap
BITMAP bmDialer;
m_bitTest.GetBitmap(&bmDialer);
int widthDialer = bmDialer.bmWidth;
int heightDialer = bmDialer.bmHeight;
CRect rDialer;
m_Dialer.GetWindowRect(&rDialer); /* r comes out in screen coordinates */
ScreenToClient(&rDialer); // MoveWindow needs coordinates in parent window
rDialer.right = rDialer.left + widthDialer;
rDialer.bottom = rDialer.top + heightDialer;
m_testBitmap.MoveWindow(&rDialer);


for any Queries post a message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900