Пример #1
0
        /// <summary>
        ///  Sets the DC Viewport extent to the specified value and returns its previous value;
        ///  extent values are in device units.
        /// </summary>
        public Size SetViewportExtent(Size newExtent)
        {
            Size oldExtent = new Size();

            IntUnsafeNativeMethods.SetViewportExtEx(new HandleRef(this, Hdc), newExtent.Width, newExtent.Height, ref oldExtent);
            return(oldExtent);
        }
Пример #2
0
        /// <summary>
        ///  Creates a WindowsFont from the handle to a native GDI font and optionally takes ownership of managing
        ///  the lifetime of the handle.
        /// </summary>
        public unsafe static WindowsFont FromHfont(IntPtr hFont, bool takeOwnership = false)
        {
            NativeMethods.LOGFONTW logFont = new NativeMethods.LOGFONTW();
            IntUnsafeNativeMethods.GetObjectW(new HandleRef(null, hFont), sizeof(NativeMethods.LOGFONTW), ref logFont);

            FontStyle style = FontStyle.Regular;

            if (logFont.lfWeight == IntNativeMethods.FW_BOLD)
            {
                style |= FontStyle.Bold;
            }
            if (logFont.lfItalic == True)
            {
                style |= FontStyle.Italic;
            }
            if (logFont.lfUnderline == True)
            {
                style |= FontStyle.Underline;
            }
            if (logFont.lfStrikeOut == True)
            {
                style |= FontStyle.Strikeout;
            }

            WindowsFont wf = new WindowsFont(logFont, style, createHandle: false)
            {
                Hfont      = hFont,
                _ownHandle = takeOwnership // if true, hFont will be deleted on dispose.
            };

            return(wf);
        }
        public IntNativeMethods.TEXTMETRIC GetTextMetrics()
        {
            IntNativeMethods.TEXTMETRIC lptm = new IntNativeMethods.TEXTMETRIC();
            HandleRef hDC  = new HandleRef(this.dc, this.dc.Hdc);
            bool      flag = this.dc.MapMode != DeviceContextMapMode.Text;

            if (flag)
            {
                this.dc.SaveHdc();
            }
            try
            {
                if (flag)
                {
                    DeviceContextMapMode mode = this.dc.SetMapMode(DeviceContextMapMode.Text);
                }
                IntUnsafeNativeMethods.GetTextMetrics(hDC, ref lptm);
            }
            finally
            {
                if (flag)
                {
                    this.dc.RestoreHdc();
                }
            }
            return(lptm);
        }
Пример #4
0
        public static int GetTextMetrics(HandleRef hDC, ref IntNativeMethods.TEXTMETRIC lptm)
        {
            int retVal = IntUnsafeNativeMethods.GetTextMetricsW(hDC, ref lptm);

            DbgUtil.AssertWin32(retVal != 0, "GetTextMetrics(hdc=[0x{0:X8}], [out TEXTMETRIC] failed.", hDC.Handle);
            return(retVal);
        }
        public void DrawLine(WindowsPen pen, int x1, int y1, int x2, int y2)
        {
            HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc);
            DeviceContextBinaryRasterOperationFlags binaryRasterOperation = this.dc.BinaryRasterOperation;
            DeviceContextBackgroundMode             backgroundMode        = this.dc.BackgroundMode;

            if (binaryRasterOperation != DeviceContextBinaryRasterOperationFlags.CopyPen)
            {
                binaryRasterOperation = this.dc.SetRasterOperation(DeviceContextBinaryRasterOperationFlags.CopyPen);
            }
            if (backgroundMode != DeviceContextBackgroundMode.Transparent)
            {
                backgroundMode = this.dc.SetBackgroundMode(DeviceContextBackgroundMode.Transparent);
            }
            if (pen != null)
            {
                this.dc.SelectObject(pen.HPen, GdiObjectType.Pen);
            }
            IntNativeMethods.POINT pt = new IntNativeMethods.POINT();
            IntUnsafeNativeMethods.MoveToEx(hdc, x1, y1, pt);
            IntUnsafeNativeMethods.LineTo(hdc, x2, y2);
            if (backgroundMode != DeviceContextBackgroundMode.Transparent)
            {
                this.dc.SetBackgroundMode(backgroundMode);
            }
            if (binaryRasterOperation != DeviceContextBinaryRasterOperationFlags.CopyPen)
            {
                this.dc.SetRasterOperation(binaryRasterOperation);
            }
            IntUnsafeNativeMethods.MoveToEx(hdc, pt.x, pt.y, null);
        }
