Beispiel #1
0
        static public Color ColorFromPoint(Graphics g, int x, int y)
        {
            IntPtr hDC = g.GetHdc();
            // Get the color of the pixel first
            uint colorref = APIsGdi.GetPixel(hDC, x, y);
            byte Red      = GetRValue(colorref);
            byte Green    = GetGValue(colorref);
            byte Blue     = GetBValue(colorref);

            g.ReleaseHdc(hDC);
            return(Color.FromArgb(Red, Green, Blue));
        }
Beispiel #2
0
        public static void DrawText(Graphics graphics, string text, Font font, Rectangle rect)
        {
            IntPtr hdc               = graphics.GetHdc();
            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = APIsGdi.SelectObject(hdc, fontHandle);

            APIsGdi.SetBkMode(hdc, APIsEnums.BackgroundMode.TRANSPARENT);

            APIsStructs.RECT rc = new APIsStructs.RECT();
            rc.left   = rect.Left;
            rc.top    = rect.Top;
            rc.right  = rc.left + rect.Width;
            rc.bottom = rc.top + rect.Height;

            APIsUser32.DrawText(hdc, text, text.Length, ref rc,
                                APIsEnums.DrawTextFormatFlags.SINGLELINE | APIsEnums.DrawTextFormatFlags.LEFT
                                | APIsEnums.DrawTextFormatFlags.MODIFYSTRING | APIsEnums.DrawTextFormatFlags.WORD_ELLIPSIS);
            APIsGdi.SelectObject(hdc, currentFontHandle);
            APIsGdi.DeleteObject(fontHandle);
            graphics.ReleaseHdc(hdc);
        }
Beispiel #3
0
        public static Size GetTextSize(Graphics graphics, string text, Font font)
        {
            IntPtr hdc = IntPtr.Zero;

            if (graphics != null)
            {
                // Get device context from the graphics passed in
                hdc = graphics.GetHdc();
            }
            else
            {
                // Get screen device context
                hdc = APIsGdi.GetDC(IntPtr.Zero);
            }

            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = APIsGdi.SelectObject(hdc, fontHandle);

            APIsStructs.RECT rect = new APIsStructs.RECT();
            rect.left   = 0;
            rect.right  = 0;
            rect.top    = 0;
            rect.bottom = 0;

            APIsUser32.DrawText(hdc, text, text.Length, ref rect,
                                APIsEnums.DrawTextFormatFlags.SINGLELINE | APIsEnums.DrawTextFormatFlags.LEFT | APIsEnums.DrawTextFormatFlags.CALCRECT);
            APIsGdi.SelectObject(hdc, currentFontHandle);
            APIsGdi.DeleteObject(fontHandle);

            if (graphics != null)
            {
                graphics.ReleaseHdc(hdc);
            }
            else
            {
                APIsUser32.ReleaseDC(IntPtr.Zero, hdc);
            }

            return(new Size(rect.right - rect.left, rect.bottom - rect.top));
        }