Reputation: 41
I'm trying to find a way to keep a window always on top. (it has to be in windowed mode) I'm currently using OpenTk.NetCore library for making a window in .net core. Is it possible to use OpenTk for keeping this window on top or is there an other way? It has to be in .Net core ...
my current code:
public class Display : GameWindow
{
public Display() : base(400, 300, GraphicsMode.Default)
{
//display window in top left corner
this.X = 0;
this.Y = 0;
//TODO : window should always been displayed on top
VSync = VSyncMode.On;
WindowBorder = WindowBorder.Hidden; //no title & border
//WindowState = WindowState.Fullscreen;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL.Enable(EnableCap.DepthTest);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Keyboard[Key.Escape])
Exit();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
GL.Begin(PrimitiveType.Triangles);
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(2.0f, 1.0f, 4.0f);
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.2f, 1.0f, 4.0f);
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.6f, 1.5f, 4.0f);
GL.End();
SwapBuffers();
}
}
public class Program
{
[STAThread]
public static void Main(string[] args)
{
/*
* The 'using' idiom guarantees proper resource cleanup.
* We request 30 UpdateFrame events per second, and 30
* RenderFrame events.
*/
using (Display display = new Display())
{
display.Run(30.0,30.0);
}
}
}
Upvotes: 3
Views: 757
Reputation: 772
OpenTK doesn't have support for this. You can detect when the window's focus, position, size, etc. have changed, but I don't believe you can influence the focus directly.
Additionally, at least on Windows, there's not really any "legitimate" way to accomplish this, in the sense that there's no way to guarantee that any one program in Windows is always above every other. You can try to attempt various things like detecting when the windows focus is lost (NativeWindow.FocusChanged
), and then call some user32 functions like SetForegroundWindow, BringWindowToTop, etc, but it's an uphill battle and not really advisable. It's possible that other operating system's / window managers have different support for this.
Upvotes: 1