public WindowsPen(DeviceContext dc, WindowsPenStyle style, int width, Color color)
 {
     this.style = style;
     this.width = width;
     this.color = color;
     this.dc = dc;
 }
 internal static void RemoveDeviceContext(DeviceContext dc)
 {
     if (activeDeviceContexts != null)
     {
         activeDeviceContexts.RemoveByHashCode(dc);
     }
 }
 public WindowsPen(DeviceContext dc, WindowsPenStyle style, int width, WindowsBrush windowsBrush)
 {
     this.style = style;
     this.wndBrush = (WindowsBrush) windowsBrush.Clone();
     this.width = width;
     this.color = windowsBrush.Color;
     this.dc = dc;
 }
 internal static void AddDeviceContext(DeviceContext dc)
 {
     if (activeDeviceContexts == null)
     {
         activeDeviceContexts = new System.Windows.Forms.ClientUtils.WeakRefCollection();
         activeDeviceContexts.RefCheckThreshold = 20;
     }
     if (!activeDeviceContexts.Contains(dc))
     {
         dc.Disposing += new EventHandler(DeviceContexts.OnDcDisposing);
         activeDeviceContexts.Add(dc);
     }
 }
 public WindowsPen(DeviceContext dc, Color color) : this(dc, WindowsPenStyle.Cosmetic, 1, color)
 {
 }
 public WindowsPen(DeviceContext dc) : this(dc, WindowsPenStyle.Cosmetic, 1, Color.Black)
 {
 }
 public WindowsBrush(DeviceContext dc, System.Drawing.Color color)
 {
     this.color = System.Drawing.Color.White;
     this.dc    = dc;
     this.color = color;
 }
Exemplo n.º 8
0
            public DCMapping(HandleRef hDC, Rectangle bounds) {
                if (hDC.Handle == IntPtr.Zero) {
                    throw new ArgumentNullException("hDC");
                }

                bool success;
                NativeMethods.POINT viewportOrg              = new NativeMethods.POINT();
                HandleRef hOriginalClippingRegion            = NativeMethods.NullHandleRef;
                NativeMethods.RegionFlags originalRegionType = NativeMethods.RegionFlags.NULLREGION;
                
                this.translatedBounds = bounds;
                this.graphics         = null;
                this.dc               = DeviceContext.FromHdc(hDC.Handle);

                this.dc.SaveHdc();

                // Retrieve the x-coordinates and y-coordinates of the viewport origin for the specified device context. 
                success = SafeNativeMethods.GetViewportOrgEx(hDC, viewportOrg);
                Debug.Assert(success, "GetViewportOrgEx() failed.");

                // Create a new rectangular clipping region based off of the bounds specified, shifted over by the x & y specified in the viewport origin.
                HandleRef hClippingRegion = new HandleRef(null, SafeNativeMethods.CreateRectRgn(viewportOrg.x + bounds.Left, viewportOrg.y + bounds.Top, viewportOrg.x + bounds.Right, viewportOrg.y + bounds.Bottom));
                Debug.Assert(hClippingRegion.Handle != IntPtr.Zero, "CreateRectRgn() failed.");

                try
                {
                    // Create an empty region oriented at 0,0 so we can populate it with the original clipping region of the hDC passed in.
                    hOriginalClippingRegion = new HandleRef(this, SafeNativeMethods.CreateRectRgn(0, 0, 0, 0));
                    Debug.Assert(hOriginalClippingRegion.Handle != IntPtr.Zero, "CreateRectRgn() failed.");

                    // Get the clipping region from the hDC: result = {-1 = error, 0 = no region, 1 = success} per MSDN
                    int result = SafeNativeMethods.GetClipRgn(hDC, hOriginalClippingRegion);
                    Debug.Assert(result != -1, "GetClipRgn() failed.");

                    // Shift the viewpoint origint by coordinates specified in "bounds". 
                    NativeMethods.POINT lastViewPort = new NativeMethods.POINT();
                    success = SafeNativeMethods.SetViewportOrgEx(hDC, viewportOrg.x + bounds.Left, viewportOrg.y + bounds.Top, lastViewPort);
                    Debug.Assert(success, "SetViewportOrgEx() failed.");

                    if (result != 0)
                    {
                        // Get the origninal clipping region so we can determine its type (we'll check later if we've restored the region back properly.)
                        NativeMethods.RECT originalClipRect = new NativeMethods.RECT();
                        originalRegionType = (NativeMethods.RegionFlags)SafeNativeMethods.GetRgnBox(hOriginalClippingRegion, ref originalClipRect);
                        Debug.Assert(originalRegionType != NativeMethods.RegionFlags.ERROR, "ERROR returned from SelectClipRgn while selecting the original clipping region..");

                        if (originalRegionType == NativeMethods.RegionFlags.SIMPLEREGION)
                        {
                            // Find the intersection of our clipping region and the current clipping region (our parent's)
                            //      Returns a NULLREGION, the two didn't intersect.
                            //      Returns a SIMPLEREGION, the two intersected
                            //      Resulting region (stuff that was in hOriginalClippingRegion AND hClippingRegion is placed in hClippingRegion
                            NativeMethods.RegionFlags combineResult = (NativeMethods.RegionFlags)SafeNativeMethods.CombineRgn(hClippingRegion, hClippingRegion, hOriginalClippingRegion, NativeMethods.RGN_AND);
                            Debug.Assert((combineResult == NativeMethods.RegionFlags.SIMPLEREGION) ||
                                            (combineResult == NativeMethods.RegionFlags.NULLREGION),
                                            "SIMPLEREGION or NULLREGION expected.");
                        }
                    }
                    else
                    {
                        // If there was no clipping region, then the result is a simple region.
                        // We don't need to keep track of the original now, since it is empty.
                        SafeNativeMethods.DeleteObject(hOriginalClippingRegion);
                        hOriginalClippingRegion = new HandleRef(null, IntPtr.Zero);
                        originalRegionType = NativeMethods.RegionFlags.SIMPLEREGION;
                    }

                    // Select the new clipping region; make sure it's a SIMPLEREGION or NULLREGION
                    NativeMethods.RegionFlags selectResult = (NativeMethods.RegionFlags)SafeNativeMethods.SelectClipRgn(hDC, hClippingRegion);
                    Debug.Assert((selectResult == NativeMethods.RegionFlags.SIMPLEREGION ||
                                  selectResult == NativeMethods.RegionFlags.NULLREGION),
                                  "SIMPLEREGION or NULLLREGION expected.");

                }
                catch (Exception ex)
                {
                    if (ClientUtils.IsSecurityOrCriticalException(ex))
                    {
                        throw;
                    }

                    this.dc.RestoreHdc();
                    this.dc.Dispose();
                }
                finally {
                    // Delete the new clipping region, as the clipping region for the HDC is now set 
                    // to this rectangle.  Hold on to hOriginalClippingRegion, as we'll need to restore
                    // it when this object is disposed.
                    success = SafeNativeMethods.DeleteObject(hClippingRegion);
                    Debug.Assert(success, "DeleteObject(hClippingRegion) failed.");

                    if (hOriginalClippingRegion.Handle != IntPtr.Zero) {
                        success = SafeNativeMethods.DeleteObject(hOriginalClippingRegion);
                        Debug.Assert(success, "DeleteObject(hOriginalClippingRegion) failed.");
                    }
                }
            }
Exemplo n.º 9
0
            void RestoreMirrorDC() {
                                    
                if (parent.IsMirrored && mirrordc != null) {
                    mirrordc.ViewportExtent = mirrordcExtent;
                    mirrordc.ViewportOrigin = mirrordcOrigin;
                    mirrordc.SetMapMode(mirrordcMode);
                    mirrordc.RestoreHdc();
                    mirrordc.Dispose();
                }

                mirrordc= null;
                mirrordcExtent = Size.Empty;
                mirrordcOrigin = Point.Empty;
                mirrordcMode = DeviceContextMapMode.Text;
            }
 public void Dispose()
 {
     if (this.graphics != null)
     {
         this.graphics.Dispose();
         this.graphics = null;
     }
     if (this.dc != null)
     {
         this.dc.RestoreHdc();
         this.dc.Dispose();
         this.dc = null;
     }
 }
 public WindowsSolidBrush(DeviceContext dc) : base(dc)
 {
 }
        internal static bool IsMeasurementDC(DeviceContext dc)
        {
            WindowsGraphics currentMeasurementGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();

            return(((currentMeasurementGraphics != null) && (currentMeasurementGraphics.DeviceContext != null)) && (currentMeasurementGraphics.DeviceContext.Hdc == dc.Hdc));
        }
 public WindowsSolidBrush(DeviceContext dc, Color color) : base(dc, color)
 {
 }
Exemplo n.º 14
0
 public WindowsBrush(DeviceContext dc, Color color)
 {
     DC    = dc;
     Color = color;
 }
Exemplo n.º 15
0
 public WindowsBrush(DeviceContext dc)
 {
     DC = dc;
 }
 public WindowsPen(DeviceContext dc, WindowsBrush windowsBrush) : this(dc, WindowsPenStyle.Cosmetic, 1, windowsBrush)
 {
 }
 private void CreateMirrorDC(IntPtr hdc, int originOffset)
 {
     this.mirrordc = DeviceContext.FromHdc(hdc);
     if (this.parent.IsMirrored && (this.mirrordc != null))
     {
         this.mirrordc.SaveHdc();
         this.mirrordcExtent = this.mirrordc.ViewportExtent;
         this.mirrordcOrigin = this.mirrordc.ViewportOrigin;
         this.mirrordcMode = this.mirrordc.SetMapMode(DeviceContextMapMode.Anisotropic);
         this.mirrordc.ViewportExtent = new Size(-this.mirrordcExtent.Width, this.mirrordcExtent.Height);
         this.mirrordc.ViewportOrigin = new Point(this.mirrordcOrigin.X + originOffset, this.mirrordcOrigin.Y);
     }
 }
Exemplo n.º 18
0
 public WindowsPen(DeviceContext dc) :
     this(dc, default, CosmeticPenWidth, Color.Black)
 {
 }
 public DCMapping(HandleRef hDC, Rectangle bounds)
 {
     if (hDC.Handle == IntPtr.Zero)
     {
         throw new ArgumentNullException("hDC");
     }
     System.Windows.Forms.NativeMethods.POINT point = new System.Windows.Forms.NativeMethods.POINT();
     HandleRef nullHandleRef = System.Windows.Forms.NativeMethods.NullHandleRef;
     System.Windows.Forms.NativeMethods.RegionFlags nULLREGION = System.Windows.Forms.NativeMethods.RegionFlags.NULLREGION;
     this.translatedBounds = bounds;
     this.graphics = null;
     this.dc = DeviceContext.FromHdc(hDC.Handle);
     this.dc.SaveHdc();
     System.Windows.Forms.SafeNativeMethods.GetViewportOrgEx(hDC, point);
     HandleRef hRgn = new HandleRef(null, System.Windows.Forms.SafeNativeMethods.CreateRectRgn(point.x + bounds.Left, point.y + bounds.Top, point.x + bounds.Right, point.y + bounds.Bottom));
     try
     {
         nullHandleRef = new HandleRef(this, System.Windows.Forms.SafeNativeMethods.CreateRectRgn(0, 0, 0, 0));
         int clipRgn = System.Windows.Forms.SafeNativeMethods.GetClipRgn(hDC, nullHandleRef);
         System.Windows.Forms.NativeMethods.POINT point2 = new System.Windows.Forms.NativeMethods.POINT();
         System.Windows.Forms.SafeNativeMethods.SetViewportOrgEx(hDC, point.x + bounds.Left, point.y + bounds.Top, point2);
         if (clipRgn != 0)
         {
             System.Windows.Forms.NativeMethods.RECT clipRect = new System.Windows.Forms.NativeMethods.RECT();
             if (System.Windows.Forms.SafeNativeMethods.GetRgnBox(nullHandleRef, ref clipRect) == 2)
             {
                 System.Windows.Forms.SafeNativeMethods.CombineRgn(hRgn, hRgn, nullHandleRef, 1);
             }
         }
         else
         {
             System.Windows.Forms.SafeNativeMethods.DeleteObject(nullHandleRef);
             nullHandleRef = new HandleRef(null, IntPtr.Zero);
             nULLREGION = System.Windows.Forms.NativeMethods.RegionFlags.SIMPLEREGION;
         }
         System.Windows.Forms.SafeNativeMethods.SelectClipRgn(hDC, hRgn);
     }
     catch (Exception exception)
     {
         if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
         {
             throw;
         }
         this.dc.RestoreHdc();
         this.dc.Dispose();
     }
     finally
     {
         System.Windows.Forms.SafeNativeMethods.DeleteObject(hRgn);
         if (nullHandleRef.Handle != IntPtr.Zero)
         {
             System.Windows.Forms.SafeNativeMethods.DeleteObject(nullHandleRef);
         }
     }
 }
 private void RestoreMirrorDC()
 {
     if (this.parent.IsMirrored && (this.mirrordc != null))
     {
         this.mirrordc.ViewportExtent = this.mirrordcExtent;
         this.mirrordc.ViewportOrigin = this.mirrordcOrigin;
         this.mirrordc.SetMapMode(this.mirrordcMode);
         this.mirrordc.RestoreHdc();
         this.mirrordc.Dispose();
     }
     this.mirrordc = null;
     this.mirrordcExtent = Size.Empty;
     this.mirrordcOrigin = Point.Empty;
     this.mirrordcMode = DeviceContextMapMode.Text;
 }
