BringToFront () in C #

I have an application that I made for a client, but sometimes he doesn’t know if the order has arrived because he plays World of Warcraft at maximum volume. But he said he wants my little notification window to appear on top of his game if a new order arrives.

So, I thought that I could just use BringToFront(); which seems to work when full-screen applications are deployed. But I noticed that while playing V8 Supercars in full screen mode, BringToFront(); does not display a notification window on top of the game, so I believe that in some games there is another way to make sure that they remain on top of everything else.

How can I be sure that whenever I need to see my form, it will always be displayed on top of everything else?

+10
source share
6 answers

You can try setting the TopMost form to a true ... notification form or .ShowDialog calling .ShowDialog instead of .Show .

+6
source
 form.TopMost = true; form.ShowDialog(); form.BringToFront(); 

It should work with all applications, including full-screen exclusive games (tested on all my games while it works).

+7
source

Here the VB code that calls the Windows API functions should be relatively easy to translate (note: this has not been verified, found on the forums, there may also be problems with the cursor appearing).

 Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal _ hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _ ByVal cy As Long, ByVal wFlags As Long) As Long Const HWND_TOPMOST = -1 Const SWP_NOMOVE = &H2 Const SWP_NOSIZE = &H1 Private Sub Form_Load() Call SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE) End Sub 

Create a timer at intervals of 1 with the following code:

 Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Sub Timer1_Timer() Dim mhwnd As Long mhwnd = GetForegroundWindow SetParent Form1.hwnd, mhwnd End Sub 

Code translated below (via automatic tool):

 const long HWND_TOPMOST = -1; const long SWP_NOMOVE = 2; const long SWP_NOSIZE = 1; [DllImport("user32.dll")] private static extern long SetWindowPos(long hwnd, long hWndInsertAfter, long X, long Y, long cx, long cy, long wFlags); private void Form_Load() { SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE)); } [DllImport("user32.dll")] private static extern long SetParent(long hWndChild, long hWndNewParent); [DllImport("user32.dll")] private static extern long GetForegroundWindow(); private void Timer1_Timer() { long mhwnd; mhwnd = GetForegroundWindow; SetParent; Form1.hwnd; mhwnd; } 
+2
source

By default, it will be displayed on top of the screen, but it is not a model.

You can use the Window.Show() method, which when closing a window changes its visibility to False when it is not required. You may need to play with the parent property of the child windows that were installed in the main window.

+1
source

I struggled with the same topic, especially when in Outlook clicked a "link" to the user protocol. (The app catches it, but always in the background ...)

Despite the fact that many solutions worked during debugging, for "Live-Deployment", it seems that only the following chain of calls reaches the desired:

(Called because link processing comes from a stream)

 this.Invoke(new Action(() => { this.Activate(); //...do stuff this.TopMost = true; this.BringToFront(); this.TopMost = false; })); 

It works about every time.

+1
source

Show () is equivalent to setting Visible = true. It does not change the control order of Z. If the control is closed by some other control that is in front of the Z-order, the user will still not be able to see your control.

BringToFront () changes the control of the Z-order (brings to the fore), but does not change its visibility. If the control is hidden, it will remain hidden. But when you make your control visible, it will appear in front of all the other controls.

The same with Hide () (makes the control invisible, but does not change the Z-order) and SendToBack (does not change the visibility, but returns the control back).

enter image description here

enter image description here

0
source

Source: https://habr.com/ru/post/890210/


All Articles