SWT实现窗口始终最前以及透明窗口

    技术2022-05-11  88

    从win2000开始,windows提供了一个新的api SetLayeredWindowAttributes,可以轻松实现透明窗口,在网上有许多vb,vc,delphi的示例程序,下面我来介绍一下如何使用swt来实现这一效果:

    BOOL SetLayeredWindowAttributes( HWND hwnd, // handle to the layered window COLORREF crKey, // specifies the color key BYTE bAlpha, // value for the blend function DWORD dwFlags // action );

    Windows NT/2000/XP: Included in Windows 2000 and later. Windows 95/98/Me: Unsupported. Header: Declared in Winuser.h; include Windows.h. Library: Use User32.lib.

    一些常量: WS_EX_LAYERED = 0x80000; LWA_ALPHA = 0x2; LWA_COLORKEY=0x1 其中dwFlags有LWA_ALPHA和LWA_COLORKEY LWA_ALPHA被设置的话,通过bAlpha决定透明度. LWA_COLORKEY被设置的话,则指定被透明掉的颜色为crKey,其他颜色则正常显示. 注:要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性(旧sdk也没有的). 

     在SWT实现代码:

    import  org.eclipse.swt.SWT; import  org.eclipse.swt.internal.win32.OS; import  org.eclipse.swt.internal.win32.TCHAR; import  org.eclipse.swt.layout.GridLayout; import  org.eclipse.swt.widgets.Display; import  org.eclipse.swt.widgets.Shell; public   class  TransparentForm  extends  Shell  {    public static void main(String args[]) {        try {            Display display = Display.getDefault();            TransparentForm shell = new TransparentForm(display, SWT.SHELL_TRIM);            shell.open();            shell.layout();            while (!shell.isDisposed()) {                if (!display.readAndDispatch())                    display.sleep();            }        } catch (Exception e) {            e.printStackTrace();        }    }    public TransparentForm(Display display, int style) {        super(display, style);        createContents();    }    protected void createContents() {        setText("SWT Application");        setSize(500375);        setLayout(new GridLayout());        OS.SetWindowLong(this.handle, OS.GWL_EXSTYLE, OS.GetWindowLong(                this.handle, OS.GWL_EXSTYLE) ^ 0x80000);        TCHAR lpLibFileName = new TCHAR(0"User32.dll"true);        int hInst = OS.LoadLibrary(lpLibFileName);        if (hInst != 0{            String name = "SetLayeredWindowAttributes

    最新回复(0)