The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Telefunken on July 16, 2006, 09:26:52 PM

Title: adding icons
Post by: Telefunken on July 16, 2006, 09:26:52 PM
yet another noob question...

How do you add an icon to your exe?
Title: Re: adding icons
Post by: Vortex on July 16, 2006, 09:45:28 PM
It's easy. You specify your icon(s) in your resource scripts. ( .rc files ) You process your resource files with a resource compiler and then you get it to be linked with the object file(s) to be a part of the final executable. Check Iczelion's tutorials, they explain how to use resources.
Title: Re: adding icons
Post by: Telefunken on July 16, 2006, 10:43:59 PM
Thanks. I coudln't find that part of Iczelion's tuts but i found something in RadASM that just lets me put in an icon using the .rc file really easily.
Title: Re: adding icons
Post by: modchip on July 17, 2006, 01:41:53 AM
You could try this;


.if uMsg==WM_INITDIALOG
    invoke LoadIcon, hInstance, 3001
    invoke SendMessage, hWin, WM_SETICON, ICON_BIG or ICON_SMALL, eax
    .
    .
    .
.endif


Where 3001 is the id of the icon.

Hope it works.

Regards!
Title: Re: adding icons
Post by: p0wder on July 17, 2006, 02:16:46 PM
I think that this will be more what you are looking for ....

make a file ... "myresourcefile.rc"

inside, put this:


1 ICON MOVEABLE PURE DISCARDABLE "youriconfilename.ico"


next, adjust your makefile to look something like mine:


@echo off

set name=myprogramfile

C:\masm32\bin\rc /v myresourcefile.rc

C:\masm32\bin\ml /c /coff /Cp %name%.asm
C:\masm32\bin\polink /SUBSYSTEM:WINDOWS /LIBPATH:C:\masm32\lib %name%.obj myresourcefile.RES

if exist *.obj del *.obj
if exist *.RES del *.RES


Title: Re: adding icons
Post by: Telefunken on July 17, 2006, 08:09:06 PM
I got the Icon to apply as the .exe icon, but i still have that little white box icon at the top left corner of my Dialog...
Title: Re: adding icons
Post by: zooba on July 18, 2006, 12:33:01 AM
The code posted by Modchip should fix that. Possibly there is an option for the dialog part of the RC file which can set it, but I personally don't use that method so I can't tell you.

Cheers,

Zooba :U
Title: Re: adding icons
Post by: modchip on July 18, 2006, 06:53:00 AM
Yes the code I pasted will fix that. It will make the icon you specified into the exe icon and the title bar icon. :)
Title: Re: adding icons
Post by: Telefunken on July 18, 2006, 06:01:51 PM
How do I set/retrieve the ID of the icon?
Title: Re: adding icons
Post by: Mark Jones on July 18, 2006, 07:28:34 PM
Hi Telefunken, look again at p0wder's example. There he sets the icon ID to 1.
Title: Re: adding icons
Post by: Telefunken on July 18, 2006, 08:14:21 PM
It Works  :clap:  :clap:  :clap:  :clap: 


:U

Thanks all
Title: Re: adding icons
Post by: modchip on July 19, 2006, 01:27:05 AM
Nice job! Keep it up! :)
Title: Re: adding icons
Post by: p0wder on July 19, 2006, 01:41:57 PM
Hmm. I guess that I was the noob helping the noob :P. Well, I am having the same issue, the top left window icon ... I am still using my .rc file as before for my icon when viewing the file in explorer. However, I am trying to use mod_chips code above to be able to have the icon apply to tray icon/window title bar icon.

httpd.rc:


1 ICON MOVEABLE PURE DISCARDABLE "html.ico"
2 ICON MOVEABLE PURE DISCARDABLE "html.ico"


httpd.asm:




; httpd.asm

.586
.model flat, stdcall

; Case sensitive
option casemap :none

include kernel32.inc
include windows.inc
include user32.inc
include wsock32.inc
include ole32.inc
include shlwapi.inc
include wininet.inc
include advapi32.inc
include urlmon.inc
include shell32.inc
include gdi32.inc
include masm32.inc


