CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Posts
    218

    Arrow 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

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    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.

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: CStatic::SetBitmap() and it's size

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

  5. #5
    Join Date
    Feb 2005
    Posts
    218

    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
  •  





Click Here to Expand Forum to Full Width

Featured