Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - 9.80:Strange behaviour CXTButton::SetIcon
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

9.80:Strange behaviour CXTButton::SetIcon

 Post Reply Post Reply
Author
Message
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Topic: 9.80:Strange behaviour CXTButton::SetIcon
    Posted: 17 October 2005 at 8:08am

In version 9.80, the implementation of CXTButton::SetIcon has changed.

It is internally routed to BOOL CXTPImageManagerIconHandle::CreateIconFromResource(LPCTSTR lpszResourceName, CSize szIcon) which internally has the following code:

...
01: HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_GROUP_ICON);
02: HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_GROUP_ICON);
03:
04: if (hRsrc != NULL)
05: {
06:  // Load and Lock to get a pointer to a GRPICONDIR
07:  HGLOBAL hGlobal = LoadResource( hInst, hRsrc );
08:
09:  CXTPImageManagerIcon::GRPICONDIR* lpGrpIconDir = (CXTPImageManagerIcon::GRPICONDIR*)LockResource( hGlobal );
10:
11:  for (int i = (int)lpGrpIconDir->idCount - 1; i >= 0; i--)
12:  {
13:   if (lpGrpIconDir->idEntries[ i ].bWidth == szIcon.cx && lpGrpIconDir->idEntries[i].bHeight == szIcon.cy)
14:   {
15:    if (CreateIconFromResource(MAKEINTRESOURCE(lpGrpIconDir->idE ntries[i].nID), szIcon))
16:     return TRUE;
17:   }
18:  }
19:
20:  return FALSE;
21: }
22:
23: hInst = AfxFindResourceHandle(lpszResourceName, RT_ICON);
24: hRsrc = ::FindResource(hInst, lpszResourceName, RT_ICON);
25:
26: if (hRsrc == NULL)
27:  return FALSE;
...

Now I call the CXTButton::SetIcon  from within my code and the result is that the main application icon is loaded instead of the icon that is present and even found by the lines above.

Let me explain:

  • Line 01 and 02 do return a non-NULL value.
  • Then the code is executed until line 15, calling itself recusively  with a value of 1 for lpGrpIconDir->idEntries[i].nID.
  • Then the recursive call of the function with a value of 1 for the lpszResourceName evalutes hRsrc to NULL and thus does not branch into the block starting on line 04 (because a resource ID of 6254 was requested and now the strange recursion passes a resource ID of 1 which actually does not exist).
  • Instead, it goes directly to line 23 where the ID of 1 is passed and (probably by Windows default) loads the applications icon.

Question: could you please comment on this? I do believe this is an error, because in the version prior to 9.7 everything works correctly.

Thanks
Uwe

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: 17 October 2005 at 11:42pm

Hello,

Thank you, we agree that the problem exists,

To fix you can

1. Change resource of IDR_MAINFRAME (Main Icon) from 1 to 128

or

patch sources

1. Add third parameter to CreateIconFromResource

bGroupResource = TRUE

and

change line 04:

if (hRsrc != NULL && bGroupResource)

change line 15:

    if (CreateIconFromResource(MAKEINTRESOURCE(lpGrpIconDir->idE ntries.nID), szIcon, FALSE))

 

Thank you,

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2005 at 12:36am

Thank you, I will try the second approach.

BTW: My IDR_MAINFRAME is not 1 but 150. Probably returning the  IDR_MAINFRAME (i.e. the lowest icon) when requesting with ID 1 is a Windows default behaviour.

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: 18 October 2005 at 3:42am
Ok, Please give me know if it helps.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2005 at 4:06am

Just tried, still did not work.

The reason is that it still passes the "1" from lpGrpIconDir->idEntries[i].nID to the first recursion of the function (instead of my real resource ID).

I do not fully understand what the function does, so I'll try it now and see how I can fix it.

Maybe you can tell me another option, too, if I find no solution by myself.

Thanks
Uwe

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: 18 October 2005 at 4:47am

Another option is to call

BOOL CXTButton::SetIcon(CSize size, HICON hIcon, HICON hIconHot/*= NULL*/, BOOL bRedraw/*= TRUE*/)

instead of

BOOL CXTButton::SetIcon(CSize size, UINT nID, UINT nHotID/*= 0*/, BOOL bRedraw/*= TRUE*/)

 

so you can load Icon useing AfxGetApp()->LoadIcon(idr) and call first method.

 

but, fix must work :( Can you attach icon that have problem?

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2005 at 8:11am
Back to Top
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2005 at 8:33am

I think I found another issue:

I now do call SetIcon() as you suggested. Now the icon IS loaded correctly, but since the Icon file contains a 32x32 and a 16x16, I do pass CSize(16,16), but internally you overwrite it again.

Namely here:

01: m_pIcon = new CXTPImageManagerIcon(0, size.cx, size.cy);
02: m_pIcon->SetIcon(hIcon);

Line 01 correctly passes my CSize(16,16), but when entering line 02 the function called there looks like the following:

01: BOOL CXTPImageManagerIcon::SetIcon(CXTPImageManagerIconHandle hIcon)
02: {
03:  ASSERT(!hIcon.IsEmpty());
04:  Clear(TRUE);
05:  m_hIcon.CopyHandle(hIcon);
06:  CSize sz = GetExtent();
07:
08:  m_nHeight = sz.cy;
09:  m_nWidth = sz.cx;
10:
11:  m_bScaled = FALSE;
12:
13:  if (m_pIconSet)
14:   m_pIconSet->RefreshAll();
15:
16:  return TRUE;
17: }

The lines 08 and 09 overwrite it again with CSize (32,32). I think this is an error.

Am I right here?

Thanks
Uwe

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: 18 October 2005 at 12:50pm

 

The problem now that LoadIcon loads 32*32 icon instead of 16*16 so CXTPImageManagerIcon::SetIcon works as expected.

To load corrrect Icon you can remove 32*32 layout from ico file, or use LoadImage instead:

HICON hIcon = (HICON)LoadImage(..,..,IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Uwe Keim View Drop Down
Newbie
Newbie


Joined: 01 November 2004
Location: Germany
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote Uwe Keim Quote  Post ReplyReply Direct Link To This Post Posted: 20 October 2005 at 11:26pm

Arrgh! .

Now I removed the 32x32 leaving only the 16x16 bitmap in the icon file. The result? Now the 16x16 icon is resized to be 32x32 resulting in a rasterized image, drawn at the wrong position (because too wide and too high).

Hmmmmmmm...

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.172 seconds.