请与程序走到前面相关的所有形式在选择一种形式 [英] Make all forms associated with a program come to the front when one form is selected

查看:114
本文介绍了请与程序走到前面相关的所有形式在选择一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式在我的应用程序的MainForm HexCompare 。如果我点击我的应用程序切换到另一个窗口,然后我点击后面的两种形式只是其中之一涉及到前面之一。我怎样才能使它所以如果我点击任一两种形式会带来既来之所有打开的窗体应用程序中的顶部?现在我需要单独选择每个表,让他们给我的窗口堆栈的顶部(这可以说是非常讨厌,由于这样的事实: HexCompare ShowInTaskbar 设置为

这个工作,我想要的方式一个很好的例子是大多数查找对话框的工作。如果点击查找对话框它带来的主要形式到前面,如果它是隐藏的另一个应用程序,而如果主窗体点击查找对话框就会走到前面,如果它是隐藏的其他应用程序。

如何的MainForm 被调用。

  [STAThread]
静态无效的主要()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(假);
    Application.Run(新的MainForm());
}
 

如何 HexCompare 调用

 私人无效lstCaputres_SelectedIndexChanged(对象发件人,EventArgs的)
{
    VAR将selectedItem =(元组LT,日期时间,byte []的>)lstCaputres.SelectedItem;
    如果(hexCompare == NULL || hexCompare.IsDisposed)
    {
        hexCompare =新HexCompare(selectedItem.Item2);
        hexCompare.Show();
    }
    其他
        hexCompare.ChangeValue(selectedItem.Item2);
}
 

编辑:

看来, HexCompare 的值。如果我能以某种方式将其设置为的MainForm 将能解决我的问题,如果是如何设置?

EDIT2:

我有半就解决了使用蒂格兰的解决方案,但它会导致闪烁因为每种形式带到前面,如果有一个更好的解决方案,我仍然有兴趣。

  //在MainForm.cs
私人无效MainForm_Activated(对象发件人,EventArgs的)
{
    hexCompare.BringToFront();
    this.BringToFront();
}

//在HexCompare.cs
私人无效HexCompare_Activated(对象发件人,EventArgs的)
{
    parent.BringToFront();
    this.BringToFront();
}
 

解决方案

您可以使用下面的 API包装带来了形式Z顺序的前面,而无需其窃取焦点。此功能可调用你的主窗体的Activated事件,只是通过它你HexCompare形式作为参数。这不是的的不同于其他的答案,但我从来没有看到任何闪烁,你提的意见。

 私人const int的SW_SHOWNOACTIVATE = 4;
  私人const int的HWND_TOPMOST = 0;
  私人常量UINT SWP_NOACTIVATE = 0×0010;

  [的DllImport(user32.dll中,入口点=SetWindowPos)
  静态的extern BOOL SetWindowPos(
       INT的hWnd,//窗口句柄
       INT hWndInsertAfter,//放置顺序手柄
       INT X,//水平位置
       INT Y,//垂直位置
       INT CX,//宽
       INT CY,//高
       UINT和uFlags); //窗口位置标志

  [的DllImport(user32.dll中)
  静态外部布尔的ShowWindow(IntPtr的的HWND,INT的nCmdShow);

  公共无效ShowInactiveTopmost(表格FRM)
  {
     的ShowWindow(frm.Handle,SW_SHOWNOACTIVATE);
     SetWindowPos(frm.Handle.ToInt32(),HWND_TOPMOST,
     frm.Left,frm.Top,frm.Width,frm.Height,
     SWP_NOACTIVATE);
  }
 

I have two forms in my application MainForm and HexCompare. If I click off of my app to another window then I click back on one of the two forms only one of them comes to the front. How can I make it so if I click either one of the two forms it will bring both to the top of all open forms in the application? Right now I need to select each form individually to get them to the top of my stack of windows (and this can be very annoying due to the fact that HexCompare has ShowInTaskbar set to false

A good example of this working the way I want to is how most Find dialogs work. If the find dialog is clicked it brings the main form to the front if it is hidden by another application, and if the main form is clicked the find dialog will come to the front if it is hidden by another application.

How MainForm is invoked.

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

How HexCompare is invoked

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 HexCompare's value of Parent is Null. If I could somehow set it to MainForm would that solve my issue, and if so how to I set it?

EDIT2:

I have semi-solved it using Tigran's solution but it causes flickering as each form is brought to the front, if there is a better solution I am still interested.

//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();
}

解决方案

You can use the following API wrapper to bring a form to the front of the z-order without having it steal focus. This function can be called in the Activated event of your main form, just pass it your HexCompare form as the parameter. This is not much different from the other answer but I've never witnessed any flicker as you mention 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);
  }

这篇关于请与程序走到前面相关的所有形式在选择一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