Print Page | Close Window

CXTPControl SetCustomIcon and icon copy

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=3532
Printed Date: 09 November 2025 at 11:40pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: CXTPControl SetCustomIcon and icon copy
Posted By: fishy
Subject: CXTPControl SetCustomIcon and icon copy
Date 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



Replies:
Posted By: Oleg
Date 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


Posted By: fishy
Date 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


Posted By: fishy
Date 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

 

 



Posted By: Oleg
Date 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


Posted By: fishy
Date Posted: 08 February 2006 at 2:09pm
Perfect.  That was the key.  Thank you very much Oleg!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net