Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32
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
LoadBitmap( ... )
function.
 
I have also created button with
BS_BITMAP
style.
 
The bitmap is loaded into button via
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 12-May-13 8:14am
Comments
Sergey Alexandrovich Kryukov at 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
Nenad Smiljkovic at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Nenad Smiljkovic at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Nenad Smiljkovic at 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 at 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
Nenad Smiljkovic at 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 at 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
Nenad Smiljkovic at 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.
Sergey Alexandrovich Kryukov at 13-May-13 16:25pm
   
Sorry if that was I who scared you. With such bitmap size, it's the best not to resample it. You get the best quality if you draw the best image for this resolution from scratch.
After all, what's wrong with it? It's always better to get the right image in first place.
—SA
Nenad Smiljkovic at 13-May-13 17:09pm
   
Don't be sorry, it wasn't you who scared me, it is the fact that somehow I expected this to end with owner draw.
I guess you are right... it has to be owner draw...
Well, I have to learn it someday.. Why not know?
Thank you!
Sergey Alexandrovich Kryukov at 13-May-13 17:11pm
   
Sure. Are you accepting the answer formally (green button)?
—SA
Nenad Smiljkovic at 13-May-13 17:42pm
   
I have clicked on " accept solution " button.
Since code example was provided, I have also accepted solution below.
I have assumed I can accept both solutions.
You will both get the points, right?
Sergey Alexandrovich Kryukov at 13-May-13 17:44pm
   
Sure you can. The page is not closed by that, anyone can add a new solution.
Thank you,
—SA
Nenad Smiljkovic at 13-May-13 17:45pm
   
Thank YOU :)
Sergey Alexandrovich Kryukov at 13-May-13 18:23pm
   
You are welcome.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 14 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100