Laden...

Howto: SDL Fenster in WinForm

Erstellt von egrath vor 17 Jahren Letzter Beitrag vor 16 Jahren 5.434 Views
egrath Themenstarter:in
871 Beiträge seit 2005
vor 17 Jahren
Howto: SDL Fenster in WinForm

Hallo,

nachdem mich das schon lange genervt hat dass es nicht so ohne weiteres möglich ist ein SDL Fenster innerhalb von Windows Forms darzustellen hab ich mich mal rangesetzt und das versucht hinzukriegen - und funktioniert 😉

Vielleicht kann das ja mal einer von euch brauchen:


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using Tao.Sdl;

namespace TaoInside
{
    public class Program : Form
    {
        #region Native Win32 Functions
        [DllImport( "user32.dll", SetLastError = true )]
        static extern IntPtr SetParent( IntPtr child, IntPtr newParent );

        [DllImport( "user32.dll", SetLastError=true)]
        static extern IntPtr FindWindow( string className, string windowName );

        [DllImport( "user32.dll", SetLastError = true )]
        static extern bool ShowWindow( IntPtr handle, ShowWindowCommand command );        

        [DllImport( "user32.dll", SetLastError=true )]
        static extern bool SetWindowPos( IntPtr handle, IntPtr handleAfter, int x, int y, int cx, int cy, SetWindowPosFlags flags );

        private enum ShowWindowCommand : int
        {
            SW_HIDE = 0,
            SW_SHOW = 5,
            SW_SHOWNA = 8,
            SW_SHOWNORMAL = 1,
            SW_SHOWMAXIMIZED = 3
        }

        private enum SetWindowPosFlags : uint
        {
            SWP_SHOWWINDOW = 0x0400,
            SWP_NOSIZE = 0x0001
        }
        #endregion

        private IntPtr m_SdlWindowHandle;
        private IntPtr m_SdlSurface;
        private Panel m_SdlPanel;

        private int pos = 0;

        public static void Main( string[] args )
        {
            Application.Run( new Program() );
        }

        public Program()
        {
            this.Size = new Size( 800, 600 );

            // Create the Panel which will hold the SDL Window
            m_SdlPanel = new Panel();
            m_SdlPanel.Size = new Size( 640, 480 );
            m_SdlPanel.Location = new Point( 10, 10 );

            // Create the Button which triggers drawing
            Button button = new Button();
            button.Text = "Draw";
            button.Click += new EventHandler( this.ButtonClickHandler );
            button.Location = new Point( m_SdlPanel.Location.X + m_SdlPanel.Size.Width + 10, 10 );

            // Add the Controls
            this.Controls.Add( button );
            this.Controls.Add( m_SdlPanel );

            // Create the SDL WIndow
            CreateSdlWindow();

            /*
            IntPtr processWindowHandle = FindWindow( null, "Untitled - Notepad" );
            SetParent( processWindowHandle, panel.Handle );
            ShowWindow( processWindowHandle, ShowWindowCommand.SW_SHOWMAXIMIZED );
            */
        }

        private void ButtonClickHandler( object sender, EventArgs e )
        {
            // Draw a rectangle to our surface
            IntPtr videoInfoPtr = Sdl.SDL_GetVideoInfo();
            Sdl.SDL_VideoInfo videoInfo = ( Sdl.SDL_VideoInfo ) Marshal.PtrToStructure( videoInfoPtr, typeof( Sdl.SDL_VideoInfo ));

            int red = Sdl.SDL_MapRGB( videoInfo.vfmt, 255, 0, 0 );
            Sdl.SDL_Rect rect = new Sdl.SDL_Rect(( short ) pos, ( short ) pos, 20, 20 );
            Sdl.SDL_FillRect( m_SdlSurface, ref rect, red );

            Sdl.SDL_UpdateRect( m_SdlSurface, 0, 0, m_SdlPanel.Size.Width, m_SdlPanel.Size.Height );

            pos += 10;
        }

        private void CreateSdlWindow()
        {
            // Initialize SDL
            Sdl.SDL_Init( Sdl.SDL_INIT_VIDEO );
            m_SdlSurface = Sdl.SDL_SetVideoMode( m_SdlPanel.Size.Width, m_SdlPanel.Size.Height, 32, Sdl.SDL_HWSURFACE | Sdl.SDL_NOFRAME );

            // Retrieve the Handle to the SDL Window
            Sdl.SDL_SysWMinfo_Windows wmInfo;
            Sdl.SDL_GetWMInfo( out wmInfo );
            m_SdlWindowHandle = new IntPtr( wmInfo.window );

            // Set the Window Position to 0/0
            SetWindowPos( m_SdlWindowHandle, this.Handle, 0, 0, 0, 0, ( SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW ));

            // Make the SDL Window the child of our Panel
            SetParent( m_SdlWindowHandle, m_SdlPanel.Handle );
            ShowWindow( m_SdlWindowHandle, ShowWindowCommand.SW_SHOWNORMAL );
        }
    } 
}

Grüsse, Egon

S
125 Beiträge seit 2005
vor 16 Jahren

Hi.

Vielleicht ein wenig spät, aber das sollte eine einfachere Lösung sein:

SDL SurfaceControl

CU

SGT_BOB

*************************
Ich bin root, ich darf das... 😜
root>_
*************************