Make all the forms associated with the program, come to the fore when choosing one form

I have two forms in my application MainForm and HexCompare . If I move from my application to another window, I click on one of two forms, and only one of them comes to the fore. How can I do this, if I click on one of the two forms, this will lead to the beginning of all open forms in the application? Right now I need to select each form separately to move them to the top of my window stack (and this can be very annoying because HexCompare has ShowInTaskbar set to false

A good example of this work, as I want, is how most search dialogs work. If you click the search dialog, it brings the main form to the foreground if it is hidden by another application, and if the main form is clicked, the search dialog will appear in front if it is hidden by another application.

How MainForm is MainForm .

 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } 

How is HexCompare called

 private void lstCaputres_SelectedIndexChanged(object sender, EventArgs e) { var selectedItem = (Tuple<DateTime, byte[]>)lstCaputres.SelectedItem; if (hexCompare == null || hexCompare.IsDisposed) { hexCompare = new HexCompare(selectedItem.Item2); hexCompare.Show(); } else hexCompare.ChangeValue(selectedItem.Item2); } 

EDIT:

It seems that the value of HexCompare Parent is Null . If I could somehow install it on MainForm , would that solve my problem, and if so, how did I install it?

EDIT2:

I decided to solve it using the Tigran solution, but it causes flickering, since each form is brought to the forefront if there is a better solution that interests me.

 //In MainForm.cs private void MainForm_Activated(object sender, EventArgs e) { hexCompare.BringToFront(); this.BringToFront(); } //in HexCompare.cs private void HexCompare_Activated(object sender, EventArgs e) { parent.BringToFront(); this.BringToFront(); } 
+4
source share
2 answers

You can use the following API wrapper to bring the form to the beginning of the z-order without taking focus. This function can be called in the activated event of your main form, just pass it as a parameter in the HexCompare format. This is not much different from the other answer, but I never saw any flicker, as you mentioned in the comments.

  private const int SW_SHOWNOACTIVATE = 4; private const int HWND_TOPMOST = 0; private const uint SWP_NOACTIVATE = 0x0010; [DllImport("user32.dll", EntryPoint = "SetWindowPos")] static extern bool SetWindowPos( int hWnd, // window handle int hWndInsertAfter, // placement-order handle int X, // horizontal position int Y, // vertical position int cx, // width int cy, // height uint uFlags); // window positioning flags [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); public void ShowInactiveTopmost(Form frm) { ShowWindow(frm.Handle, SW_SHOWNOACTIVATE); SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST, frm.Left, frm.Top, frm.Width, frm.Height, SWP_NOACTIVATE); } 
+5
source

It seems to me that should be enough to set TopMost=true and call BringToFront() in both forms.

 hexCompare = new HexCompare(selectedItem.Item2); hexCompare.TopMost = true; hexCompare.Show(); hexCompare.BringToFront(); 

Something like that.

0
source

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


All Articles