示例#1
0
        public static Graphics FromHwnd(IntPtr hwnd, bool client)
        {
            if (hwnd == IntPtr.Zero)
            {
                var scaleFactor = NSScreen.MainScreen.BackingScaleFactor;
                var context     = new CGBitmapContext(IntPtr.Zero, 1, 1, 8, 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
                context.ScaleCTM(scaleFactor, -scaleFactor);
                return(new Graphics(context));
            }

            Graphics g;
            var      obj  = ObjCRuntime.Runtime.GetNSObject(hwnd);
            var      view = obj as NSView;

            if (view == null && obj is NSWindow && ((NSWindow)obj).GraphicsContext != null)
            {
                g = new Graphics(((NSWindow)obj).GraphicsContext);
            }
            else if (NSView.FocusView() == view)
            {
                if (NSGraphicsContext.CurrentContext == null)
                {
                    return(FromHwnd(IntPtr.Zero, false));
                }
                g = Graphics.FromCurrentContext();
            }
            else if (view.LockFocusIfCanDraw())
            {
                if (NSGraphicsContext.CurrentContext == null)
                {
                    return(FromHwnd(IntPtr.Zero, false));
                }
                g             = Graphics.FromCurrentContext();
                g.focusedView = view;
            }
            else if (view.Window != null && view.Window.GraphicsContext != null)
            {
                g = new Graphics(view.Window.GraphicsContext);
            }
            else
            {
                return(Graphics.FromImage(new Bitmap(1, 1)));
            }

            if (client)
            {
                if (view is IClientView clientView)
                {
                    var clientBounds = clientView.ClientBounds;
                    g.context.ClipToRect(clientBounds);
                    g.context.TranslateCTM(clientBounds.Left, clientBounds.Top);
                    g.context.SaveState();
                    g.hasClientTransform = true;
                }
            }

            return(g);
        }
示例#2
0
        internal static INativeContex GetCGContextForView(IntPtr handle)
        {
//			IntPtr context = IntPtr.Zero;
            NSView focusWindow = null;
            // JV - je to OK??
            //NSObject whoWrapper = NSObject.Lookup (handle);
            NSObject whoWrapper    = MonoMac.ObjCRuntime.Runtime.GetNSObject(handle);
            NSView   viewWrapper   = whoWrapper as NSView;
            NSWindow windowWrapper = null;

            if (null == viewWrapper)
            {
                windowWrapper = whoWrapper as NSWindow;
            }
            else
            {
                windowWrapper = viewWrapper.Window;
            }

            IntPtr            window = windowWrapper.Handle;
            NSGraphicsContext gcw    = null;

            if (handle == IntPtr.Zero || window == IntPtr.Zero)
            {
                // FIXME: Can we actually get a CGContextRef for the desktop?  this makes context IntPtr.Zero
                gcw = NSApplication.SharedApplication.Context;
//				context = gcw.graphicsPort();

                var desktop_bounds = NSRect.Empty;
                // NSScreen.mainScreen () returns the screen the the user is currently interacting with.
                // To get the screen identified by CGMainDisplayID (), get the 0th element of this array.
                NSScreen[] screens = NSScreen.Screens;

                if (null != screens && 0 < screens.Length)
                {
                    NSScreen screenWrap = screens[0];
                    desktop_bounds = screenWrap.Frame;
                }

                return(new CocoaContext(focusWindow, gcw, (int)desktop_bounds.Width,
                                        (int)desktop_bounds.Height));
            }

            if (null != viewWrapper && viewWrapper != NSView.FocusView())
            {
                if (!viewWrapper.LockFocusIfCanDraw())
                {
                    return(null);
                }

                focusWindow = viewWrapper;
            }

            gcw = windowWrapper.GraphicsContext;
            gcw.SaveGraphicsState();
//			context = gcw.graphicsPort();

//			NSRect winRect = windowWrapper.frame();
//			QDRect window_bounds = new QDRect (winRect.Top, winRect.Left, winRect.Bottom, winRect.Right);

            var vuRect = windowWrapper.Frame;

            if (null != viewWrapper)
            {
                vuRect = viewWrapper.Bounds;
                vuRect = viewWrapper.ConvertRectToView(vuRect, null);
            }
//			Rect view_bounds = new Rect (vuRect.origin.x, vuRect.origin.y, vuRect.size.width, vuRect.size.height);

            if (vuRect.Height < 0)
            {
                vuRect.Height = 0;
            }
            if (vuRect.Width < 0)
            {
                vuRect.Width = 0;
            }

//ASSUMPTION! lockFocus did the translating and clipping.
//ASSUMPTION! The NSView isFlipped.
//			CGContextTranslateCTM (context, view_bounds.origin.x, (window_bounds.bottom - window_bounds.top) - (view_bounds.origin.y + view_bounds.size.height));
//
//			// Create the original rect path and clip to it
//			Rect rc_clip = new Rect (0, 0, view_bounds.size.width, view_bounds.size.height);
//
//
//			Rectangle [] clip_rectangles = (Rectangle []) hwnd_delegate.DynamicInvoke (new object [] {handle});
//			if (clip_rectangles != null && clip_rectangles.Length > 0) {
//				int length = clip_rectangles.Length;
//
//				CGContextBeginPath (context);
//				CGContextAddRect (context, rc_clip);
//
//				for (int i = 0; i < length; i++) {
//					CGContextAddRect (context, new Rect (clip_rectangles [i].X, view_bounds.size.height - clip_rectangles [i].Y - clip_rectangles [i].Height, clip_rectangles [i].Width, clip_rectangles [i].Height));
//				}
//				CGContextClosePath (context);
//				CGContextEOClip (context);
//#if DEBUG_CLIPPING
//				if (clip_rectangles.Length >= debug_threshold) {
//					CGContextSetRGBFillColor (context, red, green, blue, 0.5f);
//					CGContextFillRect (context, rc_clip);
//					CGContextFlush (context);
//					System.Threading.Thread.Sleep (500);
//					if (red == 1.0f) { red = 0.0f; blue = 1.0f; }
//					else if (blue == 1.0f) { blue = 0.0f; green = 1.0f; }
//					else if (green == 1.0f) { green = 0.0f; red = 1.0f; }
//				}
//#endif
//			} else {
//				CGContextBeginPath (context);
//				CGContextAddRect (context, rc_clip);
//				CGContextClosePath (context);
//				CGContextClip (context);
//			}

            return(new CocoaContext(focusWindow, gcw, (int)vuRect.Width, (int)vuRect.Height));
        }