SWT實現窗口始終最前以及透明窗口

從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
發佈了23 篇原創文章 · 獲贊 2 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章