Ejemplo n.º 1
0
 /// <summary>
 /// Creates new memory DC and bitmap of specified size and selects it into the DC.
 /// Returns false if failed.
 /// In any case deletes previous bitmap and DC.
 /// </summary>
 /// <param name="width">Width, pixels. Must be &gt; 0.</param>
 /// <param name="height">Height, pixels. Must be &gt; 0.</param>
 public bool Create(int width, int height)
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(nameof(MemoryBitmap));
     }
     using var dcs = new ScreenDC_();
     Attach(Api.CreateCompatibleBitmap(dcs, width, height));
     return(_bm != default);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets color of a screen pixel.
        /// </summary>
        /// <param name="p">x y in screen.</param>
        /// <returns>Pixel color in 0xAARRGGBB format. Alpha 0xFF. Returns 0 if fails, eg if x y is not in screen.</returns>
        /// <remarks>
        /// Uses, API <msdn>GetPixel</msdn>. It is slow. If need faster, try <see cref="getPixels(wnd, RECT, bool)"/> (get 1 or more pixels from window client area).
        /// </remarks>
        /// <example>
        /// <code><![CDATA[
        /// //print.clear();
        /// //for (;;) {
        /// //	1.s();
        /// //	print.it(uiimage.getPixel(mouse.xy));
        /// //}
        /// ]]></code>
        /// </example>
        public static unsafe uint getPixel(POINT p)
        {
            using var dc = new ScreenDC_();
            //using var dc = new WindowDC_(wnd.getwnd.root); //same speed. Same with printwindow.
            uint R = Api.GetPixel(dc, p.x, p.y);

            if (R == 0xFFFFFFFF)
            {
                return(0);                             //it's better than exception
            }
            return(ColorInt.SwapRB(R) | 0xFF000000);
        }
Ejemplo n.º 3
0
        public NativeFont_(string name, int height, FontStyle style = default, bool calculateHeightOnScreen = false)
        {
            using var dcs = new ScreenDC_(0);
            int h2 = -AMath.MulDiv(height, Api.GetDeviceCaps(dcs, 90), 72);             //LOGPIXELSY=90

            Handle = Api.CreateFont(h2,
                                    cWeight: style.Has(FontStyle.Bold) ? 700 : 0, //FW_BOLD
                                    bItalic: style.Has(FontStyle.Italic) ? 1 : 0,
                                    bUnderline: style.Has(FontStyle.Underline) ? 1 : 0,
                                    bStrikeOut: style.Has(FontStyle.Strikeout) ? 1 : 0,
                                    iCharSet: 1,
                                    pszFaceName: name);
            if (calculateHeightOnScreen)
            {
                using var dcMem = new CompatibleDC_(dcs);
                var of = Api.SelectObject(dcMem, Handle);
                Api.GetTextExtentPoint32(dcMem, "A", 1, out var z);
                HeightOnScreen = z.height;
                Api.SelectObject(dcMem, of);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates Bitmap from a GDI bitmap.
        /// </summary>
        /// <param name="hbitmap">GDI bitmap handle. This function makes its copy.</param>
        /// <remarks>
        /// How this function is different from <see cref="Image.FromHbitmap"/>:
        /// 1. Image.FromHbitmap usually creates bottom-up bitmap, which is incompatible with <see cref="Find"/>. This function creates normal top-down bitmap, like <c>new Bitmap(...)</c>, <c>Bitmap.FromFile(...)</c> etc do.
        /// 2. This function always creates bitmap of PixelFormat Format32bppRgb.
        /// </remarks>
        /// <exception cref="AuException">Failed. For example hbitmap is default(IntPtr).</exception>
        /// <exception cref="Exception">Exceptions of Bitmap(int, int, PixelFormat) constructor.</exception>
        public static unsafe Bitmap BitmapFromHbitmap(IntPtr hbitmap)
        {
            var bh = new Api.BITMAPINFOHEADER()
            {
                biSize = sizeof(Api.BITMAPINFOHEADER)
            };

            using (var dcs = new ScreenDC_(0)) {
                if (0 == Api.GetDIBits(dcs, hbitmap, 0, 0, null, &bh, 0))
                {
                    goto ge;
                }
                int wid = bh.biWidth, hei = bh.biHeight;
                if (hei > 0)
                {
                    bh.biHeight = -bh.biHeight;
                }
                else
                {
                    hei = -hei;
                }
                bh.biBitCount = 32;

                var  R  = new Bitmap(wid, hei, PixelFormat.Format32bppRgb);
                var  d  = R.LockBits(new Rectangle(0, 0, wid, hei), ImageLockMode.ReadWrite, R.PixelFormat);
                bool ok = hei == Api.GetDIBits(dcs, hbitmap, 0, hei, (void *)d.Scan0, &bh, 0);
                R.UnlockBits(d);
                if (!ok)
                {
                    R.Dispose(); goto ge;
                }
                return(R);
            }
ge:
            throw new AuException();
        }
Ejemplo n.º 5
0
            public _Metrics(popupMenu m)
            {
                foreach (var b in m._a)
                {
                    if (b.IsSeparator)
                    {
                        hasSeparators = true;
                    }
                    else
                    {
                        if (b.HasImage_)
                        {
                            hasImages = true;
                        }
                        if (b.checkType > 0)
                        {
                            hasCheck = true;
                        }
                        if (b.IsSubmenu)
                        {
                            hasSubmenus = true;
                        }
                        if (b.Hotkey != null)
                        {
                            hasHotkeys = true;
                        }
                    }
                }

                int dpi = m._dpi;

                bBorder      = dpi / 96;
                textPaddingX = Dpi.Scale(8, dpi);
                textPaddingY = Dpi.Scale(1, dpi);
                if (hasImages)
                {
                    image = Dpi.Scale(16, dpi);
                }
                if (hasCheck)
                {
                    check = Dpi.Scale(18, dpi);
                }
                if (hasSubmenus)
                {
                    submenu = Dpi.Scale(16, dpi);
                }
                separator = Dpi.Scale(8, dpi);
                sepLine   = 2;

                theme = Api.OpenThemeData(m._w, "Menu");
                if (theme != default)
                {
                    using var dc = new ScreenDC_();
                    if (hasSubmenus)
                    {
                        Api.GetThemePartSize(theme, dc, 16, 1, null, Api.THEMESIZE.TS_TRUE, out zSubmenu);
                        submenu = Math.Max(submenu, zSubmenu.width + submenu / 4);
                    }
                    if (hasCheck)
                    {
                        Api.GetThemePartSize(theme, dc, 11, 1, null, Api.THEMESIZE.TS_TRUE, out zCheck);
                        check = Math.Max(check, zCheck.width + 2);
                    }
                    if (hasSeparators && 0 == Api.GetThemePartSize(theme, dc, 15, 0, null, Api.THEMESIZE.TS_TRUE, out var k))
                    {
                        sepLine = k.height;
                    }
                }

                submenuMargin = hasHotkeys ? 0 : submenu;
            }