The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: allynm on November 15, 2009, 11:14:19 PM

Title: Adding icon to window
Post by: allynm on November 15, 2009, 11:14:19 PM
Hi everyone -

I've been trying to figure out how to add an icon to a window.  I have created an .ico file (icondemo.ico).  I have a .rc file containing a define for the (#define IDI_ICON 101 and a statement declaring the icon as IDI_ICON  ICON DISCARDABLE "c:\masm32\mymasm\icondemo.ico".  The masm source file contains the statement IDI_ECO equ 101.  And, finally, I have in the .code section the statement invoke LoadIcon, NULL, IDI_ICON.

The icon does not show up.  I haven't messed with icons in the past so I'm largely clueless.  Any suggestions?

Thank you,
Mark Allyn
Title: Re: Adding icon to window
Post by: jj2007 on November 15, 2009, 11:28:31 PM
invoke LoadIcon, hInst, IDI_APPLICATION will work, Mark.
Title: Re: Adding icon to window
Post by: hutch-- on November 15, 2009, 11:38:19 PM
Mark,

Just have a look at any of the GUI apps in masm32, they all can use the attached icon as the main window icon. JJ has shown how to use a pre-defined system icon but you can use your own easily. I personally use number IDs for icons but if you are going to use equates, make sure the equate in your source code matches the ID in the RC file.
Title: Re: Adding icon to window
Post by: jj2007 on November 15, 2009, 11:48:38 PM
Quote from: hutch-- on November 15, 2009, 11:38:19 PM
JJ has shown how to use a pre-defined system icon but you can use your own easily.

Hutch,

IDI_App does load the "own" icon, not a predefined system icon. All you need is this line as an rc file (for example):

32512 ICON "\\masm32\\RichMasm\\icons\\Smiley.ico"

Note that IDI_APPLICATION is precisely 32512.

In fact, RichMasm (http://www.masm32.com/board/index.php?topic=12460) generates that line with the option...
OPT_Icon   Smiley   ; see \Masm32\RichMasm\Icons

... using this sequence in the batch file (\masm32\RichMasm\RES\bldallRM.bat):
QuoteIF DEFINED oIcon (
   echo 32512 ICON "\\masm32\\RichMasm\\icons\\%oIcon%.ico" >MyIcon.rc
   if exist %oRes% (copy MyIcon.rc + %oRes% MyIcon.rc)
   set oRes=MyIcon.rc
)
Title: Re: Adding icon to window
Post by: hutch-- on November 16, 2009, 12:03:03 AM
 :bg

http://msdn.microsoft.com/en-us/library/ms648072(VS.85).aspx


IDI_APPLICATION
    Default application icon.


It is an interesting method you are using but you have overwridden a system default ID for no purpose. Using the system default means you don't have to include a resource to display an icon.

In the resource file, something like this.


500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "MYICON.ICO"


In source code,

    invoke LoadIcon,hInstance,500
    mov hIcon, eax


Use the icon handle in the WNDLASSEX structure and a CreateWindowEx() window will display the icon in the normal manner.
Title: Re: Adding icon to window
Post by: jj2007 on November 16, 2009, 12:16:01 AM
Quote from: hutch-- on November 16, 2009, 12:03:03 AM
It is an interesting method you are using but you have overwridden a system default ID for no purpose.

Well, I am using, not overwriting, the system default ID. But I will change the ID in case I find a second use for IDI_APPLICATION :bg

Quote
In the resource file, something like this.


500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "MYICON.ICO"


The options MOVEABLE PURE LOADONCALL DISCARDABLE are obsolete (http://www.masm32.com/board/index.php?topic=5960.0), you can omit them. In case you store your icons in a particular folder, see usage of double backslashs as shown above.
Title: Re: Adding icon to window
Post by: hutch-- on November 16, 2009, 12:44:22 AM
 :bg

> MOVEABLE PURE LOADONCALL DISCARDABLE

I know its obsolete but if you have resource editors that create them in that form and it works OK, I don't lose sleep over it.
Title: Re: Adding icon to window
Post by: allynm on November 16, 2009, 02:48:27 AM
Hi Hutch and JJ -

Thank you for your help on this.   Can you help me understand what the number is in the first field of your .rc file-- for instance, 500.  I looked in the masm32 script pulldown and saw similar syntax.  Is the "500" functionally the same as the "101" I was using?

Do I need a "equ 500" in my source file?

Regards,
Mark
Title: Re: Adding icon to window
Post by: jj2007 on November 16, 2009, 07:36:08 AM
Mark,
The number is the ID. You can use whatever you want (including 32512 ;-), but make sure the ID appears only once in your code. You may use MyIcon = 123 or MyIcon equ 456, but the number itself is what counts. And in the rc file, the MyIcon is not known, so there you definitely need the 123...
Title: Re: Adding icon to window
Post by: MichaelW on November 16, 2009, 08:09:22 AM
For a resource ID the high-order word must be zero, so the maximum value is 65535.
Title: Re: Adding icon to window
Post by: sinsi on November 16, 2009, 08:18:14 AM
Don't forget that it can be a string as well - one reason for the FFFF limit.
Title: Re: Adding icon to window
Post by: untio on November 22, 2009, 05:01:24 PM
Hi,
I only remember you that in the wndclassex structure there are two icon handles: one for the large one (I create it always of 32x32 pixels) and another one for the small one (that I create always of 16x16 pixels). I think that in the upper left corner of the window will appear the small one.

Forgive me if I am wrong.