Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C#

Java/.NET Integration as Simple as Possible

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
5 Jul 2006CPOL3 min read 161.1K   2.3K   39  
Describes the simplest way to embed .NET controls into a Java GUI (without using COM or any other technology).
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CSharpInJava
{
    public class CreateParams
    {
        private string caption;
        private string className;
        private int classStyle;
        private int exStyle;
        private int height;
        
        private IntPtr parent;
        private int width;
        private int x;
        private int y;

        public CreateParams(){}

        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }
        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }

        public int ClassStyle
        {
            get { return classStyle; }
            set { classStyle = value; }
        }

        public int ExStyle
        {
            get { return exStyle; }
            set { exStyle = value; }
        }

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        public IntPtr Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }
    }

	public class FrameWindow : Control, IWin32Window
	{
        [DllImport("kernel32.dll", EntryPoint="GetModuleHandleW", SetLastError=true)]
        internal static extern IntPtr GetModuleHandle(string lpszModule);
        [DllImport("kernel32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nItem);
        [DllImport("kernel32.dll")]
        public static extern void SetWindowLong(IntPtr hWnd, int GetWindowLongParam, int nValue);
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetCapture();
        [DllImport("user32.dll", EntryPoint="CreateWindowExW", SetLastError=true)]
        public static extern IntPtr CreateWindowEx(
            int dwExStyle, 
            string lpClassName, 
            string lpWindowName, 
            int dwStyle, 
            int x, 
            int y, 
            int nWidth, 
            int nHeight, 
            IntPtr hWndParent, 
            IntPtr hMenu, 
            IntPtr hInstance, 
            IntPtr lpParam 
            ); 
        [DllImport("kernel32.dll")]
        public static extern bool SetFocus(IntPtr hWnd);
        [DllImport("kernel32.dll")]
        public extern static int SetWindowPos (
            IntPtr hWnd, int pos,
            int X, int Y, int cx, int cy, int uFlags);
        [DllImport("kernel32.dll")] 
        public static extern IntPtr DestroyWindow(IntPtr hWnd);

        private string m_name = String.Empty;
        private IntPtr m_hwnd = IntPtr.Zero;
        private IntPtr m_parenthwnd = IntPtr.Zero;
        private IntPtr m_childhwnd = IntPtr.Zero;
        private IntPtr m_instance;
        private object m_tag;
        private CreateParams m_createParams;
        private BorderStyle m_border = BorderStyle.None;

        public static void UpdateWindowStyle(IntPtr hwnd, int RemoveStyle, int AddStyle) 
        {
            int style = GetWindowLong(hwnd, -16);
            style &= ~RemoveStyle;
            style |= AddStyle;
            SetWindowLong(hwnd, -16, style);
            SetWindowPos(hwnd, 0, 0, 0, 0, 0, 0x0037);
        }

		public FrameWindow(IntPtr parenthwnd)
		{
            m_parenthwnd = parenthwnd;
            m_instance = GetModuleHandle(null);
            m_createParams = new CreateParams();
            m_createParams.ClassStyle = (int)0x10000000;
		}

        public BorderStyle BorderStyle
        {
            get { return m_border; }
            set {
                m_border = value;
                if(value==BorderStyle.None)
                {
                    UpdateWindowStyle(Handle, (int)0x00800000, 0);
                }
                else
                {
                    UpdateWindowStyle(Handle, 0,(int)0x00800000);
                }
            }
        }
        protected internal IntPtr ChildHandle
        {
            get { return m_childhwnd; }
        }	
        public new bool Created
        {
            get { return (m_childhwnd != IntPtr.Zero); }
        }
        protected new CreateParams CreateParams 
        {
            get { return m_createParams; }
        }
        public new IntPtr Handle
        {
            get {
                if(m_hwnd==IntPtr.Zero)
                {
                    this.Capture = true;
                    m_hwnd = GetCapture();
                    this.Capture = false;
                }
                return m_hwnd;
            }
        }
        protected IntPtr Instance
        {
            get { return m_instance; }
        }
        public new string Name
        {
            get {
                if(m_name == string.Empty)
                {
                    m_name = GetName(this);
                }
                return m_name;
            }
        }
        public static string GetName(Control control)
        {
            FieldInfo[] fi = control.Parent.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance |
                BindingFlags.Public | BindingFlags.IgnoreCase);

            foreach(FieldInfo f in fi)
            {
                if(f.GetValue(control.Parent).Equals(control))
                    return f.Name;
            }

            return string.Empty;
        }
        public new object Tag
        {
            get { return m_tag; }
            set { m_tag = value; }
        }
        public new void CreateControl()
        {
            if(m_childhwnd != IntPtr.Zero)
            {
                DestroyWindow(m_childhwnd);
            }
            CreateParams.Width = this.Width;
            CreateParams.Height = this.Height;
			String err;
			try
			{
				m_childhwnd = CreateWindowEx(m_createParams.ExStyle, m_createParams.ClassName, m_createParams.Caption, (int)m_createParams.ClassStyle, m_createParams.X, m_createParams.Y, m_createParams.Width, m_createParams.Height, (IntPtr)m_parenthwnd/*m_createParams.Parent*/, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
			}
			catch(Exception e)
			{
				err = e.Message;
			}
        }
        protected override void OnGotFocus(EventArgs e)
        {
            if(ChildHandle != IntPtr.Zero)
            {
                SetFocus(ChildHandle);
            }
        }
        protected override void OnParentChanged(EventArgs e)
        {
            CreateControl();
            base.OnParentChanged (e);
        }
        protected override void OnResize(EventArgs e)
        {
            if(ChildHandle != IntPtr.Zero)
            {
                SetWindowPos(ChildHandle, 0, 0, 0, this.Width, this.Height, 0);
            }
            base.OnResize (e);
        }
        public new void SetBounds(int x, int y, int width, int height)
        {
            this.Bounds = new Rectangle(x, y, width, height);
        }
        protected override void Dispose(bool disposing)
        {
            if(m_childhwnd != IntPtr.Zero)
            {
                DestroyWindow(m_childhwnd);
                m_childhwnd = IntPtr.Zero;
            }
            base.Dispose (disposing);
        }
        public static IntPtr GetHandle(Control c)
        {
            c.Capture = true;
            IntPtr handle = GetCapture();
            c.Capture = false;
            return handle;
        }
        public static Control GetControlByName(Control parent, string name)
        {
            FieldInfo info = parent.GetType().GetField(name,
                BindingFlags.NonPublic | BindingFlags.Instance |
                BindingFlags.Public | BindingFlags.IgnoreCase);
            if(info == null) return null;
            object o = info.GetValue(parent);
            return (Control)o;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Javain Ltd
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions