Wrong icon for new window when mainform is invisible lazarus / 31-LCL ---------------------------------------------------------------------- GADZombie · 2014-01-27 09:42 Hello I have Lazarus on Windows 8 64 bit. I have made a procedure which changes form's icon to different picture. It works very good. The icon is created with both sizes: 16x16 and 32x32. The problem is, when mainform in the project is invisible (hidden, no button on taksbar), then every other window can't change it's icon to different. It always receive default project icon. Even if I create 10 windows, every one has default project icon, not that I create. But when the mainform is visible, every other window can change it's icon to whatever I want. Why it isn't working? Is this some kind of bug in Lazarus? ---------------------------------------------------------------------- howardpc · 2014-01-27 10:31 Quote from: GADZombie on January 27, 2014, 10:42:57 amWhy it isn't working? Is this some kind of bug in Lazarus? Usually the quickest way to get suggestions from other people about code you have written that does not have the effect you desire is to share the code. Sometimes there will be a missing element (or logic flaw) in your code which someone 'outside' of your code will notice immediately. Less often we conclude that you have uncovered a bug in Lazarus. ---------------------------------------------------------------------- GADZombie · 2014-01-27 20:06 I had to prepare a full example. It was not so easy . Please, you can download it from here: http://gad.art.pl/bonus/formicontest.zip There is a source code and compiled to exe example. There are 2 forms. The first one is main and it creates an icon in tray. When you'll  open a popup from tray icon you may show main form and create new secondary form. There is button to create icon for current window. Now - try to open main form and open secondary form, then assign an icon to it (with button inside). It works fine. New icon appears. Now hide main form using button and try to assign an icon to secondary form again. New icon disappears and default project icon will show in secondary form. I noticed this behaviour only when secondary window has no border, as in this example. When there is a border, it works fine! But I need windows without borders... Thanks in advance. ---------------------------------------------------------------------- howardpc · 2014-01-27 21:33 Perhaps I'm being thick... but don't borderless windows lack icons by design - there is no border decoration for the OS to put an icon on, just as the OS cannot place Minimize/Maximize or Close icons either. Unless you manually draw the icon within the client area of the window, you will never see an icon on a borderless window, surely? ---------------------------------------------------------------------- GADZombie · 2014-01-27 21:37 Nope. Secondary form is visible on the taskbar. Every one secondary form has it's own button on the taskbar and that is why I want them have different icons. ---------------------------------------------------------------------- howardpc · 2014-01-27 21:58 Would changing your btnAssignHereClick handler to the following do what you want? Code: [Select]procedure TForm1.btnAssignHereClick(Sender: TObject); begin   Application.Icon.Assign(CreateIcon);   TrayIcon1.Icon.Assign(Icon); end; ---------------------------------------------------------------------- GADZombie · 2014-01-27 22:08 No. Did you see my example? It shows what I want. The tray icon don't need to change. Main form icon should not change also. But 'secondary form' is a window I create dynamically (there may be many of them at once) and I want every one has different icon. Main form may be invisible, but secondary shows on timer sometimes, when main form is hidden. And there is the problem. When main form is hidden, secondary forms has wrong icons. When main form is visible, secondary forms has different icons like I want. ---------------------------------------------------------------------- howardpc · 2014-01-28 22:52 The attached project has a way of creating and assigning icons to borderless forms that seems to work OK (at least on Windows). It's somewhat different from your approach, since that leads to a memory leak when assigning a png to an icon (perhaps you hadn't noticed - but if you use heaptrace it's obvious). I found the simplest way to do this without memory leaks is to go via a temporary imagelist. ---------------------------------------------------------------------- GADZombie · 2014-01-29 07:53 Thank you for your example. It's much different than mine. I will test it later, because I don't have Lazarus now. But why do you think there is a memory leak when I assign png picture to an icon? It works fine. I didn't try your example yet, but you use imagelist and bitmap and I don't know if this will keep alpha channel in my pictures. That's why I used png format. I load png files and covert them to icon. If I don't use png format, the alpha channel doesn't work properly. I did try it, but it's different issue, not for this thread, so maybe I will not discuss it  here. Is there a memory leak when I assign png to icon? Code: [Select]var   tmpPng: TPortableNetworkGraphic; begin   result := TIcon.Create;   //32x32   tmpPng := TPortableNetworkGraphic.Create;   tmpPng.PixelFormat := pf32bit;   tmpPng.SetSize(32, 32);   tmpPng.Canvas.Pen.Color := clRed;   tmpPng.Canvas.MoveTo(0, 0);   tmpPng.Canvas.LineTo(32, 32);   result.Assign(tmpPng); //<-- this is the memory leak?   tmpPng.Free; I do use tmpPng.Free after that, so where is the leak? ---------------------------------------------------------------------- howardpc · 2014-01-29 08:51 Quote from: GADZombie on January 29, 2014, 08:53:54 amBut why do you think there is a memory leak when I assign png picture to an icon? It works fine. I agree it works, but not fine if there is a leak. I think there is a leak only because heaptrace reports one. QuoteI do use tmpPng.Free after that, so where is the leak? I suppose it must be somewhere in the Assign() call. I looked briefly at the source, and internally AssignImage() is called, which is quite a complex method, and I suspect was not designed to cope with png to icon conversion. However, that is just a guess, I do not know enough about graphics programming to be sure. It does include some lines which are marked as Code: [Select]// temporary hack since TRasterImage assign cannot handle multiply rawimages which does not inspire confidence. Maybe Felipe or someone who understands graphics better than me can offer advice. You are right though that png may preserve the alpha channel where a bitmap may not. I had not considered that aspect in the example I offered. ---------------------------------------------------------------------- GADZombie · 2014-01-29 09:14 Thanks anyway for your help, I appreciate it Maybe I will learn something more - what is heaptrace and how to use it in Lazarus? If this will help me finding leaks, it would be great! If something is wrong inside Assign method, then I think it's a bug in this component. If this works fine with png files, and alpha channel is preserved, so it is implemented in some way. It's doesn't look good because of this leak. Maybe there is a better solution to do the same thing without leaks. Returning to my first question. Do you think the leaks and using png files rather than imagelist with bitmap is the problem and icons in forms disappears? ---------------------------------------------------------------------- howardpc · 2014-01-29 09:44 You should definitely be using the heaptrc unit, otherwise you may be unaware of memory leaks. You can either specify it in your uses clause, or incorporate it via a commandline compiler switch (-gh) or through the the Project Options dialog (Ctrl Shift F11). The Linking page of that dialog has a "Use Heaptrc unit" option, which lets you easily enable/disable this per project. Output reporting from the unit is by default to the Terminal/console, or if you are developing a GUI app under Windows you get a windowed output. For bad leaks this is seriously annoying, since you have to click away countless windows detailing all the unfreed memory blocks heaptrace has found. So better to use the SetHeapTraceOutput() routine in this case. You can also set an environment variable to control heaptrace output. Recent versions of the Lazarus IDE include the LeakView tool (Tools->Leakview) which gives you an integrated way to read heaptrace output files. Make sure the compiler switch -gl is also enabled, so you get line number information (memory addresses alone are not very helpful unless you are an assembler programmer and are used to thinking in these terms). Smartlinking leads to loss of this information. ---------------------------------------------------------------------- GADZombie · 2014-01-29 16:56 Quote from: howardpc on January 28, 2014, 11:52:40 pmThe attached project has a way of creating and assigning icons to borderless forms that seems to work OK (at least on Windows). Well, your project don't have the most important thing - creating new windows when main form is invisible. So I added new menuitem to popup menu that creates new window the same way as creating it by button from main form. Now when main form is hidden and you will create new window from popup menu it works the same as mine project! There is the same bug. Try the attached project. ---------------------------------------------------------------------- howardpc · 2014-01-29 18:19 Taskbar icons are drawn by the OS, which uses the default icon for the executable app. I suppose to customise a taskbar icon you have to override the default behaviour of the OS shell (which probably does not inspect the icon of each minimised window to check if it differs from its application icon). On Windows that would be some Shell_Notify function. I'll try to find out more. If there is an easy solution it will probably not be cross-platform. ---------------------------------------------------------------------- GADZombie · 2014-01-29 18:35 Thank you very much. I'm afraid of my whole application is not cross- platform . So this is not a problem. But I'm not sure if this all is about shell. If yes, then why I can change those icons when main form is visible? I will try this thing on Delphi. I have one at work, so I will do it later. I wander if the same is in Delphi application. ---------------------------------------------------------------------- GADZombie · 2014-01-30 09:49 Well, I think this issue can be closed, but I've learned something new. I tried the same in Delphi 2010 today and it looks like there is the same behaviour. Everything works fine until the window has no border. It always receive default application icon and has no it's own icon. Maybe it's because window has no border, so it has no icon, and default application icon is assigned to taskbar button, because borderless window cannot have an icon. But it's bad. I wanted to do it and I thought it will be easy . It is not. I have an idea, but I must try it. Maybe I will open main application window in minimised state, when I need to create additional borderless window. It's not the best solution, but I have no better idea now. ---------------------------------------------------------------------- reinerr · 2014-02-06 05:22 I had a bit of a play (Windows 7 64-bit). If the secondary form is border-less, as in your example, and the main window is hidden Windows never asks for ICON_BIG (with WM_GETICON). If the main window is visible or the secondary form has a border then this message is sent. So I'm not sure where the problem is nor how to resolve it. EDIT: I just tried with Delphi 7 and same result. If the main form is visible or the app is in the task bar then the icon changes, if the main form is hidden and there is no app icon in the taskbar then the icon is not updated. Actually hiding the app button from the taskbar required a bit of research! So this now definitely seems to be a "feature" of Windows. ---------------------------------------------------------------------- GADZombie · 2014-02-06 08:09 Thanks for you reply. I don't think this is Windows feature. I made this in Delphi 2010, but still I'm not sure it will work in Lazarus. I'll try it later. Look at attached example. The most important part is: Override the CreateParams procedure in borderless form: Code: [Select]procedure TBorderlessForm.CreateParams(var Params: TCreateParams); begin   inherited;   Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;   Params.WndParent := GetDesktopWindow;  //without this line assigned icon will not work end; I must assign WndParent to GetDesktopWindow result. I haven't find this function in Lazarus, but it's from user32.dll: Code: [Select]function GetDesktopWindow; external user32 name 'GetDesktopWindow'; The second part is hiding main form with this: Code: [Select]procedure TMainForm.btnHideClick(Sender: TObject); begin   self.hide;   Application.ShowMainForm := False;   ShowWindow(Application.Handle, SW_HIDE);    //mainform button is hidden //  ShowWindow(Application.MainForm.Handle, SW_HIDE);   //mainform button is not hidden end; And the worst is that in Lazarus there is no Application.Handle! I don't know what is this, but in Delphi assignment to FHandle looks like this: Code: [Select][UIPermission(SecurityAction.LinkDemand, Window=UIPermissionWindow.AllWindows)] procedure TApplication.CreateHandle; var   LHandle: HWND;   SysMenu: HMenu; {$IF DEFINED(CLR)}   TempClass: TWndClassInfo;   WindowClass: TWndClass; {$ELSE}   TempClass: TWndClass; {$IFEND} begin {$IF DEFINED(CLR)}   if not FHandleCreated and not IsConsole then   begin     if not Assigned(FResources) then       FResources := TApplicationResources.Create;     FResources.FObjectInstance := MakeObjectInstance(WndProc);     WindowClass.lpszMenuName := '';     WindowClass.lpszClassName := 'TApplication'; {$ELSE} {$IFDEF MSWINDOWS}   if not FHandleCreated and not IsConsole then {$ENDIF} {$IFDEF LINUX}   if not FHandleCreated then {$ENDIF}   begin     FObjectInstance := MakeObjectInstance(WndProc); {$IFEND}     WindowClass.lpfnWndProc := @DefWindowProc;     if not GetClassInfo(HInstance, WindowClass.lpszClassName, TempClass) then     begin       WindowClass.hInstance := HInstance;       if Windows.RegisterClass(WindowClass) = 0 then         raise EOutOfResources.Create(SWindowClass);     end;     LHandle := CreateWindowEx(WS_EX_TOOLWINDOW, WindowClass.lpszClassName, {$IFNDEF CLR}PChar{$ENDIF}(FTitle),       WS_POPUP or WS_CAPTION or WS_CLIPSIBLINGS or WS_SYSMENU       or WS_MINIMIZEBOX,       GetSystemMetrics(SM_CXSCREEN) div 2,       GetSystemMetrics(SM_CYSCREEN) div 2,       0, 0, 0, 0, HInstance, nil); {$IF DEFINED(CLR)}     FResources.FHandle := LHandle; {$ELSE}     FHandle := LHandle; //<----------- HERE {$IFEND}     FHandleCreated := True; {$IF DEFINED(CLR)}     SetWindowLong(Handle, GWL_WNDPROC, @FResources.FObjectInstance); {$ELSE}     SetWindowLong(FHandle, GWL_WNDPROC, LPARAM(FObjectInstance)); {$IFEND}     if NewStyleControls then     begin       SendMessage(Handle, WM_SETICON, ICON_BIG, LPARAM(GetIconHandle));       SetClassLong(Handle, GCL_HICON, LPARAM(GetIconHandle));     end;     SysMenu := GetSystemMenu(Handle, False);     DeleteMenu(SysMenu, SC_MAXIMIZE, MF_BYCOMMAND);     DeleteMenu(SysMenu, SC_SIZE, MF_BYCOMMAND);     if NewStyleControls then DeleteMenu(SysMenu, SC_MOVE, MF_BYCOMMAND);   end; end; When I use Application.Mainform.Handle, it works weird, because if another than mainform window is visible, the mainform button will not disappear from taskbar. When there is no other forms visible, mainform button will hide. So this is not what I want. Using this wih Application.Handle works fine in Delphi. ---------------------------------------------------------------------- GADZombie · 2014-02-06 19:13 It worked in Lazarus! I had to add this one procedure: Code: [Select]procedure TBorderlessForm.CreateParams(var Params: TCreateParams); begin   inherited;   Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;   Params.WndParent := GetDesktopWindow; end; And it works as I want. So the WndParent must be set to DesktopWindow handle taken from GetDesktopWindow function. I was wrong, this function is in Lazarus also, but in Windows unit. So, the problem is solved Great! Thank you all!