I'm sorry, but what you are asking is not (trivially) possible. The reason your window does not come on top is because your application is not the foreground application (i.e. - the application responsible for the foreground window).
From the manual page:
To use SetWindowPos to bring a window to the top, the process that
owns the window must have SetForegroundWindow permission.
Of course, you did that. You did not, however, check its return code. Here is what that man page has to say:
The system restricts which processes can set the foreground window. A
process can set the foreground window only if one of the following
conditions is true:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
The sum of these restrictions is that, in all probability, you cannot turn your window into a foreground window, and you cannot bring it to front.
There are ways around that. You can make sure the foreground process calls SetForegroundWindow. Unfortunately, that is not running your code.
The one time I had to do that, I used hooks in order to inject my code into the foreground window's process. This is not a beginner's trick. If you are not up to doing that, then there is no way to do what you are trying to do.
Edited to add: There is a reason Windows tries to stop you from doing what you are trying to do. Stealing the focus from another application creates very poor UI experience for the user. Please reconsider whether you want to do what you are trying to do.