Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Command Bars
  New Posts New Posts RSS Feed - How to change button image?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

How to change button image?

 Post Reply Post Reply
Author
Message
brianh View Drop Down
Groupie
Groupie


Joined: 30 April 2004
Location: United Kingdom
Status: Offline
Points: 83
Post Options Post Options   Thanks (0) Thanks(0)   Quote brianh Quote  Post ReplyReply Direct Link To This Post Topic: How to change button image?
    Posted: 02 July 2004 at 10:00am

I have a command bar button that can be toggled on/off and I want to change the button image depending on whether it's checked or not.

Does anyone know the best way to do this?

Thanks.

Back to Top
brianh View Drop Down
Groupie
Groupie


Joined: 30 April 2004
Location: United Kingdom
Status: Offline
Points: 83
Post Options Post Options   Thanks (0) Thanks(0)   Quote brianh Quote  Post ReplyReply Direct Link To This Post Posted: 02 July 2004 at 10:24am

I have managed to solve the problem by using the following update handler for the button:

HICON m_hIconSingleProd ;
HICON m_hIconMultipleProd ;
bool m_bSingleProduct ;

...

void CMyClass::OnUpdateSingleProduct(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_bSingleProduct ? 1 : 0) ;

 CXTPControl* pCtl = CXTPControl::FromUI( pCmdUI ) ;
 if ( pCtl )
 {
  CXTPImageManagerIcon* pImgIcon = pCtl->GetParent()->GetImageManager()->GetImage( ID_SINGLE_PRODUCT ) ;
  ASSERT( pImgIcon ) ;
  if( m_bSingleProduct )
  {
   pImgIcon->SetIcon( m_hIconSingleProd ) ;
  }
  else
  {
   pImgIcon->SetIcon( m_hIconMultipleProd ) ;
  }
 }
}

Although this works it doesn't seem very efficient, is there a better way?

Also the image for this command in the menu is now offset upwards from where it should be, any ideas why?

Back to Top
brianh View Drop Down
Groupie
Groupie


Joined: 30 April 2004
Location: United Kingdom
Status: Offline
Points: 83
Post Options Post Options   Thanks (0) Thanks(0)   Quote brianh Quote  Post ReplyReply Direct Link To This Post Posted: 02 July 2004 at 11:30am
I know why the icon in the menu was offset, I was using LoadIcon() to load it and this only loads icons of the default system size so it was enlarging the icon thus causing the alignment problem.  I changed this to use LoadImage() and the icon in the menu is now correctly positioned - what's more it changes with the toolbar icon. :)
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 05 July 2004 at 3:47am

Easer way to change IconId of control:

pCtl->SetIconId(m_bSingleProduct? ID_SINGLE_PRODUCT: ID_MULTI_PRODUCT)

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
brianh View Drop Down
Groupie
Groupie


Joined: 30 April 2004
Location: United Kingdom
Status: Offline
Points: 83
Post Options Post Options   Thanks (0) Thanks(0)   Quote brianh Quote  Post ReplyReply Direct Link To This Post Posted: 05 July 2004 at 4:39am

I had seen this function but I couldn't see how to add the icons and get their ids in the first place.  Can you explain how this is done?

Thanks

Back to Top
clintsinger View Drop Down
Newbie
Newbie


Joined: 15 May 2003
Location: Canada
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote clintsinger Quote  Post ReplyReply Direct Link To This Post Posted: 27 January 2005 at 5:37pm

I too would like to see how an icon is accessed.  I have tried to add icon resources and then point the SetIconId to the corresponding resource id but all that happens is that the area on toolbar where the icon should appear is blank

I am doing the call from an OnUpdate(CCmdUI* pCmdUI) method and calling CXTPControl* pCtl = CXTPControl::FromUI( pCmdUI ) ; to get the control.

Anyone have any ideas?

Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 31 January 2005 at 5:55am
List all your code related this problem.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
bluesaphire View Drop Down
Newbie
Newbie
Avatar

Joined: 07 August 2006
Location: India
Status: Offline
Points: 14
Post Options Post Options   Thanks (0) Thanks(0)   Quote bluesaphire Quote  Post ReplyReply Direct Link To This Post Posted: 10 August 2006 at 6:37am

 Try this

depending upon the flag checked or unchecked call the setcustomicon function accordingly
if( m_bSingleProduct )
  {
   pControl->SetIconId(ID_SINGLE_PRODUCT);
HICON hIcon;

 hIcon = AfxGetApp()->LoadIcon(ID_SINGLE_PRODUCT);

  pControl->SetCustomIcon(hIcon);
}
else
{
  pControl->SetIconId(ID_MULTIPLE_PRODUCT);
HICON hIcon;

 hIcon = AfxGetApp()->LoadIcon(ID_MULTIPLE_PRODUCT);

  pControl->SetCustomIcon(hIcon);
 
}

I guess this is what you looking for?

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.203 seconds.