Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - CXTPControl SetCustomIcon and icon copy
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CXTPControl SetCustomIcon and icon copy

 Post Reply Post Reply
Author
Message
fishy View Drop Down
Newbie
Newbie
Avatar

Joined: 26 October 2005
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote fishy Quote  Post ReplyReply Direct Link To This Post Topic: CXTPControl SetCustomIcon and icon copy
    Posted: 18 January 2006 at 5:11pm
Hello,
If i use CXTPControl::SetCustomIcon (in place of SetIconId in an example similar to DynamicPopups -- see CControlShell) it appears that the HICON is being copied (m_hIcon.CopyHandle(..) in CXTPImageManagerIcon::SetIcon(CXTPImageManagerIconHandle hIcon)
 to be exact).  However, I do not see how this copied handle is being destroyed.  Whenever my submenu that uses SetCustomIcon is displayed, the icons are copied and not deleted - resulting in a handle leak.  I am sure I am missing something here but what is the correct way to use SetCustomIcon?

- fish
Back to Top
Oleg View Drop Down
Senior Member
Senior Member


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: 19 January 2006 at 12:00am

yes, it is copied. it will be destroyed automatically in Clear method:

void CXTPImageManagerIconHandle::Clear()
{
 if (m_bClearHandles)
 {
  if (m_hIcon)
  {
   DestroyIcon(m_hIcon);
  }
...

}
 

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
fishy View Drop Down
Newbie
Newbie
Avatar

Joined: 26 October 2005
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote fishy Quote  Post ReplyReply Direct Link To This Post Posted: 19 January 2006 at 9:11am
Again, I think I am missing something here but look at the code for CXTPControl::SetCustomIcon(HICON hIcon).  It calls AddCustomIcon(hIcon) on the CXTPImageManager (which copies the icon) and assigns the returned icon id to the m_nCustomIconId variable.  Now, when the CXTPControl is deleted, the m_nCustomIconId is never removed from the ImageManager, and thus never cleared.

How would I go about deleting the icon that is allocated from SetCustomIcon call in CXTPControl?  Should I manually delete it by calling clear on the ImageManager? 

Let me give you my situation to help you understand the problem.  I am trying to make a dynamic menu that is populated with the names and icons of all the currently running applications.  I have accomplished this (minus the icons) by creating my own CXTPControlButton in OnCreateControl.  In the CXTPControlButton's OnCalcDynamicSize I create a CXTPControlButton for each running application.  This works great.  The problem is that when I assign an icon to the CXTPControlButton, the icon is copied and displayed (this also works) but is never deleted.  The next time the menu is displayed the icon is copied again, and not deleted again, etc.. etc...  leaking icon handles every time you click on the menu.   I'm sure there is a way to do this correctly. 

- fishy
Back to Top
fishy View Drop Down
Newbie
Newbie
Avatar

Joined: 26 October 2005
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote fishy Quote  Post ReplyReply Direct Link To This Post Posted: 23 January 2006 at 9:41am

Any ideas here?  I would like a good solution for this.

I have a temporary solution for this problem but there is still a small issue with my solution.  My solution is to derive a class from the CXTPControlButton and remove the custom icon in its destructor.  I have the following code in the destructor of the derived class:

 // Cleanup the custom icon if it exists
 CXTPImageManager* pImageManager = GetImageManager();
 if (pImageManager != NULL && m_nCustomIconId != 0)
 {
    pImageManager->RemoveIcon(m_nCustomIconId);
 }

Now, when the menu is displayed the custom icons are assigned and copied (internally in CXTPImageManagerIcon::SetIcon).  The next time the menu is clicked I remove all all the CXTPControlButtons in the menu (which deletes them and causes my destructor to be called, deleting the custom icons).  However, this solution keeps the icons allocated between calls to OnCalcDynamicSize.  If, for example, the user clicks the menu only once, then the icons won't be deleted until the window is closed.  In fact, the icons created by the last click of the menu won't be deleted until the window is closed.  (Because OnCalcDynamicSize isn't called again).  So, is there a way to detect when the menu is closing so that I can delete the CXTPControlButtons there and not in the OnCalcDynamicSize call?  I believe that would solve this issue.

..Waiting for a response..

- fishy

 

 

Back to Top
Oleg View Drop Down
Senior Member
Senior Member


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: 24 January 2006 at 1:46pm

ok, I see problem, right Icons collected in array and destroyed in application exit.

you have right idea about destructor things,

but you can delete all controls when popup closed:

 BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
     //{{AFX_MSG_MAP(CMainFrame)
     ON_XTP_UNINITCOMMANDSPOPUP()
     //}}AFX_MSG_MAP
 END_MESSAGE_MAP()
 


void CMainFrame::OnUninitCommandsPopup(CXTPPopupBar* pPopupBar)
{
     // get the list of commands for the popup.
    CXTPControls* pControls = pPopupBar->GetControls();
 }

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
fishy View Drop Down
Newbie
Newbie
Avatar

Joined: 26 October 2005
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote fishy Quote  Post ReplyReply Direct Link To This Post Posted: 08 February 2006 at 2:09pm
Perfect.  That was the key.  Thank you very much Oleg!
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.063 seconds.