示例#1
0
        // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2012/20121101/20121102
        //public delegate void LaunchOptions();


        public static void Launch <T>(Func <T> Create, Action <WindowInfo <T> > Launching = null) where T : Control
        {
            // how many examples have we referening this method? how may times have we used it?

            #region UnhandledException
            AppDomain.CurrentDomain.UnhandledException +=
                (sender, e) =>
            {
                Console.WriteLine("UnhandledException:");
                Console.WriteLine(e);
            };
            #endregion


            var CreateWindow = default(Func <WindowInfo <T> >);
            var Windows      = new List <WindowInfo <T> >();

            CreateWindow =
                delegate
            {
                var wi = new WindowInfo <T>();

                Windows.Add(wi);

                wi.Others = Windows.Except(new[] { wi });

                wi.CreateWindow =
                    delegate
                {
                    var ci = CreateWindow();

                    ci.Window.Owner = wi.Window;

                    if (Launching != null)
                    {
                        Launching(ci);
                    }

                    ci.Window.Show();

                    return(ci);
                };

                //var ActualSize = new Canvas();

                var c = Create();
                //var c = Create().AttachTo(ActualSize);
                //wi.Content = c;

                //ActualSize.SizeChanged +=
                //    delegate
                //    {
                //        c.SizeTo(ActualSize.ActualWidth, ActualSize.ActualHeight);
                //    };

                var w = new Form
                {
                    // CLR only.
                    Name = "ApplicationForm"
                };

                w.Load +=
                    delegate
                {
                    Console.WriteLine("ApplicationForm.Load");
                };

                var f = w;

                wi.Window = w;

                // how big to make the form?
                f.ClientSize         = new System.Drawing.Size(c.Width, c.Height);
                f.ClientSizeChanged +=
                    delegate
                {
                    c.Width  = f.ClientSize.Width;
                    c.Height = f.ClientSize.Height;
                };

                w.Text  = c.GetType().Name;
                w.Text += " | F2 - Spawn, F11 - Fullscreen";


                // Additional information: Top-level control cannot be added to a control.
                f.Controls.Add(c);

                //w.Width = c.Width;
                //w.Height = c.Height;


                var ExitFullscreen = default(Action);

                #region ToFullscreen
                Action ToFullscreen =
                    () =>
                {
                    if (ExitFullscreen != null)
                    {
                        return;
                    }

                    var x = new { w.TopMost, w.WindowState, w.FormBorderStyle };


                    ExitFullscreen =
                        () =>
                    {
                        ExitFullscreen = null;

                        w.FormBorderStyle = x.FormBorderStyle;
                        w.WindowState     = x.WindowState;
                        w.TopMost         = x.TopMost;
                    };

                    w.TopMost         = true;
                    w.FormBorderStyle = FormBorderStyle.None;
                    w.WindowState     = FormWindowState.Normal;
                    w.WindowState     = FormWindowState.Maximized;
                };
                #endregion


                #region LostFocus
                w.LostFocus +=
                    delegate
                {
                    if (ExitFullscreen != null)
                    {
                        ExitFullscreen();

                        return;
                    }
                };
                #endregion

                #region KeyUp
                w.KeyPreview = true;
                f.KeyUp     +=
                    (s, e) =>
                {
                    if (e.KeyData == Keys.Escape)
                    {
                        if (ExitFullscreen != null)
                        {
                            ExitFullscreen();

                            //e.Handled = true;
                            return;
                        }
                    }

                    if (e.KeyData == Keys.F2)
                    {
                        wi.CreateWindow();

                        //e.Handled = true;
                        return;
                    }

                    if (e.KeyData == Keys.F11)
                    {
                        if (ExitFullscreen != null)
                        {
                            ExitFullscreen();

                            //e.Handled = true;
                            return;
                        }


                        ToFullscreen();

                        //e.Handled = true;
                        return;
                    }
                };
                #endregion



                return(wi);
            };

            var t = new Thread(
                delegate()
            {
                global::System.Windows.Forms.Application.EnableVisualStyles();


                var wi = CreateWindow();

                if (Launching != null)
                {
                    Launching(wi);
                }

                Application.Run(wi.Window);
                //wi.Window.ShowDialog();
            }
                )
            {
                ApartmentState = ApartmentState.STA
            };

            t.Start();
            t.Join();
        }
        // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2012/20121101/20121102
        //public delegate void LaunchOptions();

        public static void Launch <T>(Func <T> Create, Action <WindowInfo <T> > Launching = null) where T : Canvas
        {
            var CreateWindow = default(Func <WindowInfo <T> >);
            var Windows      = new List <WindowInfo <T> >();

            CreateWindow =
                delegate
            {
                var wi = new WindowInfo <T>();

                Windows.Add(wi);

                wi.Others = Windows.Except(new[] { wi });

                wi.CreateWindow =
                    delegate
                {
                    var ci = CreateWindow();

                    ci.Window.Owner = wi.Window;

                    if (Launching != null)
                    {
                        Launching(ci);
                    }

                    ci.Window.Show();

                    return(ci);
                };

                var CreateContent = Create;
                var ActualSize    = new Canvas();
                var ActualSize2   = new Canvas();

                var c = CreateContent();

                if (double.IsNaN(c.Width))
                {
                }
                else
                {
                    ActualSize.Width  = c.Width;
                    ActualSize.Height = c.Height;
                }

                c.AttachTo(ActualSize);

                // hack
                // Could not load file or assembly 'c:\/util\/jsc\/bin\/ScriptCoreLib.Ultra.Components.Volatile.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
                //var JSCBrandedWindow = Assembly.Load(@"c:\util\jsc\bin\ScriptCoreLib.Ultra.Components.Volatile.dll").GetType("ScriptCoreLib.Avalon.Desktop.JSCBrandedWindow");

                //var branded = (Window)Activator.CreateInstance(JSCBrandedWindow);

                var w = ActualSize.ToWindow();

                if (double.IsNaN(c.Width))
                {
                    w.SizeToContent = SizeToContent.Manual;


                    w.Width  = ScriptApplicationEntryPointAttribute.DefaultWidth;
                    w.Height = ScriptApplicationEntryPointAttribute.DefaultHeight;
                }
                else
                {
                    ActualSize.Width  = c.Width;
                    ActualSize.Height = c.Height;
                }


                ActualSize2.SizeChanged +=
                    delegate
                {
                    c.SizeTo(ActualSize2.ActualWidth, ActualSize2.ActualHeight);
                };

                ActualSize.SizeChanged +=
                    delegate
                {
                    c.SizeTo(ActualSize.ActualWidth, ActualSize.ActualHeight);
                };

                w.SourceInitialized +=
                    delegate
                {
                    if (w.SizeToContent != SizeToContent.Manual)
                    {
                        w.Content = null;

                        w.SizeToContent = SizeToContent.Manual;
                        c.Orphanize().AttachTo(ActualSize2);
                        ActualSize2.AttachTo(w);
                        return;
                    }
                };

                wi.Content = c;
                wi.Window  = w;

                w.Background = Brushes.White;



                w.Title += " | F2 - Spawn, F11 - Fullscreen";

                var ExitFullscreen = default(Action);

                #region ToFullscreen
                Action ToFullscreen =
                    () =>
                {
                    if (ExitFullscreen != null)
                    {
                        return;
                    }

                    var x = new { w.Topmost, w.WindowState, w.WindowStyle };


                    ExitFullscreen =
                        () =>
                    {
                        ExitFullscreen = null;

                        w.WindowStyle = x.WindowStyle;
                        w.WindowState = x.WindowState;
                        w.Topmost     = x.Topmost;
                    };

                    w.Topmost     = true;
                    w.WindowStyle = System.Windows.WindowStyle.None;
                    w.WindowState = System.Windows.WindowState.Normal;
                    w.WindowState = System.Windows.WindowState.Maximized;
                };
                #endregion


                #region Deactivated
                w.Deactivated +=
                    delegate
                {
                    if (ExitFullscreen != null)
                    {
                        ExitFullscreen();

                        return;
                    }
                };
                #endregion


                #region PreviewKeyUp
                w.PreviewKeyUp +=
                    (s, e) =>
                {
                    if (e.Key == Key.Escape)
                    {
                        if (ExitFullscreen != null)
                        {
                            ExitFullscreen();

                            e.Handled = true;
                            return;
                        }
                    }

                    if (e.Key == Key.F2)
                    {
                        wi.CreateWindow();

                        e.Handled = true;
                        return;
                    }

                    if (e.Key == Key.F11)
                    {
                        if (ExitFullscreen != null)
                        {
                            ExitFullscreen();

                            e.Handled = true;
                            return;
                        }


                        ToFullscreen();

                        e.Handled = true;
                        return;
                    }
                };
                #endregion



                return(wi);
            };

            var t = new Thread(
                delegate()
            {
                var wi = CreateWindow();

                if (Launching != null)
                {
                    Launching(wi);
                }

                wi.Window.ShowDialog();
            }
                )
            {
                ApartmentState = ApartmentState.STA
            };

            t.Start();
            t.Join();
        }