Monogame: How to get window handle in windows?

Created on 2 Oct 2016  路  2Comments  路  Source: MonoGame/MonoGame

In order to be able to embed the window in a winforms panel I'd like to run some pinvoke over the window handle after it is created. Is there a way to get it?

Most helpful comment

Nevermind, I found out how.

Basically if anybody needs to put the game in a WinForms panel, all they have to do is this after the game has been created and before it is run:

        IntPtr winHandle = game.Window.Handle;
        game.Window.IsBorderless = true;
        game.IsMouseVisible = true;

        Win32.SetWindowPos(
          winHandle,
          IntPtr.Zero,
          0,
          0,
          0,
          0,
          0x0401 // NOSIZE | SHOWWINDOW
        );

        Win32.SetParent(winHandle, winFormsPanel.Handle);
        Win32.SetWindowLong(winHandle, -20, 0x08000000); // WS_EX_NOACTIVATE, to prevent parent window from being deactivated when this window is clicked, yet keep receiving input
        Win32.ShowWindow(winHandle, 1); // SHOWNORMAL

Followed by setting the game backbuffer size to the panel control size on each resize.

All 2 comments

Nevermind, I found out how.

Basically if anybody needs to put the game in a WinForms panel, all they have to do is this after the game has been created and before it is run:

        IntPtr winHandle = game.Window.Handle;
        game.Window.IsBorderless = true;
        game.IsMouseVisible = true;

        Win32.SetWindowPos(
          winHandle,
          IntPtr.Zero,
          0,
          0,
          0,
          0,
          0x0401 // NOSIZE | SHOWWINDOW
        );

        Win32.SetParent(winHandle, winFormsPanel.Handle);
        Win32.SetWindowLong(winHandle, -20, 0x08000000); // WS_EX_NOACTIVATE, to prevent parent window from being deactivated when this window is clicked, yet keep receiving input
        Win32.ShowWindow(winHandle, 1); // SHOWNORMAL

Followed by setting the game backbuffer size to the panel control size on each resize.

Issues should be used for Bug tracking / Feature requests. If you have general questions about the framework please use MonoGame Community SIte or Gitter.

Was this page helpful?
0 / 5 - 0 ratings