1

我正在使用最新的稳定版本的 SWT。

我有一个包含 ToolBar、ScrolledComposite 和 Canvas 的 Shell。Canvas 被设置为 ScrolledComposite 的内容(例如 scrolledComposite.setContent(canvas))。画布以永远不变的特定尺寸创建(例如,400 x 400)。而 ScrolledComposite 不断增长或缩小以填充可用的父 shell 的客户区域。

我有一个附加到父 Shell 的调整大小侦听器,它尝试执行以下操作:a)如上所述增长 ScrolledComposite 和 b)在 ScrolledComposite 中水平和垂直居中画布(参见下面的示例代码)。

这与在 Mac OS X 上的工作方式完全相同,但是在 Windows 上会触发 resize 事件并正确计算新位置,但最终 Canvas 会恢复为 0,0。另一个小信息是,如果您不断调整窗口大小,您会看到画布闪烁,并且看起来好像是在正确的位置短暂绘制。

_shell.addListener (SWT.Resize, new Listener () {
    public void handleEvent (Event e)
    {
        int toolBarOffset = _toolBar.getSize().y;

        Rectangle clientArea = _shell.getClientArea();

        _scrollComposite.setBounds(0, toolBarOffset, clientArea.width, clientArea.height - toolBarOffset);

        Point canvasSize = _canvas.getSize();

        int canvasX = Math.max(0, (clientArea.width / 2) - (canvasSize.x / 2));
        int canvasY = Math.max(0, ((clientArea.height - toolBarOffset) / 2) - (canvasSize.y / 2));

        _canvas.setLocation(canvasX, canvasY);
    }
});
4

1 回答 1

1

不确定您是否已经弄清楚了,但问题是 ScrolledComposite 在调整大小时总是将内容设置为 0/0。我不确定为什么您的方法适用于 OS X,而且我没有测试我的示例,因为我这里没有 Mac。

解决方案是使用填充组合,它始终至少与 ScrolledComposite 的客户区一样大。在那个填充物中,您可以正确地将 Canvas 居中。

我做了一个小例子,作为额外的好处,如果 SC 的客户区域小于画布,它也会使画布居中(因为我首先认为这是你的问题 :))

你可能需要做一些小的调整,我猜 OS X 上的代码有一些小故障......

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

    public static void main(final String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        shell.setLayout( new FillLayout() );

        final ScrolledComposite sComp = new ScrolledComposite( shell, SWT.H_SCROLL | SWT.V_SCROLL );
        final Composite fillComp = new Composite( sComp, SWT.NONE );

        sComp.setLayout( new FillLayout() );

        final Canvas c = new Canvas( fillComp, SWT.DOUBLE_BUFFERED );
        c.addPaintListener( new PaintListener() {
            public void paintControl(PaintEvent e) {
                Point p = c.getSize();
                e.gc.setBackground( display.getSystemColor( SWT.COLOR_RED ));
                e.gc.fillRectangle( 0, 0, p.x, p.y );

                e.gc.setBackground( display.getSystemColor( SWT.COLOR_BLUE ));
                e.gc.fillRectangle( p.x / 2 - 10, p.y / 2 - 10, 20, 20 );
            }
        });

        c.setSize( 400, 400 );
        sComp.setContent( fillComp );

        sComp.addControlListener( new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                Rectangle clientArea = sComp.getClientArea();

                Point cSize = c.getSize();
                int fillX = Math.max( clientArea.width, cSize.x );
                int fillY = Math.max( clientArea.height, cSize.y );
                fillComp.setSize( fillX, fillY );

                int cX = ( clientArea.width - cSize.x ) / 2;
                int cY = ( clientArea.height - cSize.y ) / 2;

                sComp.setOrigin( -cX, -cY );

                int locX = Math.max( cX, 0 );
                int locY = Math.max( cY, 0 );
                c.setLocation( locX, locY );
            }
        });

        shell.open();
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }   
    }
}
于 2009-08-25T22:18:47.980 回答