News:

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

adding icons

Started by Telefunken, July 16, 2006, 09:26:52 PM

Previous topic - Next topic

Telefunken

yet another noob question...

How do you add an icon to your exe?

Vortex

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.

Telefunken

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.

modchip

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!

p0wder

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



Telefunken

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

zooba

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

modchip

Yes the code I pasted will fix that. It will make the icon you specified into the exe icon and the title bar icon. :)

Telefunken

How do I set/retrieve the ID of the icon?

Mark Jones

Hi Telefunken, look again at p0wder's example. There he sets the icon ID to 1.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Telefunken

It Works  :clap:  :clap:  :clap:  :clap: 


:U

Thanks all

modchip

Nice job! Keep it up! :)

p0wder

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'.

Telefunken

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

Mark Jones

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