Пример #6
0
        public Color GetNearestColor(Color color)
        {
            HandleRef hdc         = new HandleRef(null, DeviceContext.Hdc);
            int       colorResult = IntUnsafeNativeMethods.GetNearestColor(hdc, ColorTranslator.ToWin32(color));

            return(ColorTranslator.FromWin32(colorResult));
        }
Пример #7
0
        ///  Drawing methods.

        public unsafe void DrawPie(WindowsPen pen, Rectangle bounds, float startAngle, float sweepAngle)
        {
            HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc);

            if (pen != null)
            {
                // 1. Select the pen in the DC
                Gdi32.SelectObject(hdc, new HandleRef(pen, pen.HPen));
            }

            // 2. call the functions
            // we first draw a path that goes :
            // from center of pie, draw arc (this draw the line to the beginning of the arc
            // then, draw the closing line.
            // paint the path with the pen
            int   sideLength = Math.Min(bounds.Width, bounds.Height);
            Point p          = new Point(bounds.X + sideLength / 2, bounds.Y + sideLength / 2);
            int   radius     = sideLength / 2;

            IntUnsafeNativeMethods.BeginPath(hdc);
            Point oldPoint = default;

            IntUnsafeNativeMethods.MoveToEx(hdc, p.X, p.Y, &oldPoint);
            IntUnsafeNativeMethods.AngleArc(hdc, p.X, p.Y, radius, startAngle, sweepAngle);
            IntUnsafeNativeMethods.LineTo(hdc, p.X, p.Y);
            IntUnsafeNativeMethods.EndPath(hdc);
            IntUnsafeNativeMethods.StrokePath(hdc);
        }
Пример #8
0
        public void DrawRectangle(WindowsPen pen, int x, int y, int width, int height)
        {
            Debug.Assert(pen != null, "pen == null");

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

            if (pen != null)
            {
                DeviceContext.SelectObject(pen.HPen, GdiObjectType.Pen);
            }

            Gdi32.R2 rasterOp = DeviceContext.BinaryRasterOperation;

            if (rasterOp != Gdi32.R2.COPYPEN)
            {
                rasterOp = DeviceContext.SetRasterOperation(Gdi32.R2.COPYPEN);
            }

            Gdi32.SelectObject(hdc, Gdi32.GetStockObject(Gdi32.StockObject.HOLLOW_BRUSH));

            // Add 1 to width and height to create the 'bounding box' (convert from point to size).
            IntUnsafeNativeMethods.Rectangle(hdc, x, y, x + width, y + height);

            if (rasterOp != Gdi32.R2.COPYPEN)
            {
                DeviceContext.SetRasterOperation(rasterOp);
            }
        }
Пример #9
0
        /// <summary>
        ///  Sets the DC Viewport origin to the specified value and returns its previous value;
        ///  origin values are in device units.
        /// </summary>
        public unsafe Point SetViewportOrigin(Point newOrigin)
        {
            var oldOrigin = new Point();

            IntUnsafeNativeMethods.SetViewportOrgEx(new HandleRef(this, Hdc), newOrigin.X, newOrigin.Y, &oldOrigin);
            return(oldOrigin);
        }
Пример #10
0
        /// <summary>
        ///  Creates the font handle.
        /// </summary>
        private unsafe WindowsFont(NativeMethods.LOGFONTW logFont, FontStyle style, bool createHandle)
        {
            Debug.Assert(Hfont == IntPtr.Zero, "hFont is not null, this will generate a handle leak.");

            _logFont = logFont;
            if (_logFont.FaceName.Length == 0)
            {
                _logFont.FaceName = DefaultFaceName;
            }
            Style = style;

            if (createHandle)
            {
                Hfont = IntUnsafeNativeMethods.CreateFontIndirectW(ref _logFont);

                if (Hfont == IntPtr.Zero)
                {
                    _logFont.FaceName       = DefaultFaceName;
                    _logFont.lfOutPrecision = IntNativeMethods.OUT_TT_ONLY_PRECIS; // TrueType only.

                    Hfont = IntUnsafeNativeMethods.CreateFontIndirectW(ref _logFont);
                }

                // Update logFont height and other adjusted parameters.
                IntUnsafeNativeMethods.GetObjectW(new HandleRef(this, Hfont), sizeof(NativeMethods.LOGFONTW), ref _logFont);

                // We created the hFont, we will delete it on dispose.
                _ownHandle = true;
            }
        }
Пример #11
0
        // Due to a problem with calling DeleteObject() on currently selected GDI objects,
        // we now track the initial set of objects when a DeviceContext is created.  Then,
        // we also track which objects are currently selected in the DeviceContext.  When
        // a currently selected object is disposed, it is first replaced in the DC and then
        // deleted.

        private void CacheInitialState()
        {
            Debug.Assert(hDC != IntPtr.Zero, "Cannot get initial state without a valid HDC");
            hCurrentPen   = hInitialPen = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_PEN);
            hCurrentBrush = hInitialBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_BRUSH);
            hCurrentBmp   = hInitialBmp = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_BITMAP);
            hCurrentFont  = hInitialFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_FONT);
        }
        public void FillRectangle(WindowsBrush brush, int x, int y, int width, int height)
        {
            HandleRef hDC    = new HandleRef(this.dc, this.dc.Hdc);
            IntPtr    hBrush = brush.HBrush;

            IntNativeMethods.RECT rect = new IntNativeMethods.RECT(x, y, x + width, y + height);
            IntUnsafeNativeMethods.FillRect(hDC, ref rect, new HandleRef(brush, hBrush));
        }
Пример #13
0
 void IDeviceContext.ReleaseHdc()
 {
     if ((this.hDC != IntPtr.Zero) && (this.dcType == System.Windows.Forms.Internal.DeviceContextType.Display))
     {
         IntUnsafeNativeMethods.ReleaseDC(new HandleRef(this, this.hWnd), new HandleRef(this, this.hDC));
         this.hDC = IntPtr.Zero;
     }
 }
Пример #14
0
 IntPtr IDeviceContext.GetHdc()
 {
     if (this.hDC == IntPtr.Zero)
     {
         this.hDC = IntUnsafeNativeMethods.GetDC(new HandleRef(this, this.hWnd));
     }
     return(this.hDC);
 }
Пример #15
0
        /// <summary>
        ///  Creates a WindowsFont from the font selected in the supplied dc.
        /// </summary>
        public static WindowsFont FromHdc(IntPtr hdc)
        {
            IntPtr hFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, hdc), IntNativeMethods.OBJ_FONT);

            // don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc.

            return(FromHfont(hFont));
        }
Пример #16
0
 public static WindowsFont FromHfont(IntPtr hFont, bool takeOwnership)
 {
     IntNativeMethods.LOGFONT lp = new IntNativeMethods.LOGFONT();
     IntUnsafeNativeMethods.GetObject(new HandleRef(null, hFont), lp);
     return(new WindowsFont(lp, false)
     {
         hFont = hFont, ownHandle = takeOwnership
     });
 }
 public Rectangle ToRectangle()
 {
     if (this.IsInfinite)
     {
         return(new Rectangle(-2147483647, -2147483647, 0x7fffffff, 0x7fffffff));
     }
     IntNativeMethods.RECT clipRect = new IntNativeMethods.RECT();
     IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref clipRect);
     return(new Rectangle(new Point(clipRect.left, clipRect.top), clipRect.Size));
 }
Пример #18
0
        public void FillRectangle(WindowsBrush brush, int x, int y, int width, int height)
        {
            Debug.Assert(brush != null, "brush == null");

            HandleRef hdc    = new HandleRef(dc, dc.Hdc);
            IntPtr    hBrush = brush.HBrush; // We don't delete this handle since we didn't create it.
            RECT      rect   = new RECT(x, y, x + width, y + height);

            IntUnsafeNativeMethods.FillRect(hdc, ref rect, new HandleRef(brush, hBrush));
        }
Пример #19
0
        /// <summary>
        ///  A rectangle representing the window region set with the SetWindowRgn function.
        /// </summary>
        public Rectangle ToRectangle()
        {
            if (IsInfinite)
            {
                return(new Rectangle(-int.MaxValue, -int.MaxValue, int.MaxValue, int.MaxValue));
            }

            IntNativeMethods.RECT rect = new IntNativeMethods.RECT();
            IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, nativeHandle), ref rect);
            return(new Rectangle(new Point(rect.left, rect.top), rect.Size));
        }
Пример #20
0
 private DeviceContext(IntPtr hDC, System.Windows.Forms.Internal.DeviceContextType dcType)
 {
     this.hWnd   = (IntPtr)(-1);
     this.hDC    = hDC;
     this.dcType = dcType;
     this.CacheInitialState();
     DeviceContexts.AddDeviceContext(this);
     if (dcType == System.Windows.Forms.Internal.DeviceContextType.Display)
     {
         this.hWnd = IntUnsafeNativeMethods.WindowFromDC(new HandleRef(this, this.hDC));
     }
 }
Пример #21
0
        public void ResetFont()
        {
#if OPTIMIZED_MEASUREMENTDC
            // in this case, GDI will copy back the previously saved font into the DC.
            // we dont actually know what the font is in our measurement DC so
            // we need to clear it off.
            MeasurementDCInfo.ResetIfIsMeasurementDC(Hdc);
#endif
            IntUnsafeNativeMethods.SelectObject(new HandleRef(this, Hdc), new HandleRef(null, hInitialFont));
            selectedFont = null;
            hCurrentFont = hInitialFont;
        }
Пример #22
0
 private void CreateFont()
 {
     this.hFont = IntUnsafeNativeMethods.CreateFontIndirect(this.logFont);
     if (this.hFont == IntPtr.Zero)
     {
         this.logFont.lfFaceName     = "Microsoft Sans Serif";
         this.logFont.lfOutPrecision = 7;
         this.hFont = IntUnsafeNativeMethods.CreateFontIndirect(this.logFont);
     }
     IntUnsafeNativeMethods.GetObject(new HandleRef(this, this.hFont), this.logFont);
     this.ownHandle = true;
 }
Пример #23
0
 public void IntersectClip(WindowsRegion wr)
 {
     if (wr.HRegion != IntPtr.Zero)
     {
         using (WindowsRegion region = new WindowsRegion(0, 0, 0, 0))
         {
             if (IntUnsafeNativeMethods.GetClipRgn(new HandleRef(this, this.Hdc), new HandleRef(region, region.HRegion)) == 1)
             {
                 wr.CombineRegion(region, wr, RegionCombineMode.AND);
             }
             this.SetClip(wr);
         }
     }
 }
        private void DrawEllipse(WindowsPen pen, WindowsBrush brush, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
        {
            HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc);

            if (pen != null)
            {
                IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef(pen, pen.HPen));
            }
            if (brush != null)
            {
                IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef(brush, brush.HBrush));
            }
            IntUnsafeNativeMethods.Ellipse(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect);
        }
 public void Dispose(bool disposing)
 {
     if (this.nativeHandle != IntPtr.Zero)
     {
         if (this.ownHandle)
         {
             IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.nativeHandle));
         }
         this.nativeHandle = IntPtr.Zero;
         if (disposing)
         {
             GC.SuppressFinalize(this);
         }
     }
 }
Пример #26
0
        internal void Dispose(bool disposing)
        {
            bool flag = false;

            if ((this.ownHandle && (!this.ownedByCacheManager || !disposing)) && ((this.everOwnedByCacheManager || !disposing) || !DeviceContexts.IsFontInUse(this)))
            {
                IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.hFont));
                this.hFont     = IntPtr.Zero;
                this.ownHandle = false;
                flag           = true;
            }
            if (disposing && (flag || !this.ownHandle))
            {
                GC.SuppressFinalize(this);
            }
        }
Пример #27
0
        /// <summary>
        ///  Constructor to contruct a DeviceContext object from an existing Win32 device context handle.
        /// </summary>
        private DeviceContext(IntPtr hDC, DeviceContextType dcType)
        {
            this.hDC    = hDC;
            this.dcType = dcType;

            CacheInitialState();
            DeviceContexts.AddDeviceContext(this);

            if (dcType == DeviceContextType.Display)
            {
                hWnd = IntUnsafeNativeMethods.WindowFromDC(new HandleRef(this, this.hDC));
            }
#if TRACK_HDC
            Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DeviceContext( hDC=0x{0:X8}, Type={1} )", unchecked ((int)hDC), dcType)));
#endif
        }
Пример #28
0
 internal void DisposeFont(bool disposing)
 {
     if (disposing)
     {
         DeviceContexts.RemoveDeviceContext(this);
     }
     if ((this.selectedFont != null) && (this.selectedFont.Hfont != IntPtr.Zero))
     {
         if (IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, this.hDC), 6) == this.selectedFont.Hfont)
         {
             IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(null, this.hInitialFont));
             IntPtr hInitialFont = this.hInitialFont;
         }
         this.selectedFont.Dispose(disposing);
         this.selectedFont = null;
     }
 }
Пример #29
0
        public IntPtr SelectObject(IntPtr hObj, GdiObjectType type)
        {
            switch (type)
            {
            case GdiObjectType.Pen:
                this.hCurrentPen = hObj;
                break;

            case GdiObjectType.Brush:
                this.hCurrentBrush = hObj;
                break;

            case GdiObjectType.Bitmap:
                this.hCurrentBmp = hObj;
                break;
            }
            return(IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(null, hObj)));
        }
Пример #30
0
        /// <summary>
        ///  Restores the device context to the specified state. The DC is restored by popping state information off a
        ///  stack created by earlier calls to the SaveHdc function.
        ///  The stack can contain the state information for several instances of the DC. If the state specified by the
        ///  specified parameter is not at the top of the stack, RestoreDC deletes all state information between the top
        ///  of the stack and the specified instance.
        ///  Specifies the saved state to be restored. If this parameter is positive, nSavedDC represents a specific
        ///  instance of the state to be restored. If this parameter is negative, nSavedDC represents an instance relative
        ///  to the current state. For example, -1 restores the most recently saved state.
        ///  See MSDN for more info.
        /// </summary>
        public void RestoreHdc()
        {
#if TRACK_HDC
            bool result =
#endif
            // Note: Don't use the Hdc property here, it would force handle creation.
            IntUnsafeNativeMethods.RestoreDC(new HandleRef(this, hDC), -1);
#if TRACK_HDC
            // Note: Winforms may call this method during app exit at which point the DC may have been finalized already causing this assert to popup.
            Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("ret[0]=DC.RestoreHdc(hDc=0x{1:x8})", result, unchecked ((int)this.hDC))));
#endif
            Debug.Assert(contextStack != null, "Someone is calling RestoreHdc() before SaveHdc()");

            if (contextStack != null)
            {
                GraphicsState g = (GraphicsState)contextStack.Pop();

                hCurrentBmp   = g.hBitmap;
                hCurrentBrush = g.hBrush;
                hCurrentPen   = g.hPen;
                hCurrentFont  = g.hFont;

                if (g.font != null && g.font.IsAlive)
                {
                    selectedFont = g.font.Target as WindowsFont;
                }
                else
                {
                    WindowsFont previousFont = selectedFont;
                    selectedFont = null;
                    if (previousFont != null && MeasurementDCInfo.IsMeasurementDC(this))
                    {
                        previousFont.Dispose();
                    }
                }
            }

#if OPTIMIZED_MEASUREMENTDC
            // in this case, GDI will copy back the previously saved font into the DC.
            // we dont actually know what the font is in our measurement DC so
            // we need to clear it off.
            MeasurementDCInfo.ResetIfIsMeasurementDC(hDC);
#endif
        }