Exemplo n.º 21
0
 public WindowsPen(DeviceContext dc, Color color) :
     this(dc, default, CosmeticPenWidth, color)
 {
 }
Exemplo n.º 22
0
            /// <devdoc>
            ///
            /// VSWhidbey #455702.
            ///
            /// Since we added mirroring to certain controls, we need to make sure the
            /// error icons show up in the correct place. We cannot mirror the errorwindow
            /// in EnsureCreated (although that would have been really easy), since we use
            /// GDI+ for some of this code, and as we all know, GDI+ does not handle mirroring
            /// at all.
            ///
            /// To work around that we create our own mirrored dc when we need to.
            ///
            /// </devdoc>
            void CreateMirrorDC(IntPtr hdc, int originOffset) {

                Debug.Assert(mirrordc == null, "Why is mirrordc non-null? Did you not call RestoreMirrorDC?");

                mirrordc = DeviceContext.FromHdc(hdc);
                if (parent.IsMirrored && mirrordc != null) {
                    mirrordc.SaveHdc();
                    mirrordcExtent = mirrordc.ViewportExtent;
                    mirrordcOrigin = mirrordc.ViewportOrigin;

                    mirrordcMode = mirrordc.SetMapMode(DeviceContextMapMode.Anisotropic);
                    mirrordc.ViewportExtent = new Size(-(mirrordcExtent.Width), mirrordcExtent.Height);
                    mirrordc.ViewportOrigin = new Point(mirrordcOrigin.X + originOffset, mirrordcOrigin.Y);
                }
            }
Exemplo n.º 23
0
 public WindowsPen(DeviceContext dc, WindowsBrush windowsBrush) :
     this(dc, default, CosmeticPenWidth, windowsBrush)
 {
 }
 public WindowsBrush(DeviceContext dc, System.Drawing.Color color)
 {
     this.color = System.Drawing.Color.White;
     this.dc = dc;
     this.color = color;
 }
Exemplo n.º 25
0
        /// <summary>
        ///  Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified
        ///  TextFormatFlags flags.
        ///
        ///  If font is null, the font currently selected in the hdc is used.
        ///
        ///  If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used.
        /// </summary>
        public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, User32.DT flags)
        {
            if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent)
            {
                return;
            }

            Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!");
            Debug.Assert((flags & User32.DT.CALCRECT) == 0, "CALCRECT flag is set, text won't be drawn");

            HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc);

            // DrawText requires default text alignment.
            if (DeviceContext.TextAlignment != default)
            {
                DeviceContext.TextAlignment = default;
            }

            // color empty means use the one currently selected in the dc.

            if (!foreColor.IsEmpty && foreColor != DeviceContext.TextColor)
            {
                DeviceContext.TextColor = foreColor;
            }

            if (font != null)
            {
                DeviceContext.SelectFont(font);
            }

            Gdi32.BKMODE newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ?
                                          Gdi32.BKMODE.TRANSPARENT :
                                          Gdi32.BKMODE.OPAQUE;

            if (DeviceContext.BackgroundMode != newBackGndMode)
            {
                DeviceContext.SetBackgroundMode(newBackGndMode);
            }

            if (newBackGndMode != Gdi32.BKMODE.TRANSPARENT && backColor != DeviceContext.BackgroundColor)
            {
                DeviceContext.BackgroundColor = backColor;
            }

            User32.DRAWTEXTPARAMS dtparams = GetTextMargins(font);

            bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, ref dtparams);

            // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation.
            if (bounds.Width == MaxSize.Width)
            {
                bounds.Width -= bounds.X;
            }
            if (bounds.Height == MaxSize.Height)
            {
                bounds.Height -= bounds.Y;
            }

            var rect = new RECT(bounds);

            User32.DrawTextExW(hdc, text, text.Length, ref rect, flags, ref dtparams);

            // No need to restore previous objects into the dc (see comments on top of the class).
        }
