示例#1
0
            private bool DrawOntoCanvas(IntPtr handle, SKCanvas Canvas, bool forcerender = false)
            {
                var hwnd = Hwnd.ObjectFromHandle(handle);

                var x        = 0;
                var y        = 0;
                var wasdrawn = false;

                XplatUI.driver.ClientToScreen(hwnd.client_window, ref x, ref y);

                var width         = 0;
                var height        = 0;
                var client_width  = 0;
                var client_height = 0;


                if (hwnd.hwndbmp != null && hwnd.Mapped && hwnd.Visible && !hwnd.zombie)
                {
                    // setup clip
                    var parent = hwnd;
                    Canvas.ClipRect(
                        SKRect.Create(0, 0, Screen.PrimaryScreen.Bounds.Width * 2,
                                      Screen.PrimaryScreen.Bounds.Height * 2), (SKClipOperation)5);

                    while (parent != null)
                    {
                        var xp = 0;
                        var yp = 0;
                        XplatUI.driver.ClientToScreen(parent.client_window, ref xp, ref yp);

                        Canvas.ClipRect(SKRect.Create(xp, yp, parent.Width, parent.Height),
                                        SKClipOperation.Intersect);

                        parent = parent.parent;
                    }

                    Monitor.Enter(XplatUIMine.paintlock);

                    if (hwnd.ClientWindow != hwnd.WholeWindow)
                    {
                        var frm = Control.FromHandle(hwnd.ClientWindow) as Form;

                        Hwnd.Borders borders = new Hwnd.Borders();

                        if (frm != null)
                        {
                            borders = Hwnd.GetBorders(frm.GetCreateParams(), null);

                            Canvas.ClipRect(
                                SKRect.Create(0, 0, Screen.PrimaryScreen.Bounds.Width * 2,
                                              Screen.PrimaryScreen.Bounds.Height * 2), (SKClipOperation)5);
                        }

                        if (Canvas.DeviceClipBounds.Width > 0 &&
                            Canvas.DeviceClipBounds.Height > 0)
                        {
                            if (hwnd.DrawNeeded || forcerender)
                            {
                                if (hwnd.hwndbmpNC != null)
                                {
                                    Canvas.DrawImage(hwnd.hwndbmpNC,
                                                     new SKPoint(x - borders.left, y - borders.top), paint);
                                }

                                Canvas.ClipRect(
                                    SKRect.Create(x, y, hwnd.width - borders.right - borders.left,
                                                  hwnd.height - borders.top - borders.bottom), SKClipOperation.Intersect);

                                Canvas.DrawImage(hwnd.hwndbmp,
                                                 new SKPoint(x, y), paint);

                                wasdrawn = true;
                            }

                            hwnd.DrawNeeded = false;
                        }
                        else
                        {
                            Monitor.Exit(XplatUIMine.paintlock);
                            return(true);
                        }
                    }
                    else
                    {
                        if (Canvas.DeviceClipBounds.Width > 0 &&
                            Canvas.DeviceClipBounds.Height > 0)
                        {
                            if (hwnd.DrawNeeded || forcerender)
                            {
                                Canvas.DrawImage(hwnd.hwndbmp,
                                                 new SKPoint(x + 0, y + 0), paint);

                                wasdrawn = true;
                            }

                            hwnd.DrawNeeded = false;

/*
 *                      surface.Canvas.DrawText(Control.FromHandle(hwnd.ClientWindow).Name,
 *                          new SKPoint(x, y + 15),
 *                          new SKPaint() {Color = SKColor.Parse("55ffff00")});
 *                      /*surface.Canvas.DrawText(hwnd.ClientWindow.ToString(), new SKPoint(x,y+15),
 *                          new SKPaint() {Color = SKColor.Parse("ffff00")});*/
                        }
                        else
                        {
                            Monitor.Exit(XplatUIMine.paintlock);
                            return(true);
                        }
                    }

                    Monitor.Exit(XplatUIMine.paintlock);
                }

                //surface.Canvas.DrawText(x + " " + y, x, y+10, new SKPaint() { Color =  SKColors.Red});

                if (hwnd.Mapped && hwnd.Visible)
                {
                    IEnumerable <Hwnd> children;
                    lock (Hwnd.windows)
                        children = Hwnd.windows.OfType <System.Collections.DictionaryEntry>()
                                   .Where(hwnd2 =>
                        {
                            var Key   = (IntPtr)hwnd2.Key;
                            var Value = (Hwnd)hwnd2.Value;
                            if (Value.ClientWindow == Key && Value.Parent == hwnd && Value.Visible &&
                                Value.Mapped && !Value.zombie)
                            {
                                return(true);
                            }
                            return(false);
                        }).Select(a => (Hwnd)a.Value).ToArray();

                    children = children.OrderBy((hwnd2) =>
                    {
                        var info = XplatUIMine.GetInstance().GetZOrder(hwnd2.client_window);
                        if (info.top)
                        {
                            return(1000);
                        }
                        if (info.bottom)
                        {
                            return(0);
                        }
                        return(500);
                    });

                    foreach (var child in children)
                    {
                        DrawOntoCanvas(child.ClientWindow, Canvas, true);
                    }
                }

                return(true);
            }
        private void SkCanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintGLSurfaceEventArgs e) // .SKPaintSurfaceEventArgs // SKPaintGLSurfaceEventArgs
        {
            try
            {
                var surface = e.Surface;

                e.Surface.Canvas.Clear(SKColors.Gray);

                surface.Canvas.DrawCircle(0, 0, 50, new SKPaint()
                {
                    Color = SKColor.Parse("ff0000")
                });

                e.Surface.Canvas.Scale((float)scale.Width, (float)scale.Height);

                Func <IntPtr, bool> func = null;
                func = (handle) =>
                {
                    var hwnd = Hwnd.ObjectFromHandle(handle);

                    var x = 0;
                    var y = 0;

                    XplatUI.driver.ClientToScreen(hwnd.client_window, ref x, ref y);

                    var width         = 0;
                    var height        = 0;
                    var client_width  = 0;
                    var client_height = 0;


                    if (hwnd.hwndbmp != null && hwnd.Mapped && hwnd.Visible && !hwnd.zombie)
                    {
                        // setup clip
                        var parent = hwnd;
                        surface.Canvas.ClipRect(
                            SKRect.Create(0, 0, Screen.PrimaryScreen.Bounds.Width,
                                          Screen.PrimaryScreen.Bounds.Height), (SKClipOperation)5);

                        while (parent != null)
                        {
                            var xp = 0;
                            var yp = 0;
                            XplatUI.driver.ClientToScreen(parent.client_window, ref xp, ref yp);

                            surface.Canvas.ClipRect(SKRect.Create(xp, yp, parent.Width, parent.Height),
                                                    SKClipOperation.Intersect);

                            /*
                             * surface.Canvas.DrawRect(xp, yp, parent.Width, parent.Height,
                             *  new SKPaint()
                             *  {
                             *
                             *      Color = new SKColor(255, 0, 0),
                             *      Style = SKPaintStyle.Stroke
                             *
                             *
                             *  });
                             */
                            parent = parent.parent;
                        }

                        Monitor.Enter(XplatUIMine.paintlock);

                        if (hwnd.ClientWindow != hwnd.WholeWindow)
                        {
                            var frm = Control.FromHandle(hwnd.ClientWindow) as Form;

                            Hwnd.Borders borders = new Hwnd.Borders();

                            if (frm != null)
                            {
                                borders = Hwnd.GetBorders(frm.GetCreateParams(), null);

                                surface.Canvas.ClipRect(
                                    SKRect.Create(0, 0, Screen.PrimaryScreen.Bounds.Width,
                                                  Screen.PrimaryScreen.Bounds.Height), (SKClipOperation)5);
                            }

                            if (surface.Canvas.DeviceClipBounds.Width > 0 &&
                                surface.Canvas.DeviceClipBounds.Height > 0)
                            {
                                surface.Canvas.DrawImage(hwnd.hwndbmpNC,
                                                         new SKPoint(x - borders.left, y - borders.top),
                                                         new SKPaint()
                                {
                                    FilterQuality = SKFilterQuality.Low
                                });

                                surface.Canvas.ClipRect(
                                    SKRect.Create(x, y, hwnd.width - borders.right - borders.left,
                                                  hwnd.height - borders.top - borders.bottom), SKClipOperation.Intersect);

                                surface.Canvas.DrawImage(hwnd.hwndbmp,
                                                         new SKPoint(x, y),
                                                         new SKPaint()
                                {
                                    FilterQuality = SKFilterQuality.Low
                                });
                            }
                            else
                            {
                                Monitor.Exit(XplatUIMine.paintlock);
                                return(true);
                            }
                        }
                        else
                        {
                            if (surface.Canvas.DeviceClipBounds.Width > 0 &&
                                surface.Canvas.DeviceClipBounds.Height > 0)
                            {
                                surface.Canvas.DrawImage(hwnd.hwndbmp,
                                                         new SKPoint(x + 0, y + 0),
                                                         new SKPaint()
                                {
                                    FilterQuality = SKFilterQuality.Low
                                });
                            }
                            else
                            {
                                Monitor.Exit(XplatUIMine.paintlock);
                                return(true);
                            }
                        }

                        Monitor.Exit(XplatUIMine.paintlock);
                    }

                    //surface.Canvas.DrawText(x + " " + y, x, y+10, new SKPaint() { Color =  SKColors.Red});

                    if (hwnd.Mapped && hwnd.Visible)
                    {
                        IEnumerable <Hwnd> children;
                        lock (Hwnd.windows)
                            children = Hwnd.windows.OfType <System.Collections.DictionaryEntry>()
                                       .Where(hwnd2 =>
                            {
                                var Key   = (IntPtr)hwnd2.Key;
                                var Value = (Hwnd)hwnd2.Value;
                                if (Value.ClientWindow == Key && Value.Parent == hwnd && Value.Visible &&
                                    Value.Mapped && !Value.zombie)
                                {
                                    return(true);
                                }
                                return(false);
                            }).Select(a => (Hwnd)a.Value).ToArray();

                        foreach (var child in children)
                        {
                            func(child.ClientWindow);
                        }
                    }

                    return(true);
                };

                foreach (Form form in Application.OpenForms.Select(a => a).ToArray())
                {
                    if (form.IsHandleCreated)
                    {
                        if (form is MainV2 && form.WindowState != FormWindowState.Maximized)
                        {
                            form.WindowState = FormWindowState.Maximized;
                        }

                        if (form.WindowState == FormWindowState.Maximized)
                        {
                            var border = Hwnd.GetBorders(form.GetCreateParams(), null);

                            //XplatUI.driver.SetWindowPos(form.Handle, 0, 0, (int) Screen.PrimaryScreen.Bounds.Width + border.right + border.left,                            (int) Screen.PrimaryScreen.Bounds.Height + border.top + border.bottom);
                        }
                        else
                        {
                            if (form.Location.X < 0 || form.Location.Y < 0)
                            {
                                form.Location = new Point(Math.Max(form.Location.X, 0), Math.Max(form.Location.Y, 0));
                            }

                            var border = Hwnd.GetBorders(form.GetCreateParams(), null);

                            if (form.Size.Width > Screen.PrimaryScreen.Bounds.Width ||
                                form.Size.Height > Screen.PrimaryScreen.Bounds.Height)
                            {
                                //form.Size = new System.Drawing.Size((int) Screen.PrimaryScreen.Bounds.Width, (int) Screen.PrimaryScreen.Bounds.Height);
                                XplatUI.driver.SetWindowPos(form.Handle, 0, 0, (int)Screen.PrimaryScreen.Bounds.Width,
                                                            (int)Screen.PrimaryScreen.Bounds.Height);
                            }
                        }

                        try
                        {
                            func(form.Handle);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                }

                IEnumerable <Hwnd> menu;
                lock (Hwnd.windows)
                    menu = Hwnd.windows.Values.OfType <Hwnd>()
                           .Where(hw => hw.topmost && hw.Mapped && hw.Visible).ToArray();
                foreach (Hwnd hw in menu)
                {
                    if (hw.topmost && hw.Mapped && hw.Visible)
                    {
                        var ctlmenu = Control.FromHandle(hw.ClientWindow);
                        if (ctlmenu != null)
                        {
                            func(hw.ClientWindow);
                        }
                    }
                }

                {
                    surface.Canvas.ClipRect(
                        SKRect.Create(0, 0, Screen.PrimaryScreen.Bounds.Width,
                                      Screen.PrimaryScreen.Bounds.Height), (SKClipOperation)5);

                    var path = new SKPath();

                    path.MoveTo(cursorPoints.First());
                    cursorPoints.ForEach(a => path.LineTo(a));
                    path.Transform(new SKMatrix(1, 0, XplatUI.driver.MousePosition.X, 0, 1,
                                                XplatUI.driver.MousePosition.Y, 0, 0, 1));

                    surface.Canvas.DrawPath(path,
                                            new SKPaint()
                    {
                        Color = SKColors.White, Style = SKPaintStyle.Fill, StrokeJoin = SKStrokeJoin.Miter
                    });
                    surface.Canvas.DrawPath(path,
                                            new SKPaint()
                    {
                        Color = SKColors.Black, Style = SKPaintStyle.Stroke, StrokeJoin = SKStrokeJoin.Miter, IsAntialias = true
                    });
                }

                surface.Canvas.Flush();

                return;

                surface.Canvas.ClipRect(new SKRect(0, 0, Screen.PrimaryScreen.Bounds.Right,
                                                   Screen.PrimaryScreen.Bounds.Bottom), (SKClipOperation)5);

                surface.Canvas.DrawText("PixelScreenSize " + Device.Info.PixelScreenSize.ToString(),
                                        new SKPoint(50, 10), new SKPaint()
                {
                    Color = SKColor.Parse("ffff00")
                });


                surface.Canvas.DrawText("screen " + Screen.PrimaryScreen.ToString(), new SKPoint(50, 30),
                                        new SKPaint()
                {
                    Color = SKColor.Parse("ffff00")
                });

                int mx = 0, my = 0;
                XplatUI.driver.GetCursorPos(IntPtr.Zero, out mx, out my);

                surface.Canvas.DrawText("mouse " + XplatUI.driver.MousePosition.ToString(), new SKPoint(50, 50),
                                        new SKPaint()
                {
                    Color = SKColor.Parse("ffff00")
                });
                surface.Canvas.DrawText(mx + " " + my, new SKPoint(50, 70),
                                        new SKPaint()
                {
                    Color = SKColor.Parse("ffff00")
                });


                if (Application.OpenForms.Count > 0 &&
                    Application.OpenForms[Application.OpenForms.Count - 1].IsHandleCreated)
                {
                    var x = XplatUI.driver.MousePosition.X;
                    var y = XplatUI.driver.MousePosition.Y;

                    XplatUI.driver.ScreenToClient(Application.OpenForms[Application.OpenForms.Count - 1].Handle, ref x,
                                                  ref y);

                    var ctl = XplatUIMine.FindControlAtPoint(Application.OpenForms[Application.OpenForms.Count - 1],
                                                             new Point(x, y));
                    if (ctl != null)
                    {
                        XplatUI.driver.ScreenToClient(ctl.Handle, ref mx, ref my);
                        surface.Canvas.DrawText("client " + mx + " " + my, new SKPoint(50, 90),
                                                new SKPaint()
                        {
                            Color = SKColor.Parse("ffff00")
                        });

                        surface.Canvas.DrawText(ctl?.ToString(), new SKPoint(50, 130),
                                                new SKPaint()
                        {
                            Color = SKColor.Parse("ffff00")
                        });

                        var hwnd = Hwnd.ObjectFromHandle(ctl.Handle);

                        surface.Canvas.DrawText(ctl.Location.ToString(), new SKPoint(50, 150),
                                                new SKPaint()
                        {
                            Color = SKColor.Parse("ffff00")
                        });
                    }
                }

                surface.Canvas.DrawText("!", new SKPoint(XplatUI.driver.MousePosition.X,
                                                         XplatUI.driver.MousePosition.Y),
                                        new SKPaint()
                {
                    Color = SKColor.Parse("ffff00")
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
            }
        }