0
votes

I am developing a single-window app on Windows with Unity.
I allow a user to resize the window but the aspect ratio must be kept.
I want to prevent fullscreen and halfscreen, because they break the aspect ratio.

I've found the following operations make an app fullscreen or halfscreen.

  • fullscreen:
    • clicking a maximize button on the title bar
    • dragging the window to top of screen
    • pressing Alt + Enter key
    • pressing Windows + Up-Arrow key
  • halfscreen:
    • dragging the window to left/right of screen
    • pressing Windows + Left/Right -Arrow key

I want to disable all of them.

This can disable a maximize button on the title bar.

HandleRef hWnd = new HandleRef(null, GetActiveWindow());
long style = GetWindowLong(hWnd, GWL_STYLE);
style &= ~WS_MAXIMIZEBOX;
SetWindowLong(hWnd, GWL_STYLE, style);

And this can disable Windows + Up-Arrow key.

private static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
    switch (msg)
    {
        case WM_SYSCOMMAND:
            if (wParam.ToInt32() == SC_MAXIMIZE) {
                return IntPtr.Zero;
            }
            break;
    }

    return CallWindowProc(oldWndProcPtr, hWnd, msg, wParam, lParam);
}

But the other operations still work.
How can I disable the other operations?

1

1 Answers

-1
votes
private static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
    switch (msg)
    {
        case WM_SYSCOMMAND:
            if (wParam.ToInt32() == SC_MAXIMIZE) {
                return IntPtr.Zero;
            }
            break;
        // BY PASS *** Alt + Enter ***
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
            if (wParam == VK_RETURN)
                if ((HIWORD(lParam) & KF_ALTDOWN))
                    return 0;
            break;  
    }

    return CallWindowProc(oldWndProcPtr, hWnd, msg, wParam, lParam);
}