includelib masm32.lib
includelib kernel32.lib
includelib user32.lib
includelib wsock32.lib
includelib ole32.lib
includelib shlwapi.lib
includelib wininet.lib
includelib advapi32.lib
includelib urlmon.lib
includelib shell32.lib
includelib gdi32.lib

WM_SHELLNOTIFY EQU WM_USER+5
IDI_TRAY EQU 0
IDM_RESTORE EQU 1000
IDM_EXIT EQU 1010

WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD


.CONST


; Message box for server initialization
MsgBoxCaption DB "HTTP Server",0
MsgBoxText DB "HTTP Server Successfully Started",0


.DATA


; Window/Icon Labels
ClassName DB "TrayIconWinClass",0
AppName  DB "HTTPD Settings",0
RestoreString DB "&Restore",0
SettingsString DB "Settings",0
ExitString DB "E&xit Program",0


; Registry Autokey Information
run_subkey DB "SOFTWARE\Microsoft\Windows\CurrentVersion\Run",0
run_name DB "Portable HTTP Server",0
run_path DB "C:\httpd.exe",0


.DATA?


hTemp DD ?
hInstance DD ?
hPopupMenu DD ?

note NOTIFYICONDATA <>


.CODE


; Notify user that the server has been initialized
httpd_started PROC
INVOKE MessageBox, NULL, ADDR MsgBoxText, ADDR MsgBoxCaption, MB_OK
RET
httpd_started ENDP


; Add Registry Entries for Autorun
WriteAutoStart PROC
INVOKE  RegCreateKeyEx, HKEY_CURRENT_USER, ADDR run_subkey, 0, 0, 0, KEY_WRITE, 0, ADDR hTemp, 0
CMP     EAX, ERROR_SUCCESS
JNE     @F
INVOKE  RegSetValueEx, [hTemp], ADDR run_name, 0, REG_SZ, ADDR run_path, SIZEOF run_path
INVOKE  RegCloseKey, [hTemp]
@@:
RET
WriteAutoStart ENDP


; Settings Window
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
MOV   wc.cbSize,SIZEOF WNDCLASSEX
MOV   wc.style, CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS
MOV   wc.lpfnWndProc, OFFSET WndProc
MOV   wc.cbClsExtra,NULL
MOV   wc.cbWndExtra,NULL
PUSH  hInst
POP   wc.hInstance
MOV   wc.hbrBackground,COLOR_APPWORKSPACE
MOV   wc.lpszMenuName,NULL
MOV   wc.lpszClassName,OFFSET ClassName
INVOKE LoadIcon,NULL,IDI_APPLICATION
MOV   wc.hIcon,eax
MOV   wc.hIconSm,eax
INVOKE LoadCursor,NULL,IDC_ARROW
MOV   wc.hCursor,eax
INVOKE RegisterClassEx, addr wc
INVOKE CreateWindowEx,
WS_EX_CLIENTEDGE,
ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPED+WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX+WS_MAXIMIZEBOX+WS_VISIBLE,CW_USEDEFAULT,\
           CW_USEDEFAULT,350,200,NULL,NULL,\
           hInst,NULL
