Skip to content

Window not appearing centered on the monitor #1213

Description

@Timber1900

Description

When creating a openTK window with openTK 4.* the window does not appear in the middle of the screen

Repro steps

From what I can gather on my machine simply creating the window, even without rendering anything to it creates the issue

  1. Create the window without changing its location

Expected behavior

Window should appear centered on the screen
image

Actual behavior

Window appears slightly to the left and bottom
image

Related information

  • Windows 10
  • OpenTK 4.*
  • NetCore 3.1
  • DPI scaling is 100%
  • Workarround:

Center the window when creating the window using the location atribute:

public static class Screen
    {
        [DllImport("user32.dll")]
        private static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);

        /// <summary>
        ///     Gets the screen size
        /// </summary>
        /// <returns>Returns the screen size</returns>
        public static Vector2 GetScreenSize()
        {
            const int ENUM_CURRENT_SETTINGS = -1;

            DEVMODE devMode = default;
            devMode.dmSize = (short) Marshal.SizeOf(devMode);
            EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode);
            return new Vector2(devMode.dmPelsWidth, devMode.dmPelsHeight);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct DEVMODE
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
            public readonly string dmDeviceName;

            public readonly short dmSpecVersion;
            public readonly short dmDriverVersion;
            public short dmSize;
            public readonly short dmDriverExtra;
            public readonly int dmFields;
            public readonly int dmPositionX;
            public readonly int dmPositionY;
            public readonly int dmDisplayOrientation;
            public readonly int dmDisplayFixedOutput;
            public readonly short dmColor;
            public readonly short dmDuplex;
            public readonly short dmYResolution;
            public readonly short dmTTOption;
            public readonly short dmCollate;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
            public readonly string dmFormName;

            public readonly short dmLogPixels;
            public readonly int dmBitsPerPel;
            public readonly int dmPelsWidth;
            public readonly int dmPelsHeight;
            public readonly int dmDisplayFlags;
            public readonly int dmDisplayFrequency;
            public readonly int dmICMMethod;
            public readonly int dmICMIntent;
            public readonly int dmMediaType;
            public readonly int dmDitherType;
            public readonly int dmReserved1;
            public readonly int dmReserved2;
            public readonly int dmPanningWidth;
            public readonly int dmPanningHeight;
        }
    }

public MainRenderWindow(int width, int height, string title, double fps) :
            base(CreateGameWindowSettings(fps), CreateNativeWindowSettings(width, height, title))
        {
        }

        private static GameWindowSettings CreateGameWindowSettings(double fps = 60.0)
        {
            var gws = new GameWindowSettings
            {
                UpdateFrequency = fps,
                RenderFrequency = fps
            };
            return gws;
        }

        private static NativeWindowSettings CreateNativeWindowSettings(int width = 1000, int height = 1000,
            string title = "OpenTK Window")
        {
            var MonitorSize = Screen.GetScreenSize();
            var nws = new NativeWindowSettings
            {
                Size = new Vector2i(width, height),
                Title = title,
                Location = new Vector2i((int) ((MonitorSize.X - width) / 2), (int) ((MonitorSize.Y - height - 10) / 2)) //This centers the screen on the monitor
            };
            return nws;
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions