News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

display icon on dialog box

Started by Mark_201, June 07, 2006, 12:35:20 AM

Previous topic - Next topic

Mark_201

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?






Mark Jones

Hello Mark,

Are you using an IDE like RadASM or Qeditor perhaps? Please give us more information and some code if possible.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Mark_201

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


hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

P1

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)

Mark Jones

#5
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
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

P1

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)

Mark_201

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

Mark_201

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