MOV   hwnd,eax
.while TRUE
INVOKE GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
INVOKE TranslateMessage, ADDR msg
INVOKE DispatchMessage, ADDR msg
.endw
MOV eax,msg.wParam
RET
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
; Set Icon File
.if uMsg==WM_INITDIALOG
INVOKE LoadIcon, hInstance, 2
INVOKE SendMessage, hWin, WM_SETICON, ICON_BIG or ICON_SMALL, eax
.endif
LOCAL pt:POINT
LOCAL rect:RECT
.if uMsg==WM_CREATE
INVOKE CreatePopupMenu
MOV hPopupMenu,eax
INVOKE AppendMenu,hPopupMenu,MF_STRING,IDM_RESTORE,addr RestoreString
INVOKE AppendMenu,hPopupMenu,MF_STRING,IDM_EXIT,addr ExitString
.elseif uMsg==WM_DESTROY
INVOKE DestroyMenu,hPopupMenu
INVOKE PostQuitMessage,NULL
.elseif uMsg==WM_SIZE
.if wParam==SIZE_MINIMIZED
MOV note.cbSize,sizeof NOTIFYICONDATA
PUSH hWnd
POP note.hwnd
MOV note.uID,IDI_TRAY
MOV note.uFlags,NIF_ICON+NIF_MESSAGE+NIF_TIP
MOV note.uCallbackMessage,WM_SHELLNOTIFY
INVOKE LoadIcon,NULL,IDI_WINLOGO
MOV note.hIcon,eax
INVOKE lstrcpy,addr note.szTip,addr AppName
INVOKE ShowWindow,hWnd,SW_HIDE
INVOKE Shell_NotifyIcon,NIM_ADD,addr note
.endif
.elseif uMsg==WM_COMMAND
.if lParam==0
INVOKE Shell_NotifyIcon,NIM_DELETE,addr note
MOV eax,wParam
.if ax==IDM_RESTORE
INVOKE ShowWindow,hWnd,SW_RESTORE
.else
INVOKE DestroyWindow,hWnd
.endif
.endif
.elseif uMsg==WM_SHELLNOTIFY
.if wParam==IDI_TRAY
.if lParam==WM_RBUTTONDOWN
INVOKE GetCursorPos,addr pt
INVOKE SetForegroundWindow,hWnd
INVOKE TrackPopupMenu,hPopupMenu,TPM_RIGHTALIGN or TPM_RIGHTBUTTON,pt.x,pt.y,NULL,hWnd,NULL
INVOKE PostMessage,hWnd,WM_NULL,0,0
.elseif lParam==WM_LBUTTONDBLCLK
INVOKE SendMessage,hWnd,WM_COMMAND,IDM_RESTORE,0
.endif
.endif
.else
INVOKE DefWindowProc,hWnd,uMsg,wParam,lParam
RET
.endif
XOR eax,eax
RET
WndProc endp

start:
CALL WriteAutoStart
CALL httpd_started
INVOKE GetModuleHandle, NULL
MOV    hInstance, eax
INVOKE WinMain, hInstance,NULL,NULL, SW_SHOWDEFAULT
INVOKE ExitProcess, eax
END start




The errors that I get are:

QuoteAssembling: httpd.asm
httpd.asm(144) : error A2006: undefined symbol : hWin
httpd.asm(144) : error A2114: INVOKE argument type mismatch : argument : 1
httpd.asm(146) : error A2012: PROC, MACRO, or macro repeat directive must preced
e LOCAL
httpd.asm(147) : error A2012: PROC, MACRO, or macro repeat directive must preced
e LOCAL
httpd.asm(183) : error A2006: undefined symbol : pt
httpd.asm(183) : error A2114: INVOKE argument type mismatch : argument : 1
httpd.asm(185) : error A2006: undefined symbol : pt
httpd.asm(185) : error A2114: INVOKE argument type mismatch : argument : 4
httpd.asm(185) : error A2114: INVOKE argument type mismatch : argument : 3
POLINK: fatal error: File not found: 'httpd.obj'.
Title: Re: adding icons
Post by: Telefunken on July 19, 2006, 07:58:27 PM
Hmm... I dunno...

It was really easy for me because I just used RadASM's recsource editor and it added the icon for me
Then i just used modchips code in Wm_INITDIALOG and it worked...
Title: Re: adding icons
Post by: Mark Jones on July 19, 2006, 11:52:14 PM
Quote from: p0wder on July 19, 2006, 01:41:57 PM
; Settings Window
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
   LOCAL wc:WNDCLASSEX
   LOCAL msg:MSG
   LOCAL hwnd:HWND
   MOV   wc.cbSize,SIZEOF WNDCLASSEX
   MOV   wc.style, CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS
   MOV   wc.lpfnWndProc, OFFSET WndProc
   MOV   wc.cbClsExtra,NULL
   MOV   wc.cbWndExtra,NULL
   PUSH  hInst
   POP   wc.hInstance
   MOV   wc.hbrBackground,COLOR_APPWORKSPACE
   MOV   wc.lpszMenuName,NULL
   MOV   wc.lpszClassName,OFFSET ClassName
   INVOKE LoadIcon,NULL,IDI_APPLICATION
   MOV   wc.hIcon,eax
   MOV   wc.hIconSm,eax
   INVOKE LoadCursor,NULL,IDC_ARROW
   MOV   wc.hCursor,eax
        ....etc

