-1

I am working on a project but I faced a problem in making a form stay always on top like task manager in delphi I used this code but didn't work

SetWindowPos(Form1.Handle, HWND_TOPMOST, 0,0,0,0, SWP_NOACTIVATE+SWP_NOMOVE+SWP_NOSIZE);
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
AkA
  • 13
  • 5
  • 3
    Always use `or` when you combine flags, not `+`. You never know if the values have a bit in common. – Andreas Rejbrand Oct 16 '19 at 10:55
  • Since `fsStayOnTop` isn't what you are looking for, you need to be more specific. What exactly do you mean by "like Task Manager"? You need to tell us the precise behaviour you want to achieve. – Andreas Rejbrand Oct 16 '19 at 11:13

3 Answers3

2

Modern task manager uses internal private Windows functionality for its stay on top behaviour. The system does not make this available to user windows. The functionality that task manager uses simply isn't available to you.

Related question: Is Task Manager a special kind of 'Always on Top' window for windows 10?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Just use the Object Inspector at design time to set the form's FormStyle property to fsStayOnTop.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • already used it but it stays on top but not like task manager – AkA Oct 16 '19 at 11:00
  • Well, I don't know what you mean by "like task manager", so I cannot really help you unless you are more specific. Of course, it is impossible to make an application always on top of every other application, because if two applications were like this, we would have a contradiction. – Andreas Rejbrand Oct 16 '19 at 11:11
  • On top of the start menu? – Andreas Rejbrand Oct 16 '19 at 11:27
  • yep but it stays on top over every appication, https://imgur.com/Oazhyxr – AkA Oct 16 '19 at 11:32
  • 1
    @AkA: That is impossible. Proof: Suppose there is a way to make an application on top of every other application. Now make two such applications, A and B. Let their forms be red and blue, respectively. Start both applications. Move their windows so that they overlap on the screen. Because A has the property that it is above every other form, this region of the screen is red. But because B has the property that it is above every other form, this region of the screen is blue. This is impossible: a region on the screen cannot both be red and blue at the same time. – Andreas Rejbrand Oct 16 '19 at 11:35
  • It seems like you want it to be on top of all "ordinary" windows and special system "windows" like the start menu, the task view, etc. *That* is not mathematically impossible. – Andreas Rejbrand Oct 16 '19 at 11:39
  • @Andeeas Modern windows has a private stay on top mode that task manager uses, but we can't. – David Heffernan Oct 16 '19 at 13:29
  • @David Heffernan so it's not possible I used it ,Form1.DoubleBuffered:=True; Form1.PaintTo(GetDC(GetDesktopWindow),0,0); it seems working but flickering – AkA Oct 16 '19 at 16:41
  • function CreateWindowInBand( dwExStyle:DWORD;lpClassName,lpWindowName:LPCWSTR;dwStyle:DWORD;x,y, nWidth,nHeight:Integer; hWndParent:HWND;hMenu:HMENU;hInstance:HINST; lpParam:LPVOID;dwBand:DWORD):HWND;stdcall; external 'user32.dll'; function SetWindowBand(hWnd,hwndInsertAfter:HWND;dwBand:DWORD):Bool;stdcall; external 'user32.dll'; function GetWindowBand(hWnd:HWND;pdwBand:PDWORD):Bool;stdcall; external 'user32.dll'; I tested it, didn't work to set always on top. worked for creating form – AkA Oct 16 '19 at 18:59
  • 1
    @aka No one here told that a specific function will make it work. What's being said is that you won't be able to make it work. – Sertac Akyuz Oct 16 '19 at 19:21
  • @AkA was my answer lacking in clarity? – David Heffernan Oct 16 '19 at 20:36
  • @Aka: If I understand it correctly, `CreateWindowInBand` is an *undocumented* function. You should *never* use such functions in real products, because there is no contract telling you how they work, they might be changed in the next Windows update, etc. So if you use such a function, you might get strange behaviour in unpredictable ways, under certain conditions that you cannot possibly predict, and even in the cases where it did seem to work it might stop working at any time. – Andreas Rejbrand Oct 17 '19 at 05:49
0

The code from the original post might work on a main form, but will not work on a secondary form. fsStayOnTop is only part of the solution for a secondary form. Below is an easy solution for making a secondary form stay on top while the main form is obscured by other applications - without resorting to showmodal or form creation hacks.

Put this in the "Form B" OnCreate event:

FormStyle:= fsStayOnTop;

but that alone won't do the trick...

Drag a TApplicationEvents onto your "Form B"

In the OnDeactivate event for ApplicationEvents1, add the following:

SetForegroundWindow(Handle);

I keep an eye on a small status window while my main form is crunching data out of site. Works beautifully!

Mad Martian
  • 109
  • 6