Native window decorations: when resizing a window to the right or to the bottom, then first fill the new space with the window background color (instead of black) before the layout is updated (issue #339)

This commit is contained in:
Karl Tauber
2021-07-31 18:02:10 +02:00
parent d3e39a1359
commit 4ab90065dc
7 changed files with 108 additions and 6 deletions

View File

@@ -28,6 +28,8 @@ import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.geom.AffineTransform;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
@@ -47,6 +49,7 @@ import com.sun.jna.Structure.FieldOrder;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.GDI32;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WTypes.LPWSTR;
@@ -57,6 +60,7 @@ import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.UINT_PTR;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinUser.HMONITOR;
import com.sun.jna.platform.win32.WinUser.WindowProc;
import com.sun.jna.win32.W32APIOptions;
@@ -282,7 +286,7 @@ public class FlatWindowsNativeWindowBorder
//---- class WndProc ------------------------------------------------------
private class WndProc
implements WindowProc
implements WindowProc, PropertyChangeListener
{
private static final int GWLP_WNDPROC = -4;
@@ -345,9 +349,15 @@ public class FlatWindowsNativeWindowBorder
// remove the OS window title bar
updateFrame( (window instanceof JFrame) ? ((JFrame)window).getExtendedState() : 0 );
// set window background (used when resizing window)
updateWindowBackground();
window.addPropertyChangeListener( "background", this );
}
void uninstall() {
window.removePropertyChangeListener( "background", this );
// restore original window procedure
if( SystemInfo.isX86_64 )
User32Ex.INSTANCE.SetWindowLongPtr( hwnd, GWLP_WNDPROC, defaultWndProc );
@@ -382,6 +392,28 @@ public class FlatWindowsNativeWindowBorder
wmSizeWParam = -1;
}
@Override
public void propertyChange( PropertyChangeEvent evt ) {
updateWindowBackground();
}
private void updateWindowBackground() {
Color bg = window.getBackground();
if( bg != null )
setWindowBackground( bg.getRed(), bg.getGreen(), bg.getBlue() );
}
private void setWindowBackground( int r, int g, int b ) {
// delete old background brush
ULONG_PTR oldBrush = User32.INSTANCE.GetClassLongPtr( hwnd, GCLP_HBRBACKGROUND );
if( oldBrush != null && oldBrush.longValue() != 0 )
GDI32.INSTANCE.DeleteObject( new HANDLE( oldBrush.toPointer() ) );
// create new background brush
HBRUSH brush = GDI32Ex.INSTANCE.CreateSolidBrush( RGB( r, g, b ) );
User32Ex.INSTANCE.SetClassLongPtr( hwnd, GCLP_HBRBACKGROUND, brush );
}
/**
* NOTE: This method is invoked on the AWT-Windows thread (not the AWT-EventQueue thread).
*/
@@ -637,6 +669,13 @@ public class FlatWindowsNativeWindowBorder
return (short) ((lParam.longValue() >> 16) & 0xffff);
}
/**
* Same implementation as RGB(r,g,b) macro in wingdi.h.
*/
private DWORD RGB( int r, int g, int b ) {
return new DWORD( (r & 0xff) | ((g & 0xff) << 8) | ((b & 0xff) << 16) );
}
/**
* Opens the window's system menu.
* The system menu is the menu that opens when the user presses Alt+Space or
@@ -690,6 +729,8 @@ public class FlatWindowsNativeWindowBorder
LONG_PTR SetWindowLong( HWND hWnd, int nIndex, LONG_PTR wndProc );
LRESULT CallWindowProc( LONG_PTR lpPrevWndFunc, HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam );
LONG_PTR SetClassLongPtr( HWND hWnd, int nIndex, HANDLE wndProc );
int GetDpiForWindow( HWND hwnd );
int GetSystemMetricsForDpi( int nIndex, int dpi );
@@ -702,6 +743,16 @@ public class FlatWindowsNativeWindowBorder
BOOL TrackPopupMenu( HMENU hMenu, int uFlags, int x, int y, int nReserved, HWND hWnd, RECT prcRect );
}
//---- interface GDI32Ex --------------------------------------------------
private interface GDI32Ex
extends GDI32
{
GDI32Ex INSTANCE = Native.load( "gdi32", GDI32Ex.class, W32APIOptions.DEFAULT_OPTIONS );
HBRUSH CreateSolidBrush( DWORD color );
}
//---- class NCCALCSIZE_PARAMS --------------------------------------------
@FieldOrder( { "rgrc" } )