代码之家  ›  专栏  ›  技术社区  ›  rogerdpack

如何强制JavaSWT程序“将自身移动到前台”?

  •  16
  • rogerdpack  · 技术社区  · 15 年前

    目前使用swt时,我有时希望一个程序能够任意出现在前台(就像闹钟可能出现的那样)。

    @shell.setMinimized(false)
    @shell.forceActive
    

    在任何时候创建一个新的shell也会将(新shell)带到最前面。

    然而,到目前为止,如果外壳是 最小化后,上述代码只会在任务栏中闪烁应用程序图标。实际上,你第一次运行它时,它就把它带到了最前面。之后,它只会在任务栏上闪烁。那是窗户。在Linux上,它似乎只在任务栏中闪烁(ubuntu默认)。

    有没有人知道一种跨平台的方法可以让应用程序在swt中占据领先地位?

    似乎没有一句咒语能做到这一点:forceActive setActive setMinimized(false)setFocus forceFocus和setVisible。

    我很确定这是可能的(至少在windows中),就像E文本编辑器所做的那样。这不是swt,但至少是其他一些应用 have been known to do it

    我在想也许这是 swt bug 192036

    相关的:

    6 回复  |  直到 8 年前
        1
  •  6
  •   Community CDub    8 年前

    http://github.com/rdp/redcar/commit/d7dfeb8e77f13e5596b11df3027da236f23c83f0

    显示了我是如何在windows中完成的(使用ffi)。

    一些有用的技巧“可能”是

    在BringToFront.SetForegroundWindow(需要)之后添加“sleep 0.1” 打电话(希望这一个不是必须的)。

    添加一个shell.set\u活动 之后

    注意,setActive执行user32.dll BringWindowToTop调用,需要在分离线程输入之前完成。

    http://betterlogic.com/roger/?p=2950

    正确的 )

    在Linux上,forceActive

    还涉及:

    How to bring a window to the front?

    http://github.com/jarmo/win32screenshot/blob/master/lib/win32/screenshot/bitmap_maker.rb#L110 “设置前景”似乎与 xp和windows 7

    [1] Need to bring application to foreground on Windows https://bugs.eclipse.org/bugs/show_bug.cgi?id=303710

        2
  •  6
  •   Manuel    13 年前

    private void bringToFront(final Shell shell) {
        shell.getDisplay().asyncExec(new Runnable() {
            public void run() {
                shell.forceActive();
            }
        });
    }
    
        3
  •  4
  •   L. Cornelius Dol    15 年前

    这实际上是Windows的一项功能,可以通过Tweak UI power toy启用(至少对于Windows XP)。启用时,O/S故意阻止窗口强制自己成为聚焦窗口,以阻止其“窃取焦点”。因此,抓取焦点的操作被更改为仅闪烁任务栏图标-因为O/S是根据用户的请求故意转换操作的,所以您对此无能为力(这是一件好事)。

    这(可能)是因为 所以 许多应用程序滥用了“带到前台”API,这种行为既让用户恼火,又导致他们输入错误的应用程序。

        4
  •  3
  •   Basilevs    11 年前

    Bug 192036 - Shell.forceActive doesn't raise a window above all other windows

    @rogerdpack's query on Eclipse bug tracker dirty workaround 做我们需要的。

    public void forceActive(Shell shell) {
        int hFrom = OS.GetForegroundWindow();
    
        if (hFrom <= 0) {
          OS.SetForegroundWindow(shell.handle);
          return;
        }
    
        if (shell.handle == hFrom) {
          return;
        }
    
        int pid = OS.GetWindowThreadProcessId(hFrom, null);
        int _threadid = OS.GetWindowThreadProcessId(shell.handle, null);
    
        if (_threadid == pid) {
          OS.SetForegroundWindow(shell.handle);
          return;
        }
    
        if (pid > 0) {
          if ( !OS.AttachThreadInput(_threadid, pid, true)) {
            return;
          }
          OS.SetForegroundWindow(shell.handle);
          OS.AttachThreadInput(_threadid, pid, false);
        }
    
        OS.BringWindowToTop(shell.handle);
        OS.UpdateWindow(shell.handle);
        OS.SetActiveWindow(shell.handle);
      }
    
        5
  •  2
  •   dklovedoctor    13 年前
    private static void onTop(Shell shell) {
            int s = -1;
            Shell[] shells = display.getShells();
            for (int i = 0; i < shells.length; ++i) {
                if (!shells[i].equals(shell)) {
                    shells[i].setEnabled(false);
                    shells[i].update();
                } else {
                    s = i;
                }
            }
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            for (int i = 0; i < shells.length; ++i) {
                if (i != s) {
                    shells[i].setEnabled(true);
                    shells[i].update();
                }
            }
        }
    
        6
  •  1
  •   milez jdi    10 年前

    shell.setMinimized(false) shell.setActive() 要恢复系统的以前状态,请执行以下操作: shell . ,只有当

    shell.getDisplay().syncExec(new Runnable() {
    
        @Override
        public void run() {
            if (!shell.getMinimized())
            {
                shell.setMinimized(true);
            }
            shell.setMinimized(false);
            shell.setActive();
        }
    });