以编程方式检测窗口最大化/最小化??? Eclipse RCP

发布于 2024-12-01 20:36:05 字数 1287 浏览 1 评论 0 原文

正如标题所示,我想向我的 rcp 用户界面添加一个侦听器,以便检测最大化和最小化。其实这不是我真正的目的,但我认为这是解决我的问题的一种方法。我有一个中心有一些形状的视图,即使调整窗口大小,我也不会将绘图精确地保持在中心。为此,我使用了以下侦听器:

public void createPartControl(final Composite parent) {
    display = parent.getDisplay();
    white= display.getSystemColor(SWT.COLOR_WHITE);
    parent.setLayout(new FillLayout(SWT.VERTICAL));
    final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinHeight(100);
    sc.setMinWidth(100);
    sc.setSize(565, 305);
    final Composite child = new Composite(sc,SWT.NONE);   
    child.setLayout(new FillLayout());
    // Set child as the scrolled content of the ScrolledComposite
    sc.setContent(child);
    child.setBackground(white);
    gc = new GC(child);
    parent.addListener (SWT.Resize,  new Listener () {
        public void handleEvent (Event e) {
            x = child.getBounds().width/2;
            y = child.getBounds().height/2;
            child.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent event) {
                    dessin(gc);  // draw my shapes
    }
    });
}

一切都很顺利,除了当我最大化窗口然后最小化它时,在这种情况下我松开了绘图(它位于角落)。 有什么想法吗?我的想法正确吗?

As the title shows, I want to add a listener to my rcp user interface in order to detect maximization and minimization. Actually, it not that my real purpose, but I think it is a way to solve my problem. I have a view with some shapes in the center, and I wonna keep the drawing exactly in the center even if the window is resized. To do so, I used the following listener :

public void createPartControl(final Composite parent) {
    display = parent.getDisplay();
    white= display.getSystemColor(SWT.COLOR_WHITE);
    parent.setLayout(new FillLayout(SWT.VERTICAL));
    final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinHeight(100);
    sc.setMinWidth(100);
    sc.setSize(565, 305);
    final Composite child = new Composite(sc,SWT.NONE);   
    child.setLayout(new FillLayout());
    // Set child as the scrolled content of the ScrolledComposite
    sc.setContent(child);
    child.setBackground(white);
    gc = new GC(child);
    parent.addListener (SWT.Resize,  new Listener () {
        public void handleEvent (Event e) {
            x = child.getBounds().width/2;
            y = child.getBounds().height/2;
            child.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent event) {
                    dessin(gc);  // draw my shapes
    }
    });
}

everything goes well except when I maximize the window and then minimize it, in this case I loose the drawing (it is in the corner).
Any idea please? I'm I thinking in the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是暖光i 2024-12-08 20:36:05

检测最小化和非最小化(不一定是最大化)的两个事件是 IconifyDeiconify,它们仅发生在 Shell 上。请参阅 javadocs 外壳

The two events to detect minimization and un-minimization (not necessarily maximization) are Iconify and Deiconify which only occur on the Shell. See the javadocs for Shell.

梦明 2024-12-08 20:36:05

考虑移动父级的调整大小事件,因为子级不一定需要调整大小。

Consider moving the resize event is seen for the parent, as the child need not necessarily be resized yet.

征棹 2024-12-08 20:36:05

为了将某些内容保持在其他内容的中心,您所需要的只是 SWT.Resize 事件,因此这个问题是 XY 问题。 (除了这种情况下的OP似乎已经怀疑这可能是一个XY问题。)

但是,许多人提出这个问题时都有合理的需要以编程方式检测窗口最小化/最大化/恢复事件,原因如下

:如果您希望能够在退出时保存应用程序窗口的边界,则不能只保存 Shell.getBounds() 返回的任何内容,因为您的应用程序可能会在最小化、最大化或全屏时终止,在这种情况下,其边界不应该是坚持下来了。应该保留的是 shell 的最小化/正常/最大化/全屏状态(我称之为“姿势”),以及 shell 上次姿势为“正常”时的边界。因此,本质上,您需要跟踪姿势何时“正常”,为此您需要有一个“姿势改变”事件。

问题是,当 SWT 发出“deiconified”事件时,它尚未计算 shell 的边界,因此在这种情况下获得的值是伪造的。

因此,这里是解决方案:

您将需要一种重新计算姿势的方法,如下所示:

private void recalculatePosture()
{
    Posture posture = swtShell.getFullScreen()? Posture.FULLSCREEN 
        : swtShell.getMinimized()? Posture.MINIMIZED
        : swtShell.getMaximized()? Posture.MAXIMIZED
        : Posture.NORMAL;
    if( posture != previousPosture )
    {
        issue event...
        previousPosture = posture;
    }
}

为了生成“最大化”、“恢复(从最大化)”和“全屏”事件,您可以使用 Shell.addListener() 用于侦听 SWT.MoveSWT.Resize 事件,并在它们发生时调用 recalculatePosture() 发生。

为了生成“最小化”事件,您可以使用 ShellListenershellIconified() 方法,如 @the.duckman 所说,然后再次调用 recalculatePosture( )

为了生成“恢复(从最小化)”事件,您需要在 ShellListener 中执行以下操作:

@Override
protected void onShellDeiconified( ShellEvent e )
{
    display.asyncExec( () -> recalculatePosture() );
}

这将导致在之后的短时间内重新计算姿势 'deiconified' 事件,此时 SWT 将开始正确计算 shell 的边界。

In order to keep something in the center of something else all you need is the SWT.Resize event, so this question is a classic case of the XY Problem. (Except that the OP in this case seems to already suspect that this may be an XY Problem.)

However, many people arrive at this question with a legitimate need to programmatically detect window minimized / maximized / restored events, for the following reason:

If you want to be able to save the bounds of your application window on exit, you cannot just save whatever is returned by Shell.getBounds(), because your application may be terminated while minimized or maximized or fullscreen, in which case its bounds should not be persisted. What should be persisted is the minimized/normal/maximized/fullscreen state of the shell, (I call it "posture",) and the bounds of the shell last time its posture was "normal". So, essentially, you need to keep track of when the posture is "normal", and for that you need to have a "posture changed" event.

The problem is that when SWT issues the "deiconified" event, it has not calculated the bounds of the shell yet, so the value that you get in that case is bogus.

So, here is the solution to that:

You are going to need a method which recalculates the posture as follows:

private void recalculatePosture()
{
    Posture posture = swtShell.getFullScreen()? Posture.FULLSCREEN 
        : swtShell.getMinimized()? Posture.MINIMIZED
        : swtShell.getMaximized()? Posture.MAXIMIZED
        : Posture.NORMAL;
    if( posture != previousPosture )
    {
        issue event...
        previousPosture = posture;
    }
}

In order to generate the "maximized", "restored (from maximized)" and "fullscreen" events you can use Shell.addListener() to listen for the SWT.Move and SWT.Resize event, and invoke recalculatePosture() when they occur.

In order to generate the "minimized" event you can use the shellIconified() method of the ShellListener as @the.duckman said, and again, invoke recalculatePosture().

In order to generate the "restored (from minimized)" event, you need to do the following in your ShellListener:

@Override
protected void onShellDeiconified( ShellEvent e )
{
    display.asyncExec( () -> recalculatePosture() );
}

This will cause the recalculation of posture a short time after the 'deiconified' event, at which point SWT will have gotten around to properly calculating the bounds of the shell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文