The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Mark_201 on June 07, 2006, 12:35:20 AM

Title: display icon on dialog box
Post by: Mark_201 on June 07, 2006, 12:35:20 AM
I'm trying to display an icon (.ICO) in a dialog window.  I added an image in the dialog editor and added the .ico file to the properties of the image.  I can see the icon displayed in the editor but not after I compile and run my program.  Is there a setting in the image property that I am missing? Or do I need to add some code?





Title: Re: display icon on dialog box
Post by: Mark Jones on June 07, 2006, 01:55:15 AM
Hello Mark,

Are you using an IDE like RadASM or Qeditor perhaps? Please give us more information and some code if possible.
Title: Re: display icon on dialog box
Post by: Mark_201 on June 07, 2006, 04:28:19 AM
Quote from: Mark Jones on June 07, 2006, 01:55:15 AM
Hello Mark,

Are you using an IDE like RadASM or Qeditor perhaps? Please give us more information and some code if possible.

Sorry about that.  I should put that info im my sig.  I'm using RadASM.


I got it!   :dance:

Doing a search in the forums I came up with this.

.if uMsg==WM_INITDIALOG                          ; WM_INITDIALOG
invoke GetModuleHandle,NULL
invoke LoadIcon,eax,IDC_ICON2                ; icon ID
mov hAboutIcon,eax                           ; icon handle
invoke GetDlgItem,hWin,IDC_ABOUT_IMG      ; eax = handle of dialog static control(IDC_ABOUT_IMG)
;STM_SETIMAGE - associate static control with icon image
;IDC_ICON2    - id of icon in resource file
;hAboutIcon   - icon handle
invoke SendMessage,eax,STM_SETIMAGE,IDC_ICON2,hAboutIcon


do I have to DestroyIcon at any time?

I wasn't sure how to ad an icon to the resource file. So i added the following to the MAIN.RC:

2 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "icon1.ico"

phew

Title: Re: display icon on dialog box
Post by: hutch-- on June 07, 2006, 04:52:03 AM
Mark,

Loading the icon in the WM_INITDIALOG message is fine as that message is never called again in the life of the app. Just make sure that the value "IDC_ICON2" is available within the assembler code and make sure you build the resource file properly so it includes the icon.
Title: Re: display icon on dialog box
Post by: P1 on June 07, 2006, 02:02:24 PM
Mark,

Be sure to use the experience that can be found in the examples of MASM32.

Like, C:\Masm32\examples\exampl02\resdlg2

Regards,  P1  :8)
Title: Re: display icon on dialog box
Post by: Mark Jones on June 08, 2006, 11:57:54 PM
Quote from: Mark_201 on June 07, 2006, 04:28:19 AM
do I have to DestroyIcon at any time?

Yes, Windows does not automatically delete the icon resource from memory. Invoke DestroyIcon in the WM_CLOSE message handler. :U

EDIT: The above apparently is not totally true. According to Win32.hlp:

Quote
It is only necessary to call DestroyIcon for icons created with the CreateIconIndirect function.

However... the MemProof utility reports an icon as a memory-leak issue unless DestroyIcon is used. (MemProof is not 100% reliable so...)

Back to this conundrum again... Is there any way to test this officially? Calling DestroyIcon with a valid icon handle returns "success" so my initial assumptions appear correct, at least superficially... :wink
Title: Re: display icon on dialog box
Post by: P1 on June 09, 2006, 02:53:04 PM
Mark,

Have you worked through your icon issues yet?

The example I gave you, had a correct/working implementation of using a icon in a dialog.

Regards,  P1  :8)
Title: Re: display icon on dialog box
Post by: Mark_201 on June 12, 2006, 02:14:03 AM
Quote from: P1 on June 09, 2006, 02:53:04 PM
Mark,

Have you worked through your icon issues yet?

The example I gave you, had a correct/working implementation of using a icon in a dialog.

Regards,  P1  :8)

opps.  Yes thanks all.  I have that working now.   Using the Win32 API is much easier in asm then Visual C++.

Mark
Title: Re: display icon on dialog box
Post by: Mark_201 on June 12, 2006, 03:19:54 AM
Since I got it working in ASM I decided to get it working in Visual C++.  It wasn't working in VC++.
Found this in the help file:

STM_SETIMAGE Message

--------------------------------------------------------------------------------

An application sends an STM_SETIMAGE message to associate a new image with a static control.

Syntax


To send this message, call the SendMessage function as follows.

lResult = SendMessage(         // returns HANDLE in lResult   
   HWND) hWndControl,         // handle to destination control     
   (UINT) STM_SETIMAGE,      // message ID     
   (WPARAM) wParam,           // = (WPARAM) () wParam;   
   (LPARAM) lParam                // = (LPARAM) (HANDLE) lParam;
);
   
Parameters

wParam
Specifies the type of image to associate with the static control. This parameter can be one of the following values:

IMAGE_BITMAP
IMAGE_CURSOR
IMAGE_ENHMETAFILE
IMAGE_ICON              <-----------------------------------------------------This is what I needed.
lParam
Handle to the image to associate with the static control.
Return Value

The return value is a handle to the image previously associated with the static control, if any; otherwise, it is NULL.

Remarks

To associate an image with a static control, the control must have the proper style. The following table shows the style needed for each image type.

Image type Static control style
IMAGE_BITMAP SS_BITMAP
IMAGE_CURSOR SS_ICON
IMAGE_ENHMETAFILE SS_ENHMETAFILE
IMAGE_ICON SS_ICON                                  <----------------- Set this in dialog resource file


So I changed my code.


.if uMsg==WM_INITDIALOG                                    ; WM_INITDIALOG
      invoke LoadIcon,hInstance,IDC_ICON2                ; icon ID
      mov hAboutIcon,eax                                        ; icon handle
      invoke GetDlgItem,hWin,IDC_ABOUT_IMG           ; get handle of static control on dialog box
   ; eax = handle of dialog static control(IDC_ABOUT_IMG)
   ;STM_SETIMAGE - associate static control with icon image
   ;IMAGE_ICON
   ;hAboutIcon   - icon handle
   invoke SendMessage,eax,STM_SETIMAGE,IMAGE_ICON,hAboutIcon

old code:
   invoke  SendMessage,eax,STM_SETIMAGE,IDC_ICON2,hAboutIcon
   // I was using IDC_ICON2 which just happen to have the same value of 1 as IMAGE_ICON!  doh



Ok now it work  :bg


Mark