在java程序中利用swt实现窗口始终最前以及透明窗口 JavaOSBlendWindowsVB
程序员文章站
2022-07-04 19:38:05
...
从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也没有的).
上面是该函数的资料,下面是一段代码示例:
里面用到的几个类都在org.eclipse.swt.internal.win32下,相信大家看到上面的代码就都清楚该如何做了,我也就不多罗嗦了.
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也没有的).
上面是该函数的资料,下面是一段代码示例:
display = new Display();; shell = new Shell(display , SWT.NULL);; shell.setMaximized(false);; shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK););; shell.setText("Example");; shell.setBounds(0 , 700 , 1024 , 68);; //始终最前 OS.SetWindowPos(shell.handle , OS.HWND_TOPMOST, 0 , 700 , 1024 , 68 , SWT.NULL);; //透明窗体 OS.SetWindowLong(shell.handle , OS.GWL_EXSTYLE , OS.GetWindowLong(shell.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"; byte [] lpProcName = new byte [name.length ();]; for (int i=0; i<lpProcName.length; i++); { lpProcName [i] = (byte); name.charAt (i);; } int fun = OS.GetProcAddress(hInst , lpProcName);; if (fun != 0); { OS.CallWindowProc(fun , shell.handle , 0,50,1);; } OS.FreeLibrary(hInst);; }
里面用到的几个类都在org.eclipse.swt.internal.win32下,相信大家看到上面的代码就都清楚该如何做了,我也就不多罗嗦了.