-
August 24th, 2005, 09:36 AM
#1
CStatic::SetBitmap() and it's size
Hi,
Msdn says for SetBitmap(): "The bitmap will be automatically drawn in the static control. By default, it will be drawn in the upper-left corner and the static control will be RESIZED TO THE SIZE OF THE BITMAP."
So in the following:
Code:
CRect r1,r2,r3;
GetWindowRect(&r1);//the width of the CStatic as I want it
SetBitmap(bitmap);
GetWindowRect(&r2);//r2 has the width and height of the bitmap
This code is executed in my CPicture class, which is a CStatic. This CPicture instance is a control on a dialog. What I want is that the dimensions (only width and height) are set again to the ones of r1 after doing SetBitmap()! The coordinate of the left upper corner (the position of the CStatic control on the dialog) must remain the same!
How can I do this? How can I put the size of the static back to the original size (which is r1)?
I already tried to do ScreenToClient(r1); MoveWindow(r1) after the SetBitmap() call; but then the CStatic position is at (0,0) in dialog coordinates. So in the left upper corner of the dialog; that is not where I want my CStatic to be.
Probably an easy solution exists.
Greets
-
August 24th, 2005, 10:45 AM
#2
Re: CStatic::SetBitmap() and it's size
If the style of your static control is SS_BITMAP, then the size of the control will be the same as the size of your bitmap.
If you want to do the reverse, i.e. having the bitmap resized to the size of the control, then, you first need to stretch you bitmap to the size of the static control, before applying SetBitmap().
In order to stretch a bitmap, I think, you first have to attach it to a DC, if it is not already attached, and then use StrectchBlt().
If the style of your static control is SS_CENTERIMAGE, then the control will not be resized, and the image will be centered in the control.
-
August 24th, 2005, 11:17 AM
#3
Re: CStatic::SetBitmap() and it's size
did you try ::SetWindowPos(..) on the Static control?
but even if it will work it will hide some part of the image! if you want to see all the image in the smaller control, you need to use strecth functions like mentioned above.
Cheers
-
August 24th, 2005, 01:45 PM
#4
Re: CStatic::SetBitmap() and it's size
Originally Posted by timv
What I want is that the dimensions (only width and height) are set again to the ones of r1 after doing SetBitmap()! The coordinate of the left upper corner (the position of the CStatic control on the dialog) must remain the same!
It does. If you do not set SS_CENTERIMAGE (SS_REALSIZEIMAGE is irrelevant), static control will resize itself to fit bitmap size.
To prevent that you do not need class derived from CStatis, unless you want to reuse this behavior in other project.
In a dialog class:
Code:
CRect rectClient;
m_ctrlStatic.GetWindowRect(rectClient);
m_ctrlStatic.SetBitmap(m_bmp);
ScreenToClient(rectClient);
m_ctrlStatic.MoveWindow(rectClient);
If you want to have class to reuse this behavior, you will have to handle STM_SETIMAGE message since SetImage is not virtual. You do not have to check wParam, nor lParam unless you want different behavior for loading icons:
Code:
//this macro goes between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP in the cpp
ON_MESSAGE(STM_SETIMAGE, OnSetImage)
//this declaration goes in the h
afx_msg LRESULT OnSetImage(WPARAM wParam, LPARAM lParam);
//this is definition that does the job
LRESULT CConstSizeBmp::OnSetImage(WPARAM wParam, LPARAM lParam)
{
CRect rectClient;
GetWindowRect(rectClient);
Default();
GetParent()->ScreenToClient(rectClient);
MoveWindow(rectClient);
return 0;
}
This will crop a bitmap if larger or cover only a part of static control.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
August 25th, 2005, 02:35 AM
#5
Re: CStatic::SetBitmap() and it's size
If the style of your static control is SS_CENTERIMAGE, then the control will not be resized, and the image will be centered in the control.
Thanks guys, I was unaware of this. It works great now.
timv
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|