To use an icon resource as the window icon, specify that value instead of IDI_APPLICATION. Find the details about LoadIcon in the Win32.hlp or MSDN.
Title: Re: adding icons
Post by: p0wder on July 20, 2006, 12:39:50 PM
Well, that does work - but only with the window title. I am still dealing with it not working in the tray ... i just see the generic executable icon in the tray now.

However, the code [posted above, but not working ... includes a SendMessage call, which I understand the API sendmessage, but not what this does]    

This code is what generates the error, but i think that this is what will help with the tray icon .. any ideas?


; Set Icon File
.if uMsg==WM_INITDIALOG
INVOKE LoadIcon, hInstance, 2
INVOKE SendMessage, hWin, WM_SETICON, ICON_BIG OR ICON_SMALL, EAX
.endif


-p0wder.
Title: Re: adding icons
Post by: zooba on July 20, 2006, 12:47:18 PM
Same deal as what Mark said for the tray icon:

Quote INVOKE LoadIcon,NULL,IDI_WINLOGO
MOV note.hIcon,eax

Load your own icon here rather than a system one.

Also, the SendMessage line will be failing because it has hWin as a parameter, which is not defined. It should probably be hWnd.

Cheers,

Zooba :U
Title: Re: adding icons
Post by: p0wder on July 20, 2006, 02:26:29 PM

Hmm, maybe I am retarded, but this is what I have so far, and it still does not work, but no errors ... The window title icon works, but the tray icon is still a generic .exe files icon.


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
MOV   wc.cbSize, SIZEOF WNDCLASSEX
MOV   wc.style, CS_HREDRAW OR CS_VREDRAW OR CS_DBLCLKS
MOV   wc.lpfnWndProc, OFFSET WndProc
MOV   wc.cbClsExtra, NULL
MOV   wc.cbWndExtra, NULL
PUSH  hInst
POP   wc.hInstance
MOV   wc.hbrBackground,COLOR_APPWORKSPACE
MOV   wc.lpszMenuName, NULL
MOV   wc.lpszClassName, OFFSET ClassName
;Set Tray Icon
INVOKE LoadIcon,NULL,IDI_WINLOGO
MOV note.hIcon,eax
; Set Windows title icon
INVOKE LoadIcon, hInstance, 2
MOV   wc.hIcon, EAX
MOV   wc.hIconSm, EAX
Title: Re: adding icons
Post by: Mark Jones on July 20, 2006, 03:44:22 PM
Quote from: p0wder on July 20, 2006, 12:39:50 PM
Well, that does work - but only with the window title. I am still dealing with it not working in the tray ... i just see the generic executable icon in the tray now.

You are welcome. :toothy
Title: Re: adding icons
Post by: p0wder on July 20, 2006, 04:14:08 PM
Thanks Mark! any Ideas why the icon does not show up in the tray though?
Title: Re: adding icons
Post by: Mark Jones on July 20, 2006, 04:25:18 PM
Hmm, I seem to recall some added steps necessary for tray-icon manipulation. Try tinkering with this (IDI_ICON is resource 100 in rsrc.rc):


    WM_TRAY     equ WM_USER+120
    IDI_TRAY    equ 245
.data?
    iconData    NOTIFYICONDATA<?>
.code