Exemplo n.º 26
0
        /// <summary>
        ///  Returns the Size in logical units of the given text using the given Font, and according to the formatting flags.
        ///  The proposed size is used to create a bounding rectangle as follows:
        ///  - If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by
        ///  the lpRect parameter and extends the base of the rectangle to bound the last line of text.
        ///  - If the largest word is wider than the rectangle, the width is expanded.
        ///  - If the text is less than the width of the rectangle, the width is reduced.
        ///  - If there is only one line of text, DrawText modifies the right side of the rectangle so that
        ///  it bounds the last character in the line.
        ///  If the font is null, the hdc's current font will be used.
        ///
        ///  Note for vertical fonts (if ever supported): DrawTextEx uses GetTextExtentPoint32 for measuring the text and this
        ///  function has the following limitation (from MSDN):
        ///  - This function assumes that the text is horizontal, that is, that the escapement is always 0. This is true for both
        ///  the horizontal and vertical measurements of the text.  The application must convert it explicitly.
        /// </summary>
        public Size MeasureText(string text, WindowsFont font, Size proposedSize, User32.DT flags)
        {
            Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!");

            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            // DrawText returns a rectangle useful for aligning, but not guaranteed to encompass all
            // pixels (its not a FitBlackBox, if the text is italicized, it will overhang on the right.)
            // So we need to account for this.

#if OPTIMIZED_MEASUREMENTDC
            User32.DRAWTEXTPARAMS dtparams;
            // use the cache if we've got it
            if (MeasurementDCInfo.IsMeasurementDC(DeviceContext))
            {
                dtparams = MeasurementDCInfo.GetTextMargins(this, font);
            }
            else
            {
                dtparams = GetTextMargins(font);
            }
#else
            User32.DRAWTEXTPARAMS dtparams = GetTextMargins(font);
#endif

            // If Width / Height are < 0, we need to make them larger or DrawText will return
            // an unbounded measurement when we actually trying to make it very narrow.
            int minWidth = 1 + dtparams.iLeftMargin + dtparams.iRightMargin;

            if (proposedSize.Width <= minWidth)
            {
                proposedSize.Width = minWidth;
            }
            if (proposedSize.Height <= 0)
            {
                proposedSize.Height = 1;
            }

            var rect = new RECT(0, 0, proposedSize.Width, proposedSize.Height);
            if (font != null)
            {
                DeviceContext.SelectFont(font);
            }

            // If proposedSize.Height >= MaxSize.Height it is assumed bounds needed.  If flags contain SINGLELINE and
            // VCENTER or BOTTOM options, DrawTextEx does not bind the rectangle to the actual text height since
            // it assumes the text is to be vertically aligned; we need to clear the VCENTER and BOTTOM flags to
            // get the actual text bounds.
            if (proposedSize.Height >= MaxSize.Height && (flags & User32.DT.SINGLELINE) != 0)
            {
                // Clear vertical-alignment flags.
                flags &= ~(User32.DT.BOTTOM | User32.DT.VCENTER);
            }

            if (proposedSize.Width == MaxSize.Width)
            {
                // PERF: No constraining width means no word break.
                // in this case, we dont care about word wrapping - there should be enough room to fit it all
                flags &= ~(User32.DT.WORDBREAK);
            }

            flags |= User32.DT.CALCRECT;
            User32.DrawTextExW(DeviceContext.Hdc, text, text.Length, ref rect, flags, ref dtparams);

            return(rect.Size);
        }
 internal static bool IsMeasurementDC(DeviceContext dc)
 {
     WindowsGraphics currentMeasurementGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();
     return (((currentMeasurementGraphics != null) && (currentMeasurementGraphics.DeviceContext != null)) && (currentMeasurementGraphics.DeviceContext.Hdc == dc.Hdc));
 }
Exemplo n.º 28
0
            public void Dispose() {
                if( graphics != null ){
                    // Reset GDI+ if used.
                    // we need to dispose the graphics object first, as it will do 
                    // some restoration to the ViewPort and ClipRectangle to restore the hDC to 
                    // the same state it was created in
                    graphics.Dispose();
                    graphics = null;
                }

                if (this.dc != null) {
                    // Now properly reset GDI.
                    this.dc.RestoreHdc();
                    this.dc.Dispose();
                    this.dc = null;
                }
            }
Exemplo n.º 29
0
 public WindowsGraphics(DeviceContext dc)
 {
     Debug.Assert(dc != null, "null dc!");
     DeviceContext = dc;
     DeviceContext.SaveHdc();
 }