I wrote this little control a few months ago. It is Win32 only, and lets
you embed a System.Windows.Forms control into a Gtk# widget. I'm sure
the same approach can be followed to make it run on X, but I'm not
familiar enough with X to accomplish it.

I'm sending it here in case anybody has interest in finishing it up and
committing it to GtkSharp.

There are a few bugs. Right click handling does not work, for example.
It also can't be typed into properly, for the same reason right click
handling doesn't work, probably.

Somebody please help me fix it up!
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ISIS.Private.App
{
    
    /// <summary>
    /// Provides a System.Windows.Form control.
    /// </summary>
    public class GtkFormsPlug : Gtk.DrawingArea
    {

        private Control control;

        /// <summary>
        /// Creates a new instance.
        /// </summary>
        public GtkFormsPlug()
        {
            DoubleBuffered = false;
            Realized += this_Realized;
            SizeAllocated += this_SizeAllocated;

            // Our bridge control.
            control = new Control();
            control.Location = new Point(0, 0);
        }

        /// <summary>
        /// Invoked when the <see cref="GdkWindow"/> has been created. That is, when an OS window has been allocated.
        /// We reparent our peer <see cref="Control"/> into the new OS window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void this_Realized(object sender, EventArgs e)
        {
            control.Show();
            SetParent(control.Handle, gdk_win32_drawable_get_handle(GdkWindow.Handle));
            QueueResize();
        }

        /// <summary>
        /// Invoked when Gtk knows the size of this child widget. Here we force the size of our peer <see
        /// cref="Control"/>.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="args"></param>
        private void this_SizeAllocated(object o, Gtk.SizeAllocatedArgs args)
        {
            control.Size = new Size(args.Allocation.Width, args.Allocation.Height);
        }

        /// <summary>
        /// Gets a reference to the peer control. Users of the <see cref="GtkWinFormsPlug"/> should add their Windows
        /// Forms <see cref="Control"/> instances as children of this.
        /// </summary>
        public Control Control
        {
            get { return control; }
        }

        [DllImport("libgdk-win32-2.0-0.dll")]
        private static extern IntPtr gdk_win32_drawable_get_handle(IntPtr w);

        [DllImport("user32.dll")]
        private static extern IntPtr SetParent(IntPtr child, IntPtr parent);

    }

}
_______________________________________________
Gtk-sharp-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Reply via email to