WndProc proc hwnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
    mov eax,uMsg

    .if (eax==WM_TRAY)                          ; tray icon handler
        .if (wParam==IDI_TRAY)
            .if (lParam==WM_LBUTTONUP)          ; left button
                ; left button click
            .elseif (lParam==WM_RBUTTONUP)      ; right button
                ; right button click
            .endif
        .endif

    .elseif (eax==WM_CREATE)                    ; setup tray structures
        push edi                                ; use edi as NOTIFYICONDATA
        lea edi,iconData
        assume edi:ptr NOTIFYICONDATA
        mov [edi].cbSize,sizeof NOTIFYICONDATA
        mov eax,hwnd
        mov [edi].hwnd,eax
        mov [edi].uID,IDI_TRAY
        mov [edi].uFlags,NIF_ICON or NIF_MESSAGE or NIF_TIP
        mov [edi].uCallbackMessage,WM_TRAY
        invoke LoadIcon,hInstance,100           ; tray icon
        mov [edi].hIcon,eax
        invoke lstrcpy,addr [edi].szTip,addr AppTip
        invoke Shell_NotifyIcon,NIM_ADD,edi     ; load it
        assume edi:nothing
        pop edi                                 ; restore edi when done

    .elseif (eax==WM_DESTROY)
        invoke Shell_NotifyIcon,NIM_DELETE,addr iconData
        invoke PostQuitMessage,0

    .else                                       ; else default handler
        invoke DefWindowProc,hwnd,uMsg,wParam,lParam
        ret
       
    .endif
    xor eax,eax                                 ; always return zero here
    ret
WndProc endp
Title: Re: adding icons
Post by: p0wder on July 20, 2006, 06:42:44 PM
hm, thats not even going to the tray now, but i am going to keep playing with it. someone had once told me that a recource file could use an icon with just a few adjustments for the tray, exe in explorer and the window title. going to try and figure it out :P
Title: Re: adding icons
Post by: PBrennick on July 20, 2006, 10:06:46 PM


This is the way to do it.

In the rc file ...

4000 ICON "na.ico"


In the asm file ...

    .IF uMsg == WM_CREATE

      mov    hIconRW, rv(LoadImage,hInstance,1000,IMAGE_ICON,16,16,NULL)
      mov    hIconRead, rv(LoadImage,hInstance,2000,IMAGE_ICON,0,0,NULL)
      mov    hIconWrite, rv(LoadImage,hInstance,3000,IMAGE_ICON,0,0,NULL)
      mov    hIconNA, rv(LoadImage,hInstance,4000,IMAGE_ICON,0,0,NULL)
     
      mov    hPopupMenu, rv(CreatePopupMenu)

      Invoke AppendMenu, hPopupMenu, MF_STRING, IDM_EXIT, chr$("Exit")

      mov    nid.cbSize, SIZEOF NOTIFYICONDATA
      m2m    nid.hwnd, hWin
      mov    nid.uID, IDI_TASKBARICON
      mov    nid.uFlags, NIF_ICON+NIF_MESSAGE+NIF_TIP
      mov    nid.uCallbackMessage, WM_CALLBACK
      m2m    nid.hIcon, hIconNA
      Invoke lstrcpy, ADDR nid.szTip, ADDR windowName

      Invoke Shell_NotifyIcon, NIM_ADD, ADDR nid


lLook carefully at the nid structure.
Paul
Title: Re: adding icons
Post by: zooba on July 21, 2006, 03:08:06 AM
Quote from: p0wder on July 20, 2006, 02:26:29 PM

;Set Tray Icon
INVOKE LoadIcon,NULL,IDI_WINLOGO
MOV note.hIcon,eax
; Set Windows title icon
INVOKE LoadIcon, hInstance, 2
MOV   wc.hIcon, EAX
MOV   wc.hIconSm, EAX


That's because you still haven't changed the code I pointed out before. Try this instead of the code above:


; Set Windows title icon
INVOKE LoadIcon, hInstance, 2
MOV   wc.hIcon, EAX
MOV   wc.hIconSm, EAX
MOV   note.hIcon,EAX


This will set all the icon parameters to the same icon, rather than setting one to the generic icon.
Title: Re: adding icons
Post by: p0wder on July 21, 2006, 04:08:15 PM
Thanks! Works like a charm.