Ejemplo n.º 1
0
        /// Drawing methods.

        /// <devdoc>
        /// </devdoc>
        public void DrawPie(WindowsPen pen, Rectangle bounds, float startAngle, float sweepAngle)
        {
            HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc);

            if (pen != null)
            {
                // 1. Select the pen in the DC
                IntUnsafeNativeMethods.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);
            IntUnsafeNativeMethods.MoveToEx(hdc, p.X, p.Y, null);
            IntUnsafeNativeMethods.AngleArc(hdc, p.X, p.Y, radius, startAngle, sweepAngle);
            IntUnsafeNativeMethods.LineTo(hdc, p.X, p.Y);
            IntUnsafeNativeMethods.EndPath(hdc);
            IntUnsafeNativeMethods.StrokePath(hdc);
        }
Ejemplo n.º 2
0
        public int SaveHdc()
        {
            HandleRef hdc   = new HandleRef(this, this.Hdc);
            int       state = IntUnsafeNativeMethods.SaveDC(hdc);

            if (contextStack == null)
            {
                contextStack = new Stack();
            }

            GraphicsState g = new GraphicsState();

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

#if !DRAWING_NAMESPACE
            g.font = new WeakReference(selectedFont);
#endif


            contextStack.Push(g);

#if TRACK_HDC
            Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("state[0]=DC.SaveHdc(hDc=0x{1:x8})", state, unchecked ((int)this.hDC))));
#endif

            return(state);
        }
Ejemplo n.º 3
0
        /// <include file='doc\WindowsGraphics.uex' path='docs/doc[@for="WindowsGraphics.DrawRectangle3"]/*' />
        public void DrawRectangle(WindowsPen pen, int x, int y, int width, int height)
        {
            Debug.Assert(pen != null, "pen == null");

            HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc);

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

            DeviceContextBinaryRasterOperationFlags rasterOp = this.dc.BinaryRasterOperation;

            if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen)
            {
                rasterOp = this.dc.SetRasterOperation(DeviceContextBinaryRasterOperationFlags.CopyPen);
            }

            IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef(null, IntUnsafeNativeMethods.GetStockObject(IntNativeMethods.HOLLOW_BRUSH)));
            // Microsoft

            IntUnsafeNativeMethods.Rectangle(hdc, x, y, x + width, y + height);

            if (rasterOp != DeviceContextBinaryRasterOperationFlags.CopyPen)
            {
                this.dc.SetRasterOperation(rasterOp);
            }
        }
Ejemplo n.º 4
0
        /// <devdoc>
        ///    <para>
        ///       Returns the Size of the given text using the specified font if not null, otherwise the font currently
        ///       set in the dc is used.
        ///       This method is used to get the size in points of a line of text; it uses GetTextExtentPoint32 function
        ///       which computes the width and height of the text ignoring TAB\CR\LF characters.
        ///       A text extent is the distance between the beginning of the space and a character that will fit in the space.
        ///    </para>
        /// </devdoc>
        public Size GetTextExtent(string text, WindowsFont font)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            IntNativeMethods.SIZE size = new IntNativeMethods.SIZE();

            HandleRef hdc = new HandleRef(null, this.dc.Hdc);

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

            IntUnsafeNativeMethods.GetTextExtentPoint32(hdc, text, size);

            // Unselect, but not from Measurement DC as it keeps the same
            // font selected for perf reasons.
            if (font != null && !MeasurementDCInfo.IsMeasurementDC(this.dc))
            {
                this.dc.ResetFont();
            }

            return(new Size(size.cx, size.cy));
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public void SetClip(WindowsRegion region)
        {
            HandleRef hdc     = new HandleRef(this, this.Hdc);
            HandleRef hRegion = new HandleRef(region, region.HRegion);

            IntUnsafeNativeMethods.SelectClipRgn(hdc, hRegion);
        }
Ejemplo n.º 7
0
        public void IntersectClip(WindowsRegion wr)
        {
            //if the incoming windowsregion is infinite, there is no need to do any intersecting.
            if (wr.HRegion == IntPtr.Zero)
            {
                return;
            }

            WindowsRegion clip = new WindowsRegion(0, 0, 0, 0);

            try {
                int result = IntUnsafeNativeMethods.GetClipRgn(new HandleRef(this, this.Hdc), new HandleRef(clip, clip.HRegion));

                // If the function succeeds and there is a clipping region for the given device context, the return value is 1.
                if (result == 1)
                {
                    Debug.Assert(clip.HRegion != IntPtr.Zero);
                    wr.CombineRegion(clip, wr, RegionCombineMode.AND);  //1 = AND (or Intersect)
                }

                SetClip(wr);
            }
            finally {
                clip.Dispose();
            }
        }
        /// <devdoc>
        ///     Sets the DC Viewport origin to the specified value and returns its previous value; origin values are in device units.
        /// </devdoc>
        public Point SetViewportOrigin(Point newOrigin)
        {
            IntNativeMethods.POINT oldOrigin = new IntNativeMethods.POINT();
            IntUnsafeNativeMethods.SetViewportOrgEx(new HandleRef(this, this.Hdc), newOrigin.X, newOrigin.Y, oldOrigin);

            return(oldOrigin.ToPoint());
        }
Ejemplo n.º 9
0
        public static int DrawTextEx(HandleRef hDC, string text, ref IntNativeMethods.RECT lpRect, int nFormat, [In, Out] IntNativeMethods.DRAWTEXTPARAMS lpDTParams)
        {
            int retVal;

            if (Marshal.SystemDefaultCharSize == 1)
            {
                // CapRectangleForWin9x(ref lpRect);
                // we have to do this because if we pass too big of a value (<= 2^31) to win9x
                // it fails and returns negative values as the rect in which it painted the text
                lpRect.top    = Math.Min(Int16.MaxValue, lpRect.top);
                lpRect.left   = Math.Min(Int16.MaxValue, lpRect.left);
                lpRect.right  = Math.Min(Int16.MaxValue, lpRect.right);
                lpRect.bottom = Math.Min(Int16.MaxValue, lpRect.bottom);

                // Convert Unicode string to ANSI.
                int    byteCount = IntUnsafeNativeMethods.WideCharToMultiByte(IntNativeMethods.CP_ACP, 0, text, text.Length, null, 0, IntPtr.Zero, IntPtr.Zero);
                byte[] textBytes = new byte[byteCount];
                IntUnsafeNativeMethods.WideCharToMultiByte(IntNativeMethods.CP_ACP, 0, text, text.Length, textBytes, textBytes.Length, IntPtr.Zero, IntPtr.Zero);

                // Security: Windows 95/98/Me: This value may not exceed 8192.
                byteCount = Math.Min(byteCount, IntNativeMethods.MaxTextLengthInWin9x);
                retVal    = DrawTextExA(hDC, textBytes, byteCount, ref lpRect, nFormat, lpDTParams);
            }
            else
            {
                retVal = DrawTextExW(hDC, text, text.Length, ref lpRect, nFormat, lpDTParams);
            }

            DbgUtil.AssertWin32(retVal != 0, "DrawTextEx(hdc=[0x{0:X8}], text=[{1}], rect=[{2}], flags=[{3}] failed.", hDC.Handle, text, lpRect, nFormat);
            return(retVal);
        }
Ejemplo n.º 10
0
        /// <devdoc>
        /// </devdoc>
        public Color GetNearestColor(Color color)
        {
            HandleRef hdc         = new HandleRef(null, this.dc.Hdc);
            int       colorResult = IntUnsafeNativeMethods.GetNearestColor(hdc, ColorTranslator.ToWin32(color));

            return(ColorTranslator.FromWin32(colorResult));
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Creates the font handle.
        /// </summary>


        private void CreateFont()
        {
            Debug.Assert(hFont == IntPtr.Zero, "hFont is not null, this will generate a handle leak.");
            Debug.Assert(this.logFont != null, "WindowsFont.logFont not initialized.");

            this.hFont = IntUnsafeNativeMethods.CreateFontIndirect(this.logFont);

#if TRACK_HFONT
            Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("HFONT[0x{0:x8}] = CreateFontIndirect( LOGFONT={1} )", (int)this.hFont, this.logFont)));
#endif
            if (this.hFont == IntPtr.Zero)
            {
                this.logFont.lfFaceName     = defaultFaceName;
                this.logFont.lfOutPrecision = IntNativeMethods.OUT_TT_ONLY_PRECIS; // True Type only.

                this.hFont = IntUnsafeNativeMethods.CreateFontIndirect(this.logFont);

#if TRACK_HFONT
                Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("HFONT[0x{0:x8}] = CreateFontIndirect( LOGFONT={1} )", (int)this.hFont, this.logFont)));
#endif
            }

            // Update logFont height and other adjusted parameters.
            //
            IntUnsafeNativeMethods.GetObject(new HandleRef(this, this.hFont), this.logFont);

            // We created the hFont, we will delete it on dispose.
            this.ownHandle = true;
        }
Ejemplo n.º 12
0
        internal void Dispose(bool disposing)
        {
            bool deletedHandle = false;

            if (this.ownHandle)
            {
                if (!ownedByCacheManager || !disposing)
                {
                    // If we were ever owned by the CacheManger and we're being disposed
                    // we can be sure that we're not in use by any DC's (otherwise Dispose() wouldn't have been called)
                    // skip the check IsFontInUse check in this case.
                    // Also skip the check if disposing == false, because the cache is thread-static
                    // and that means we're being called from the finalizer.
                    if (everOwnedByCacheManager || !disposing || !DeviceContexts.IsFontInUse(this))
                    {
                        Debug.Assert(this.hFont != IntPtr.Zero, "Unexpected null hFont.");
                        DbgUtil.AssertFinalization(this, disposing);

                        IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.hFont));
#if TRACK_HFONT
                        Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DeleteObject(HFONT[0x{0:x8}]))", (int)this.hFont)));
#endif
                        this.hFont     = IntPtr.Zero;
                        this.ownHandle = false;
                        deletedHandle  = true;
                    }
                }
            }

            if (disposing && (deletedHandle || !ownHandle))
            {
                GC.SuppressFinalize(this);
            }
        }
Ejemplo n.º 13
0
        /// <devdoc>
        ///     Sets the DC Viewport extent to the specified value and returns its previous value; extent values are in device units.
        /// </devdoc>
        public Size SetViewportExtent(Size newExtent)
        {
            IntNativeMethods.SIZE oldExtent = new IntNativeMethods.SIZE();

            IntUnsafeNativeMethods.SetViewportExtEx(new HandleRef(this, this.Hdc), newExtent.Width, newExtent.Height, oldExtent);

            return(oldExtent.ToSize());
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
0
        public unsafe void CreateFontIndirect()
        {
            NativeMethods.LOGFONTW logFont = default;
            IntPtr handle = IntUnsafeNativeMethods.CreateFontIndirectW(ref logFont);

            Assert.NotEqual(IntPtr.Zero, handle);
            Assert.True(Gdi32.DeleteObject(handle) != BOOL.FALSE);
        }
Ejemplo n.º 16
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(this.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);
        }
Ejemplo n.º 17
0
        /// <devdoc>
        ///     CreateIC creates a DeviceContext object wrapping an hdc created with the Win32 CreateIC function.
        /// </devdoc>


        public static DeviceContext CreateIC(string driverName, string deviceName, string fileName, HandleRef devMode)
        {
            // Note: All input params can be null but not at the same time.  See MSDN for information.

            IntPtr hdc = IntUnsafeNativeMethods.CreateIC(driverName, deviceName, fileName, devMode);

            return(new DeviceContext(hdc, DeviceContextType.Information));
        }
Ejemplo n.º 18
0
            private void DrawTreeItem(string itemText, int imageIndex, IntPtr dc, System.Windows.Forms.NativeMethods.RECT rcIn, int state, int backColor, int textColor)
            {
                IntNativeMethods.SIZE size      = new IntNativeMethods.SIZE();
                IntNativeMethods.RECT lpRect    = new IntNativeMethods.RECT();
                IntNativeMethods.RECT rect      = new IntNativeMethods.RECT(rcIn.left, rcIn.top, rcIn.right, rcIn.bottom);
                ImageList             imageList = base.ImageList;
                IntPtr zero = IntPtr.Zero;

                if ((state & 2) != 0)
                {
                    zero = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(base.Parent, base.Parent.FontHandle));
                }
                if (((state & 1) != 0) && (this.hbrushDither != IntPtr.Zero))
                {
                    this.FillRectDither(dc, rcIn);
                    System.Windows.Forms.SafeNativeMethods.SetBkMode(new HandleRef(null, dc), 1);
                }
                else
                {
                    System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), backColor);
                    IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 6, ref rect, null, 0, null);
                }
                IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(null, dc), itemText, size);
                lpRect.left   = (rect.left + 0x10) + 8;
                lpRect.top    = rect.top + (((rect.bottom - rect.top) - size.cy) >> 1);
                lpRect.bottom = lpRect.top + size.cy;
                lpRect.right  = rect.right;
                System.Windows.Forms.SafeNativeMethods.SetTextColor(new HandleRef(null, dc), textColor);
                IntUnsafeNativeMethods.DrawText(new HandleRef(null, dc), itemText, ref lpRect, 0x8804);
                System.Windows.Forms.SafeNativeMethods.ImageList_Draw(new HandleRef(imageList, imageList.Handle), imageIndex, new HandleRef(null, dc), 4, rect.top + (((rect.bottom - rect.top) - 0x10) >> 1), 1);
                if ((state & 2) != 0)
                {
                    int clr = System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlLightLight));
                    lpRect.left   = rect.left;
                    lpRect.top    = rect.top;
                    lpRect.bottom = rect.top + 1;
                    lpRect.right  = rect.right;
                    IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null);
                    lpRect.bottom = rect.bottom;
                    lpRect.right  = rect.left + 1;
                    IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null);
                    System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlDark));
                    lpRect.left   = rect.left;
                    lpRect.right  = rect.right;
                    lpRect.top    = rect.bottom - 1;
                    lpRect.bottom = rect.bottom;
                    IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null);
                    lpRect.left = rect.right - 1;
                    lpRect.top  = rect.top;
                    IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null);
                    System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), clr);
                }
                if (zero != IntPtr.Zero)
                {
                    System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(null, zero));
                }
            }
Ejemplo n.º 19
0
        /// <devdoc>
        ///     Creates a DeviceContext object wrapping a memory DC compatible with the specified device.
        /// </devdoc>


        public static DeviceContext FromCompatibleDC(IntPtr hdc)
        {
            // If hdc is null, the function creates a memory DC compatible with the application's current screen.
            // In this case the thread that calls CreateCompatibleDC owns the HDC that is created. When this thread is destroyed,
            // the HDC is no longer valid.

            IntPtr compatibleDc = IntUnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, hdc));

            return(new DeviceContext(compatibleDc, DeviceContextType.Memory));
        }
        public static WindowsBrush FromDC(DeviceContext dc)
        {
            IntPtr hBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, dc.Hdc), IntNativeMethods.OBJ_BRUSH);

            IntNativeMethods.LOGBRUSH logBrush = new IntNativeMethods.LOGBRUSH();
            IntUnsafeNativeMethods.GetObject(new HandleRef(null, hBrush), logBrush);

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

            return(WindowsBrush.FromLogBrush(dc, logBrush));
        }
Ejemplo n.º 21
0
        /// <devdoc>
        ///     A rectangle representing the window region set with the SetWindowRgn function.
        /// </devdoc>
        public Rectangle ToRectangle()
        {
            if (this.IsInfinite)
            {
                return(new Rectangle(-int.MaxValue, -int.MaxValue, int.MaxValue, int.MaxValue));
            }

            IntNativeMethods.RECT rect = new IntNativeMethods.RECT();
            IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref rect);
            return(new Rectangle(new Point(rect.left, rect.top), rect.Size));
        }
Ejemplo n.º 22
0
        /// <devdoc>
        ///     Creates a WindowsFont from the handle to a native GDI font and optionally takes ownership of managing
        ///     the lifetime of the handle.
        /// </devdoc>


        public static WindowsFont FromHfont(IntPtr hFont, bool takeOwnership)
        {
            IntNativeMethods.LOGFONT lf = new IntNativeMethods.LOGFONT();
            IntUnsafeNativeMethods.GetObject(new HandleRef(null, hFont), lf);

            WindowsFont wf = new WindowsFont(lf, /*createHandle*/ false);

            wf.hFont     = hFont;
            wf.ownHandle = takeOwnership; // if true, hFont will be deleted on dispose.

            return(wf);
        }
Ejemplo n.º 23
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(this.Hdc);
#endif
            IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(null, hInitialFont));
            selectedFont = null;
            hCurrentFont = hInitialFont;
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Returns a handle to this <see cref='Font'/>.
        /// </summary>
        public IntPtr ToHfont()
        {
            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT();

            ToLogFont(lf);

            IntPtr handle = IntUnsafeNativeMethods.IntCreateFontIndirect(lf);

            if (handle == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            return(handle);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Returns a handle to this <see cref='Font'/>.
        /// </summary>
        public IntPtr ToHfont()
        {
            using (ScreenDC dc = ScreenDC.Create())
                using (Graphics graphics = Graphics.FromHdcInternal(dc))
                {
                    SafeNativeMethods.LOGFONT lf = ToLogFontInternal(graphics);
                    IntPtr handle = IntUnsafeNativeMethods.CreateFontIndirect(ref lf);
                    if (handle == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }

                    return(handle);
                }
        }
Ejemplo n.º 26
0
        /// <include file='doc\IDeviceContext.uex' path='docs/doc[@for="DeviceContext.ReleaseHdc"]/*' />
        ///<devdoc>
        ///     If the object was created from a DC, this object doesn't 'own' the dc so we just ignore
        ///     this call.
        ///</devdoc>
        void IDeviceContext.ReleaseHdc()
        {
            if (this.hDC != IntPtr.Zero && this.dcType == DeviceContextType.Display)
            {
#if TRACK_HDC
                int retVal =
#endif
                IntUnsafeNativeMethods.ReleaseDC(new HandleRef(this, this.hWnd), new HandleRef(this, this.hDC));
                // Note: retVal == 0 means it was not released but doesn't necessarily means an error; class or private DCs are never released.
#if TRACK_HDC
                Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("[ret={0}]=DC.ReleaseDC(hDc=0x{1:x8}, hWnd=0x{2:x8})", retVal, unchecked ((int)this.hDC), unchecked ((int)this.hWnd))));
#endif
                this.hDC = IntPtr.Zero;
            }
        }
Ejemplo n.º 27
0
        /// <include file='doc\IDeviceContext.uex' path='docs/doc[@for="DeviceContext.GetHdc"]/*' />
        /// <devdoc>
        ///     Explicit interface method implementation to hide them a bit for usability reasons so the object is seen
        ///     as a wrapper around an hdc that is always available, and for performance reasons since it caches the hdc
        ///     if used in this way.
        /// </devdoc>


        IntPtr IDeviceContext.GetHdc()
        {
            if (this.hDC == IntPtr.Zero)
            {
                Debug.Assert(this.dcType == DeviceContextType.Display, "Calling GetDC from a non display/window device.");

                // Note: for common DCs, GetDC assigns default attributes to the DC each time it is retrieved.
                // For example, the default font is System.
                this.hDC = IntUnsafeNativeMethods.GetDC(new HandleRef(this, this.hWnd));
#if TRACK_HDC
                Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("hdc[0x{0:x8}]=DC.GetHdc(hWnd=0x{1:x8})", unchecked ((int)this.hDC), unchecked ((int)this.hWnd))));
#endif
            }

            return(this.hDC);
        }
Ejemplo n.º 28
0
        /// <devdoc>
        ///     Constructor to contruct a DeviceContext object from an existing Win32 device context handle.
        /// </devdoc>


        private DeviceContext(IntPtr hDC, DeviceContextType dcType)
        {
            this.hDC    = hDC;
            this.dcType = dcType;

            CacheInitialState();
            DeviceContexts.AddDeviceContext(this);

            if (dcType == DeviceContextType.Display)
            {
                this.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
        }
Ejemplo n.º 29
0
        /// <devdoc>
        ///     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.
        /// </devdoc>
        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, 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 !DRAWING_NAMESPACE
                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();
                    }
                }
#endif
            }

#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(this.hDC);
#endif
        }
Ejemplo n.º 30
0
        /// <devdoc>
        ///     Selects the specified object into the dc and returns the old object.
        /// </devdoc>
        public IntPtr SelectObject(IntPtr hObj, GdiObjectType type)
        {
            switch (type)
            {
            case GdiObjectType.Pen:
                hCurrentPen = hObj;
                break;

            case GdiObjectType.Brush:
                hCurrentBrush = hObj;
                break;

            case GdiObjectType.Bitmap:
                hCurrentBmp = hObj;
                break;
            }
            return(IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(null, hObj)));
        }