Last active
November 7, 2024 14:09
-
-
Save chrisdill/c2ca6a0ccf592c073a8d2aaeaa4adb7f to your computer and use it in GitHub Desktop.
Using raylib from inside a win forms application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
using Raylib_cs; | |
using Color = Raylib_cs.Color; | |
namespace Rayforms | |
{ | |
public class RayForm : Form | |
{ | |
private Panel gamePanel; | |
private bool windowAttached = false; | |
#region WinAPI Entry Points | |
[DllImport("user32.dll")] | |
private static extern IntPtr SetWindowPos(IntPtr handle, IntPtr handleAfter, int x, int y, int cx, int cy, uint flags); | |
[DllImport("user32.dll")] | |
private static extern IntPtr SetParent(IntPtr child, IntPtr newParent); | |
[DllImport("user32.dll")] | |
private static extern IntPtr ShowWindow(IntPtr handle, int command); | |
#endregion | |
public RayForms() | |
{ | |
Size = new Size(1024, 700); | |
Text = "Rayforms"; | |
gamePanel = new Panel(); | |
gamePanel.Size = new Size(800, 500); | |
gamePanel.Location = new Point(50, 50); | |
Button button = new Button(); | |
button.Text = "Attach window"; | |
button.Size = new Size(150, 20); | |
button.Location = new Point( | |
(Size.Width / 2) - (button.Size.Width / 2), | |
gamePanel.Location.Y + gamePanel.Size.Height + 10 | |
); | |
button.Click += new EventHandler(ClickedButton); | |
Controls.Add(button); | |
Controls.Add(gamePanel); | |
} | |
private void ClickedButton(object sender, EventArgs e) | |
{ | |
if (!windowAttached) | |
{ | |
// new Thread(Test).Start(); | |
Test(); | |
} | |
} | |
private void Test() | |
{ | |
Raylib.SetConfigFlags(ConfigFlag.FLAG_WINDOW_UNDECORATED); | |
Raylib.InitWindow(800, 480, "Rayforms test"); | |
Raylib.SetTargetFPS(60); | |
IntPtr winHandle = Raylib.GetWindowHandle(); | |
Invoke(new Action(() => | |
{ | |
SetWindowPos(winHandle, Handle, 0, 0, 0, 0, 0x0401 /*NOSIZE | SHOWWINDOW */); | |
SetParent(winHandle, gamePanel.Handle); | |
ShowWindow(winHandle, 1); | |
windowAttached = true; | |
})); | |
while (!Raylib.WindowShouldClose()) // Detect window close button or ESC key | |
{ | |
// Update | |
//---------------------------------------------------------------------------------- | |
// TODO: Update your variables here | |
//---------------------------------------------------------------------------------- | |
// Draw | |
//---------------------------------------------------------------------------------- | |
Raylib.BeginDrawing(); | |
Raylib.ClearBackground(Color.RAYWHITE); | |
Raylib.DrawText("Congrats! You created your first window!", 190, 200, 20, Color.MAROON); | |
Raylib.DrawText(rl.GetFrameTime().ToString(), 100, 10, 15, Color.MAROON); | |
Raylib.DrawFPS(10, 10); | |
Raylib.EndDrawing(); | |
//---------------------------------------------------------------------------------- | |
} | |
Raylib.CloseWindow(); | |
} | |
public static void Run() | |
{ | |
Application.Run(new RayForm()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was in the tests folder in raylib-cs. Tests changed to a single project so putting this demo here for now.
Inspired by the win forms example from SDL2-CS - https://gist.github.com/flibitijibibo/cf282bfccc1eaeb47550