Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have experience with JNI, then this is a possible solution.</p> <p>In Windows and Oracle Hotspot, this code will allow a window to be resized between minimum and maximum dimensions without flickering or causing any of the nasty JFrame-post-resize side-effects. If this code is called once the JFrame is created and made visible, then the code used above in componentResized() to catch resize events can be completely done away with, as Windows has facilities to keep the user from being able to resize above or below the specified min/max sizes.</p> <p>// Java code:</p> <pre><code>static { if (System.getProperty("sun.arch.data.model").equals("32")) { // 32-bit JVM System.loadLibrary("my32bitdll"); System.out.println("Running 32-bit JVM"); } else { // 64-bit JVM System.loadLibrary("my64bitdll"); System.out.println("Running 64-bit JVM"); } } // Sets a window to never be resized above or below these minimum widths/heights public static native int setMinMaxResizeBoundaries(int hwnd, int minWidth, int minHeight, int maxWidth, int maxHeight); </code></pre> <p>// C++ code (include standard windows.h, winbase.h, etc)</p> <pre><code>// Global variables defined in DllMain.cpp // Used for setMinMaxResizeBoundaries() struct SHwndMinMax { HWND hwnd; int minWidth; int minHeight; int maxWidth; int maxHeight; WNDPROC prefWndProc; }; SHwndMinMax gsHwndMinMax[2048]; int gsHwndMinMaxCount = 0; LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); // Code added somwhere: // setMinMaxResizeBoundaries() // Sets the resize boundary window sizes, so the window will not be resized above/below that size JNIEXPORT jint JNICALL Java_your_class_here_setMinMaxResizeBoundaries(JNIEnv* env, jclass cls, jint hwnd, jint minWidth, jint minHeight, jint maxWidth, jint maxHeight) { // We create a hook for the window, and intercept the WM_GETMINMAXINFO message occurs, and update the info if (IsWindow((HWND)hwnd)) { // Let's add it if (gsHwndMinMaxCount &lt; 2048) { // We're good // Can add code here to check if this option is valid or not--so it can later be "unhooked" by a separate function call gsHwndMinMax[gsHwndMinMaxCount].hwnd = (HWND)hwnd; gsHwndMinMax[gsHwndMinMaxCount].minWidth = minWidth; gsHwndMinMax[gsHwndMinMaxCount].minHeight = minHeight; gsHwndMinMax[gsHwndMinMaxCount].maxWidth = maxWidth; gsHwndMinMax[gsHwndMinMaxCount].maxHeight = maxHeight; gsHwndMinMax[gsHwndMinMaxCount].prefWndProc = (WNDPROC)SetWindowLongPtr((HWND)hwnd, GWLP_WNDPROC, (LONG_PTR)&amp;MinMaxWindowProc); // Success ++gsHwndMinMaxCount; return(0); } else { // Failuire, too many hooks return(-2); } } else { // Failure, HWND is not valid return(-1); } } LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { int i; MINMAXINFO* mmi; for (i = 0; i &lt; gsHwndMinMaxCount; i++) { if (hwnd == gsHwndMinMax[i].hwnd) { // This is our man, see if it's our message if (msg == WM_GETMINMAXINFO) { // It is // When maximized, window is at upper-left mmi = (MINMAXINFO*)lParam; mmi-&gt;ptMaxSize.x = gsHwndMinMax[i].maxWidth; mmi-&gt;ptMaxSize.y = gsHwndMinMax[i].maxHeight; mmi-&gt;ptMaxPosition.x = 0; // Can add code here to properly position the window centered in the screen, etc. mmi-&gt;ptMaxPosition.y = 0; // Same here // Set the minimum and maximum tracking size (when the user is resizing, what's the smallest and biggest window they see) mmi-&gt;ptMinTrackSize.x = gsHwndMinMax[i].minWidth; mmi-&gt;ptMinTrackSize.y = gsHwndMinMax[i].minHeight; mmi-&gt;ptMaxTrackSize.x = gsHwndMinMax[i].maxWidth; mmi-&gt;ptMaxTrackSize.y = gsHwndMinMax[i].maxHeight; return(DefWindowProc(hwnd, msg, wParam, lParam)); } else { // Nope, pass it on return(CallWindowProc(gsHwndMinMax[i].prefWndProc, hwnd, msg, wParam, lParam)); } } } return(0); } </code></pre> <p>// The following is code to get the HWND accurately:</p> <p>// Java Code (add to Java code above)</p> <pre><code>// Returns the HWND for the specified component, or -1 if does not exist public static native int getComponentHWND(Component c); </code></pre> <p>// Code in C++</p> <pre><code>// getComponentHWND() // Called to return the HWND of the component, if it has one. JNIEXPORT jint JNICALL Java_your_class_here_getComponentHWND(JNIEnv* env, jclass cls, jobject obj) { HWND hWnd = 0; typedef jboolean (JNICALL *PJAWT_GETAWT)(JNIEnv*, JAWT*); JAWT awt; JAWT_DrawingSurface* ds; JAWT_DrawingSurfaceInfo* dsi; JAWT_Win32DrawingSurfaceInfo* dsi_win; jboolean result; jint lock; HMODULE _hAWT = 0; // Load AWT Library if (!_hAWT) _hAWT = LoadLibrary(L"jawt.dll"); // for Java 1.4+ if (!_hAWT) _hAWT = LoadLibrary(L"awt.dll"); // for Java 1.3 if (_hAWT) { PJAWT_GETAWT JAWT_GetAWT = (PJAWT_GETAWT)GetProcAddress(_hAWT, "JAWT_GetAWT"); if (JAWT_GetAWT) { awt.version = JAWT_VERSION_1_4; // Init here with JAWT_VERSION_1_3 or JAWT_VERSION_1_4 // Get AWT API Interface result = JAWT_GetAWT(env, &amp;awt); if (result != JNI_FALSE) { ds = awt.GetDrawingSurface(env, obj); if (ds != NULL) { lock = ds-&gt;Lock(ds); if ((lock &amp; JAWT_LOCK_ERROR) == 0) { dsi = ds-&gt;GetDrawingSurfaceInfo(ds); if (dsi) { dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi-&gt;platformInfo; if (dsi_win) hWnd = dsi_win-&gt;hwnd; else hWnd = (HWND) -1; // Failed to obtain the handle (not running on Windows) ds-&gt;FreeDrawingSurfaceInfo(dsi); } else { hWnd = (HWND)-2; // Failed to get the drawing surface info block } ds-&gt;Unlock(ds); } else { hWnd = (HWND)-3; // Failed to lock the drawing surface to obtain information about it } awt.FreeDrawingSurface(ds); } else { hWnd = (HWND)-4; // Failed to get the drawing surface from the compoment } } else { hWnd = (HWND)-5; // Failed to obtain a proper result from _JAWT_GetAWT() } } else { hWnd = (HWND)-6; // Failed to find "_JAWT_GetAWT()" function } } else { hWnd = (HWND)-7; // Failed to load awt.dll } return (jint)hWnd; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload