Exemple #1
0
        public static IntPtr Capture(IntPtr windowHandle, int x, int y, int width, int height)
        {
            if (width < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }
            if (height < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }
            if (width == 0 || height == 0)
            {
                return(IntPtr.Zero);
            }
            IntPtr hSource        = Window.GetWindowDC(windowHandle);
            IntPtr hMemory        = GDI.CreateCompatibleDC(hSource);
            IntPtr hBitmap        = GDI.CreateCompatibleBitmap(hSource, width, height);
            IntPtr hPrimaryBitmap = GDI.SelectObject(hMemory, hBitmap);

            GDI.BitBlt(hMemory, 0, 0, width, height, hSource, x, y, GDI.RasterOperationCodes.SRCCOPY);
            GDI.SelectObject(hMemory, hPrimaryBitmap);
            GDI.DeleteDC(hMemory);
            Window.ReleaseDC(windowHandle, hSource);
            return(hBitmap);
        }
Exemple #2
0
        private void Create(Description description)
        {
            TextureAllocator = description.TextureAllocator;
            TextureAllocator.GarbageCollectionCollectEvent.Add(delegate(RenderingTextureAllocator allocator, RenderingTextureAllocator.Map map, HashSet <RenderingTextureAllocator.Map.Entry> inOutUsedTextures)
            {
                // Clean dictionary for weak references that died.
                List <GlyphIndex> GlyphKeysToRemove = new List <GlyphIndex>();
                GlyphData unused;
                foreach (KeyValuePair <GlyphIndex, WeakReference <GlyphData> > glyph in _glyphDictionary)
                {
                    if (!glyph.Value.TryGetTarget(out unused))
                    {
                        GlyphKeysToRemove.Add(glyph.Key);
                    }
                }
                foreach (GlyphIndex GlyphKeyToRemove in GlyphKeysToRemove)
                {
                    _glyphDictionary.Remove(GlyphKeyToRemove);
                }
                return(null);
            });

            // Setup GDI device and font
            _gdiHdc = GDI.CreateCompatibleDC(IntPtr.Zero);
            if (_gdiHdc == IntPtr.Zero)
            {
                throw new GDI.GDIException("CreateCompatibleDC");
            }
            _gdiFont = GDI.CreateFontW((int)(description.FontSize + 0.5f), 0, 0, 0, description.FontIsBold ? 700 : 0,
                                       description.FontIsItalic ? 1u : 0u, description.FontIsUnderline ? 1u : 0u, description.FontIsStrikeout ? 1u : 0u, 0, 0, 0,
                                       GDI.FontQuality.CLEARTYPE_QUALITY, 0, description.FontName);
            if (_gdiFont == IntPtr.Zero)
            {
                throw new GDI.GDIException("CreateFont");
            }
            IntPtr selectObjectResult = GDI.SelectObject(_gdiHdc, _gdiFont);

            if (selectObjectResult == IntPtr.Zero || selectObjectResult == new IntPtr(65535))
            {
                throw new GDI.GDIException("SelectObject");
            }
            _gdiGetCharacterPlacementOrder  = Marshal.AllocHGlobal(sizeof(uint) * _gdiGetCharacterPlacementGlyphCount);
            _gdiGetCharacterPlacementDx     = Marshal.AllocHGlobal(sizeof(int) * _gdiGetCharacterPlacementGlyphCount);
            _gdiGetCharacterPlacementGlpyhs = Marshal.AllocHGlobal(sizeof(GlyphIndex) * _gdiGetCharacterPlacementGlyphCount);
            GDI.TEXTMETRICW textMetric;
            if (!GDI.GetTextMetricsW(_gdiHdc, out textMetric))
            {
                throw new GDI.GDIException("GetTextMetricsW");
            }
            _lineAscent   = textMetric.tmAscent;
            _lineSpaceing = textMetric.tmAscent + textMetric.tmDescent + textMetric.tmExternalLeading;
            if (GDI.SetBkColor(_gdiHdc, 0x00ffffff) == 0xffffffff) // White background (GDI does not support alpha though)
            {
                throw new GDI.GDIException("SetBkColor");
            }

            // Preload characters
            ParseString(description.PreLoadedCharacters, false, new List <GlyphRenderInfo>());
        }
Exemple #3
0
        public void UpdateView()
        {
            if ((FUpdateCount == 0) && IsHandleCreated)
            {
                if (FMemDC != IntPtr.Zero)
                {
                    GDI.DeleteDC(FMemDC);
                }
                FMemDC = (IntPtr)GDI.CreateCompatibleDC(FDC);

                int    vViewWidth  = GetViewWidth();
                int    vViewHeight = GetViewHeight();
                IntPtr vBitmap     = (IntPtr)GDI.CreateCompatibleBitmap(FDC, vViewWidth, vViewHeight);
                GDI.SelectObject(FMemDC, vBitmap);
                try
                {
                    using (HCCanvas vDataBmpCanvas = new HCCanvas(FMemDC))
                    {
                        // 控件背景
                        vDataBmpCanvas.Brush.Color = Color.White;// $00E7BE9F;
                        vDataBmpCanvas.FillRect(new RECT(0, 0, vViewWidth, vViewHeight));

                        PaintInfo vPaintInfo = new PaintInfo();
                        try
                        {
                            FData.PaintData(this.Padding.Left - FHScrollBar.Position, // 当前页数据要绘制到的Left
                                            this.Padding.Top,                         // 当前页数据要绘制到的Top
                                            this.Width - FHScrollBar.Position - this.Padding.Right,
                                            this.Padding.Top + FData.Height,          // 当前页数据要绘制的Bottom
                                            this.Padding.Top,                         // 界面呈现当前页数据的Top位置
                                            this.Height - FHScrollBar.Height,         // 界面呈现当前页数据Bottom位置
                                            FVScrollBar.Position,                     // 指定从哪个位置开始的数据绘制到页数据起始位置
                                            vDataBmpCanvas,
                                            vPaintInfo);

                            for (int i = 0; i <= vPaintInfo.TopItems.Count - 1; i++)  // 绘制顶层Ite
                            {
                                vPaintInfo.TopItems[i].PaintTop(vDataBmpCanvas);
                            }
                        }
                        finally
                        {
                            vPaintInfo.Dispose();
                        }

                        GDI.BitBlt(FDC, 0, 0, vViewWidth, vViewHeight, FMemDC, 0, 0, GDI.SRCCOPY);
                    }
                }
                finally
                {
                    GDI.DeleteObject(vBitmap);
                }

                RECT vRect = new RECT(0, 0, vViewWidth, vViewHeight);
                User.InvalidateRect(this.Handle, ref vRect, 0);  // 只更新变动区域,防止闪烁,解决BitBlt光标滞留问题
                User.UpdateWindow(this.Handle);
            }
        }
Exemple #4
0
        public void SetView(Bitmap newView, int newOpacity)
        {
            if (newView == null)
            {
                return;
            }

            if (newView.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            View    = newView;
            Opacity = newOpacity % 256;

            IntPtr screenDc  = User.GetDC(IntPtr.Zero);
            IntPtr memDc     = GDI.CreateCompatibleDC(screenDc);
            IntPtr hBitmap   = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                hBitmap   = newView.GetHbitmap(Color.FromArgb(0));
                oldBitmap = GDI.SelectObject(memDc, hBitmap);

                var size = new SIZE {
                    cx = newView.Width, cy = newView.Height
                };
                var pointSource = new POINT {
                    x = 0, y = 0
                };
                var topPos = new POINT {
                    x = Left, y = Top
                };
                var blend = new User.BLENDFUNCTION
                {
                    BlendOp             = User.AC_SRC_OVER,
                    BlendFlags          = 0,
                    SourceConstantAlpha = (byte)newOpacity,
                    AlphaFormat         = User.AC_SRC_ALPHA
                };

                User.UpdateLayeredWindow(Handle, screenDc, ref topPos,
                                         ref size, memDc, ref pointSource,
                                         0,
                                         ref blend, User.ULW_ALPHA);
            }
            finally
            {
                User.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    GDI.SelectObject(memDc, oldBitmap);
                    GDI.DeleteObject(hBitmap);
                }
                GDI.DeleteDC(memDc);
            }
        }
Exemple #5
0
        public static HCCanvas CreateStyleCanvas()
        {
            //IntPtr vScreenDC = User.GetDC(IntPtr.Zero);
            IntPtr vDC = (IntPtr)GDI.CreateCompatibleDC(IntPtr.Zero);

            HCCanvas Result = new HCCanvas(vDC);

            return(Result);
        }
        public override void PaintTop(HCCanvas aCanvas)
        {
            if (this.Resizing)
            {
                Bitmap vBitmap = FImage;

                //如果原图片是索引像素格式之列的,则需要转换
                if (IsPixelFormatIndexed(FImage.PixelFormat))
                {
                    vBitmap = new Bitmap(FImage.Width, FImage.Height, PixelFormat.Format32bppArgb);
                }

                using (Graphics vGraphicSrc = Graphics.FromImage(vBitmap))
                {
                    BLENDFUNCTION vBlendFunction = new BLENDFUNCTION();
                    vBlendFunction.BlendOp             = GDI.AC_SRC_OVER;
                    vBlendFunction.BlendFlags          = 0;
                    vBlendFunction.AlphaFormat         = GDI.AC_SRC_OVER; // 通常为 0,如果源位图为32位真彩色,可为 AC_SRC_ALPHA
                    vBlendFunction.SourceConstantAlpha = 128;             // 透明度

                    IntPtr vImageHDC = vGraphicSrc.GetHdc();
                    try
                    {
                        IntPtr vMemDC   = (IntPtr)GDI.CreateCompatibleDC(vImageHDC);
                        IntPtr vHbitmap = FImage.GetHbitmap();// (IntPtr)GDI.CreateCompatibleBitmap(vImageHDC, FImage.Width, FImage.Height);
                        GDI.SelectObject(vMemDC, vHbitmap);

                        GDI.AlphaBlend(
                            aCanvas.Handle,
                            ResizeRect.Left,
                            ResizeRect.Top,
                            ResizeWidth,
                            ResizeHeight,
                            vMemDC,
                            0,
                            0,
                            FImage.Width,
                            FImage.Height,
                            vBlendFunction);

                        GDI.DeleteDC(vMemDC);
                        GDI.DeleteObject(vHbitmap);
                    }
                    finally
                    {
                        vGraphicSrc.ReleaseHdc(vImageHDC);
                    }
                }

                if (!vBitmap.Equals(FImage))
                {
                    vBitmap.Dispose();
                }
            }

            base.PaintTop(aCanvas);
        }
        public Face LoadSystemFace(Library library, string face, ref FontStyles style)
        {
            int  weight    = GDI.FW_REGULAR;
            uint fdwItalic = 0;

            //Map style
            if ((style & FontStyles.Bold) == FontStyles.Bold)
            {
                weight = GDI.FW_BOLD;
            }
            if ((style & FontStyles.Italic) == FontStyles.Italic)
            {
                fdwItalic = 1;
            }
            //Get font data from GDI
            byte[] buffer;
            unsafe {
                var hfont = GDI.CreateFont(0, 0, 0, 0, weight,
                                           fdwItalic, 0, 0, GDI.DEFAULT_CHARSET, GDI.OUT_OUTLINE_PRECIS,
                                           GDI.CLIP_DEFAULT_PRECIS, GDI.DEFAULT_QUALITY,
                                           GDI.DEFAULT_PITCH, face);
                //get data
                var hdc = GDI.CreateCompatibleDC(IntPtr.Zero);
                GDI.SelectObject(hdc, hfont);
                var size = GDI.GetFontData(hdc, 0, 0, IntPtr.Zero, 0);
                buffer = new byte[size];
                fixed(byte *ptr = buffer)
                {
                    GDI.GetFontData(hdc, 0, 0, (IntPtr)ptr, size);
                }

                GDI.DeleteDC(hdc);
                //delete font
                GDI.DeleteObject(hfont);
            }
            //create font object
            //TODO: Handle multi-face data on Windows. (Is that a thing?)
            var fc = new Face(library, buffer, 0);
            //Extract style
            var fs = FontStyles.Regular;

            if ((fc.StyleFlags & StyleFlags.Bold) != 0)
            {
                fs |= FontStyles.Bold;
            }
            if ((fc.StyleFlags & StyleFlags.Italic) != 0)
            {
                fs |= FontStyles.Italic;
            }
            style = fs;
            //return font
            return(fc);
        }
Exemple #8
0
        private void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The idea of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

            IntPtr screenDc  = GDI.GetDC(IntPtr.Zero);
            IntPtr memDc     = GDI.CreateCompatibleDC(screenDc);
            IntPtr hBitmap   = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                oldBitmap = GDI.SelectObject(memDc, hBitmap);

                GDI.Size          size        = new GDI.Size(bitmap.Width, bitmap.Height);
                GDI.Point         pointSource = new GDI.Point(0, 0);
                GDI.Point         topPos      = new GDI.Point(Left, Top);
                GDI.BLENDFUNCTION blend       = new GDI.BLENDFUNCTION();
                blend.BlendOp             = GDI.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = GDI.AC_SRC_ALPHA;

                GDI.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, GDI.ULW_ALPHA);
            }
            finally
            {
                GDI.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    GDI.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap);
                    // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from GDI GDI and it's working fine without any resource leak.
                    GDI.DeleteObject(hBitmap);
                }
                GDI.DeleteDC(memDc);
            }
        }
Exemple #9
0
        static bool GdiOpenFace(string face, FontStyles style, out byte[] buffer)
        {
            int  weight    = GDI.FW_REGULAR;
            uint fdwItalic = 0;

            //Map style
            if ((style & FontStyles.Bold) == FontStyles.Bold)
            {
                weight = GDI.FW_BOLD;
            }
            if ((style & FontStyles.Italic) == FontStyles.Italic)
            {
                fdwItalic = 1;
            }
            //Get font data from GDI
            buffer = null;
            unsafe
            {
                var hfont = GDI.CreateFont(0, 0, 0, 0, weight,
                                           fdwItalic, 0, 0, GDI.DEFAULT_CHARSET, GDI.OUT_OUTLINE_PRECIS,
                                           GDI.CLIP_DEFAULT_PRECIS, GDI.DEFAULT_QUALITY,
                                           GDI.DEFAULT_PITCH, face);
                //get data
                var hdc = GDI.CreateCompatibleDC(IntPtr.Zero);
                GDI.SelectObject(hdc, hfont);
                var size = GDI.GetFontData(hdc, 0, 0, IntPtr.Zero, 0);
                if (size == GDI.GDI_ERROR)
                {
                    FLLog.Warning("GDI", "GetFontData for " + face + " failed");
                    GDI.DeleteDC(hdc);
                    GDI.DeleteObject(hfont);
                    return(false);
                }
                buffer = new byte[size];
                fixed(byte *ptr = buffer)
                {
                    GDI.GetFontData(hdc, 0, 0, (IntPtr)ptr, size);
                }

                GDI.DeleteDC(hdc);
                //delete font
                GDI.DeleteObject(hfont);
                return(true);
            }
        }
        public override void PaintTop(HCCanvas ACanvas)
        {
            using (Graphics vGraphicSrc = Graphics.FromImage(FImage))
            {
                BLENDFUNCTION vBlendFunction = new BLENDFUNCTION();
                vBlendFunction.BlendOp             = GDI.AC_SRC_OVER;
                vBlendFunction.BlendFlags          = 0;
                vBlendFunction.AlphaFormat         = GDI.AC_SRC_OVER; // 通常为 0,如果源位图为32位真彩色,可为 AC_SRC_ALPHA
                vBlendFunction.SourceConstantAlpha = 128;             // 透明度


                IntPtr vImageHDC = vGraphicSrc.GetHdc();
                try
                {
                    IntPtr vMemDC  = (IntPtr)GDI.CreateCompatibleDC(vImageHDC);
                    IntPtr vBitmap = FImage.GetHbitmap();// (IntPtr)GDI.CreateCompatibleBitmap(vImageHDC, FImage.Width, FImage.Height);
                    GDI.SelectObject(vMemDC, vBitmap);

                    GDI.AlphaBlend(
                        ACanvas.Handle,
                        ResizeRect.Left,
                        ResizeRect.Top,
                        ResizeWidth,
                        ResizeHeight,
                        vMemDC,
                        0,
                        0,
                        FImage.Width,
                        FImage.Height,
                        vBlendFunction);

                    GDI.DeleteDC(vMemDC);
                    GDI.DeleteObject(vBitmap);
                }
                finally
                {
                    vGraphicSrc.ReleaseHdc(vImageHDC);
                }
            }

            base.PaintTop(ACanvas);
        }
Exemple #11
0
        public static void Initialization()
        {
            IntPtr vDC = (IntPtr)GDI.CreateCompatibleDC(IntPtr.Zero);

            try
            {
                PixelsPerInchX = GDI.GetDeviceCaps(vDC, GDI.LOGPIXELSX);  // 每英寸水平逻辑像素数,1英寸dpi数
                PixelsPerInchY = GDI.GetDeviceCaps(vDC, GDI.LOGPIXELSY);  // 每英寸水平逻辑像素数,1英寸dpi数
            }
            finally
            {
                GDI.DeleteDC(vDC);
            }

            FontSizeScale = 72.0f / PixelsPerInchX;

            // 1英寸25.4毫米   FPixelsPerInchX
            PixelsPerMMX = PixelsPerInchX / 25.4f;  // 1毫米对应像素 = 1英寸dpi数 / 1英寸对应毫米
            PixelsPerMMY = PixelsPerInchY / 25.4f;  // 1毫米对应像素 = 1英寸dpi数 / 1英寸对应毫米
        }
 public void StretchPrintDrawBitmap(RECT rect, Bitmap bitmap)
 {
     using (Graphics srcgraphics = Graphics.FromImage(bitmap))
     {
         IntPtr vImageHDC = srcgraphics.GetHdc();
         try
         {
             IntPtr vMemDC   = (IntPtr)GDI.CreateCompatibleDC(vImageHDC);
             IntPtr vHbitmap = bitmap.GetHbitmap();// (IntPtr)GDI.CreateCompatibleBitmap(vImageHDC, FImage.Width, FImage.Height);
             GDI.SelectObject(vMemDC, vHbitmap);
             //GDI.BitBlt(aCanvas.Handle, aDrawRect.Left, aDrawRect.Top, aDrawRect.Width, aDrawRect.Height, vMemDC, 0, 0, GDI.SRCCOPY);
             GDI.StretchBlt(FHandle, rect.Left, rect.Top, rect.Width, rect.Height, vMemDC, 0, 0, bitmap.Width, bitmap.Height, GDI.SRCCOPY);
             GDI.DeleteDC(vMemDC);
             GDI.DeleteObject(vHbitmap);
         }
         finally
         {
             srcgraphics.ReleaseHdc(vImageHDC);
         }
     }
 }