Find composite location on screen

前提是你 提交于 2019-12-03 03:13:14

I believe the method Control.toDisplay() should be able to translate your coordinates into ones relative to the screen.

This snippet may illustrate what you are after:

package org.eclipse.jface.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

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

        final Text t = new Text(shell,SWT.BORDER);
        t.setBounds(new Rectangle(10,10,200,30));
        System.err.println(t.toDisplay(1, 1));

        Button b = new Button(shell,SWT.PUSH);
        b.setText("Show size");
        b.setBounds(new Rectangle(220,10,100,20));
        b.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {
                System.err.println(t.toDisplay(1, 1)); 
            }

        });

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!