Re: Setting Icon....
- Posted by Daniel Berstein <daber at PAIR.COM> Feb 28, 1999
- 467 views
At 12:24 PM 28-02-1999 , you wrote: >The Single window when its created is assign a default icon. >I do not think you can change it on the fly. So you have to assign your >own icon as the deault icon when you create it. > >But a MDI window I think can change the icon of any of its child >by sending a message to it because it creates these child windows. >So are trying to change a single window or a MDI window ? >If its a single window use your ICON as the default when creating it. >To change the MDI you have find out the correct message to send. In fact the icon used in a window is defined on it's window class. You can send a WM_SETICON message, but the message is only recognized on Win9x, not NT. If you want to change it at runtime you need to get (or create) a new icon handle and modify the window class with the SetClassLong() WinAPI function. In the following code I use the LoadImage() API function to load from file an icon. The ChangeWindowIcon modifies the small icon associated with the window (GCL_HICONSM). The small icon is displayed on the left-top corner of the window and in the taskbar. The large icon (GCL_HICON) is used when switching task using ALT+TAB. I tested the code and worked fine (on NT 4.0). I modified window.exw (on %EUDIR%\DEMO\WIN32) to call ChangeWindowIcon when creating the Window (WM_CREATE), tell me if it works OK on other circumstances. Perhaps it requieres a repaint before displaying the new icon. --[CHANGEICON.EW]-- include dll.e -- Class field offsets for GetClassLong() global constant GCL_MENUNAME = -8, -- Pointer to the menu name string GCL_HBRBACKGROUND = -10, -- Handle to the default background brush GCL_HCURSOR = -12, -- Handle to the window class cursor GCL_HICON = -14, -- Handle to the window class icon GCL_HMODULE = -16, -- Handle of the module that registered the class GCL_CBWNDEXTRA = -18, -- Size of extra memory associated with each --window of this class, in bytes. GCL_CBCLSEXTRA = -20, -- Size of the extra memory associated with this --class, in bytes. GCL_WNDPROC = -24, -- Pointer to the window procedure for this class. --If a developer replaces the window procedure --using this index, it must conform to the window --procedure callback definition as outlined in the --RegisterClass function. This subclass will affect --all windows subsequently created with this class. --An application should not subclass a window created --by another process. GCL_STYLE = -26, -- 32-bit style bits for this class GCW_ATOM = -32, -- Atom that uniquely identifies this class. This --is the same atom returned by the RegisterClass --and RegisterClassEx functions GCL_HICONSM = -34 --Handle to the window class small icon -- LoadImage() constants global constant IMAGE_BITMAP = 0, IMAGE_ICON = 1, IMAGE_CURSOR = 2, IMAGE_ENHMETAFILE = 3, LR_DEFAULTCOLOR = #0000, LR_MONOCHROME = #0001, LR_COLOR = #0002, LR_COPYRETURNORG = #0004, LR_COPYDELETEORG = #0008, LR_LOADFROMFILE = #0010, LR_LOADTRANSPARENT = #0020, LR_DEFAULTSIZE = #0040, LR_VGACOLOR = #0080, LR_LOADMAP3DCOLORS = #1000, LR_CREATEDIBSECTION = #2000, LR_COPYFROMRESOURCE = #4000, LR_SHARED = #8000 atom user32, ApiSetClassLong, ApiLoadImage procedure init() user32 = open_dll("user32.dll") ApiSetClassLong = define_c_func(user32, "SetClassLongA", {C_INT, C_INT, C_LONG}, C_LONG) ApiLoadImage = define_c_func(user32, "LoadImageA", {C_INT, C_POINTER, C_UINT, C_INT, C_INT, C_UINT}, C_LONG) end procedure global function LoadImage(integer hinst, sequence name, integer utype, integer cxDesired, integer cyDesired, integer fuLoad) atom lpszname lpszname = allocate_string(name) return c_func(ApiLoadImage, {hinst, lpszname, utype, cxDesired, cyDesired, fuLoad}) free(lpszname) end function global function SetClassLong(integer hwnd, integer nIndex, integer NewVal) return c_func(ApiSetClassLong, {hwnd, nIndex, NewVal}) end function global function ChangeWindowIcon(integer hwnd, sequence icon_file) integer hicon hicon = LoadImage(0, icon_file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE) return SetClassLong(hwnd, GCL_HICONSM, hicon) end init() --[TEST.EXW]-- include changeicon.ew --TO DO: Create and populate window -- hwnd = window handle ChangeWindowIcon(hwnd, "path\\icon_file.ico") Regards, Daniel Berstein [daber at pair.com]