Esempio n. 1
0
        // this code is adapted from Icon.GetIconSize please take this into account when changing this
        private Size GetIconSize(IntPtr iconHandle)
        {
            Size iconSize = Size;

            NativeMethods.ICONINFO info = new NativeMethods.ICONINFO();
            SafeNativeMethods.GetIconInfo(new HandleRef(this, iconHandle), info);
            NativeMethods.BITMAP bmp = new NativeMethods.BITMAP();

            if (info.hbmColor != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp);
                SafeNativeMethods.DeleteObject(new HandleRef(null, info.hbmColor));
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight);
            }
            else if (info.hbmMask != IntPtr.Zero)
            {
                UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmMask), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp);
                iconSize = new Size(bmp.bmWidth, bmp.bmHeight / 2);
            }

            if (info.hbmMask != IntPtr.Zero)
            {
                SafeNativeMethods.DeleteObject(new HandleRef(null, info.hbmMask));
            }
            return(iconSize);
        }
Esempio n. 2
0
        /// <summary>
        /// This code is required to set the correct window region during the resize of the Form at design time.
        /// There is case when the form contains a MainMenu and also has IsMdiContainer property set, in which, the MdiClient fails to
        /// resize and hence draw the correct backcolor.
        /// </summary>
        private void SetWindowRgn()
        {
            IntPtr rgn1 = IntPtr.Zero;
            IntPtr rgn2 = IntPtr.Zero;

            NativeMethods.RECT rect = new NativeMethods.RECT();
            CreateParams       cp   = CreateParams;

            AdjustWindowRectEx(ref rect, cp.Style, false, cp.ExStyle);

            Rectangle bounds = Bounds;

            rgn1 = SafeNativeMethods.CreateRectRgn(0, 0, bounds.Width, bounds.Height);
            try
            {
                rgn2 = SafeNativeMethods.CreateRectRgn(-rect.left, -rect.top,
                                                       bounds.Width - rect.right, bounds.Height - rect.bottom);
                try
                {
                    if (rgn1 == IntPtr.Zero || rgn2 == IntPtr.Zero)
                    {
                        throw new InvalidOperationException(SR.ErrorSettingWindowRegion);
                    }

                    if (SafeNativeMethods.CombineRgn(new HandleRef(null, rgn1), new HandleRef(null, rgn1), new HandleRef(null, rgn2), NativeMethods.RGN_DIFF) == 0)
                    {
                        throw new InvalidOperationException(SR.ErrorSettingWindowRegion);
                    }

                    if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(null, rgn1), true) == 0)
                    {
                        throw new InvalidOperationException(SR.ErrorSettingWindowRegion);
                    }
                    else
                    {
                        // The hwnd now owns the region.
                        rgn1 = IntPtr.Zero;
                    }
                }
                finally
                {
                    if (rgn2 != IntPtr.Zero)
                    {
                        SafeNativeMethods.DeleteObject(new HandleRef(null, rgn2));
                    }
                }
            }
            finally
            {
                if (rgn1 != IntPtr.Zero)
                {
                    SafeNativeMethods.DeleteObject(new HandleRef(null, rgn1));
                }
            }
        }
Esempio n. 3
0
            public Image this[int index] {
                get {
                    if (index < 0 || index >= Count)
                    {
                        throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument,
                                                                           "index",
                                                                           index.ToString()));
                    }
                    return(owner.GetBitmap(index));
                }
                set {
                    if (index < 0 || index >= Count)
                    {
                        throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument,
                                                                           "index",
                                                                           index.ToString()));
                    }

                    if (value == null)
                    {
                        throw new ArgumentNullException("value");
                    }

                    if (!(value is Bitmap))
                    {
                        throw new ArgumentException(SR.GetString(SR.ImageListBitmap));
                    }


                    AssertInvariant();

                    Bitmap bitmap = (Bitmap)value;

                    if (owner.UseTransparentColor)
                    {
                        // Since there's no ImageList_ReplaceMasked, we need to generate
                        // a transparent bitmap
                        Bitmap source = bitmap;
                        bitmap = (Bitmap)bitmap.Clone();
                        bitmap.MakeTransparent(owner.transparentColor);
                    }


                    IntPtr hMask   = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
                    IntPtr hBitmap = ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
                    bool   ok      = SafeNativeMethods.ImageList_Replace(new HandleRef(owner, owner.Handle), index, new HandleRef(null, hBitmap), new HandleRef(null, hMask));
                    SafeNativeMethods.DeleteObject(new HandleRef(null, hBitmap));
                    SafeNativeMethods.DeleteObject(new HandleRef(null, hMask));

                    if (!ok)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.ImageListReplaceFailed));
                    }
                }
            }
Esempio n. 4
0
            private void InvalidateNonClient()
            {
                if (!IsPopupTextBox)
                {
                    return;
                }
                NativeMethods.RECT absoluteClientRectangle = AbsoluteClientRECT;
                HandleRef          hNonClientRegion        = NativeMethods.NullHandleRef;
                HandleRef          hClientRegion           = NativeMethods.NullHandleRef;
                HandleRef          hTotalRegion            = NativeMethods.NullHandleRef;

                try {
                    // get the total client area, then exclude the client by using XOR
                    hTotalRegion     = new HandleRef(this, SafeNativeMethods.CreateRectRgn(0, 0, this.Width, this.Height));
                    hClientRegion    = new HandleRef(this, SafeNativeMethods.CreateRectRgn(absoluteClientRectangle.left, absoluteClientRectangle.top, absoluteClientRectangle.right, absoluteClientRectangle.bottom));
                    hNonClientRegion = new HandleRef(this, SafeNativeMethods.CreateRectRgn(0, 0, 0, 0));

                    SafeNativeMethods.CombineRgn(hNonClientRegion, hTotalRegion, hClientRegion, NativeMethods.RGN_XOR);

                    // Call RedrawWindow with the region.
                    NativeMethods.RECT ignored = new NativeMethods.RECT();
                    SafeNativeMethods.RedrawWindow(new HandleRef(this, Handle),
                                                   ref ignored, hNonClientRegion,
                                                   NativeMethods.RDW_INVALIDATE | NativeMethods.RDW_ERASE |
                                                   NativeMethods.RDW_UPDATENOW | NativeMethods.RDW_ERASENOW |
                                                   NativeMethods.RDW_FRAME);
                }
                finally {
                    // clean up our regions.
                    try {
                        if (hNonClientRegion.Handle != IntPtr.Zero)
                        {
                            SafeNativeMethods.DeleteObject(hNonClientRegion);
                        }
                    }
                    finally {
                        try {
                            if (hClientRegion.Handle != IntPtr.Zero)
                            {
                                SafeNativeMethods.DeleteObject(hClientRegion);
                            }
                        }
                        finally {
                            if (hTotalRegion.Handle != IntPtr.Zero)
                            {
                                SafeNativeMethods.DeleteObject(hTotalRegion);
                            }
                        }
                    }
                }
            }
Esempio n. 5
0
        // Adds bitmap to the Imagelist handle...
        //
        private int AddToHandle(Original original, Bitmap bitmap)
        {
            Debug.Assert(HandleCreated, "Calling AddToHandle when there is no handle");
            IntPtr hMask   = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
            IntPtr hBitmap = ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
            int    index   = SafeNativeMethods.ImageList_Add(new HandleRef(this, Handle), new HandleRef(null, hBitmap), new HandleRef(null, hMask));

            SafeNativeMethods.DeleteObject(new HandleRef(null, hBitmap));
            SafeNativeMethods.DeleteObject(new HandleRef(null, hMask));

            if (index == -1)
            {
                throw new InvalidOperationException(SR.GetString(SR.ImageListAddFailed));
            }
            return(index);
        }
        internal IntPtr CreateHalftoneHBRUSH()
        {
            short[] array = new short[8];
            for (int i = 0; i < 8; i++)
            {
                array[i] = (short)(21845 << (i & 1));
            }
            IntPtr intPtr = SafeNativeMethods.CreateBitmap(8, 8, 1, 1, array);

            NativeMethods.LOGBRUSH lOGBRUSH = new NativeMethods.LOGBRUSH();
            lOGBRUSH.lbColor = ColorTranslator.ToWin32(Color.Black);
            lOGBRUSH.lbStyle = 3;
            lOGBRUSH.lbHatch = intPtr;
            IntPtr result = SafeNativeMethods.CreateBrushIndirect(lOGBRUSH);

            SafeNativeMethods.DeleteObject(new HandleRef(null, intPtr));
            return(result);
        }
Esempio n. 7
0
        /// <include file='doc\Splitter.uex' path='docs/doc[@for="Splitter.DrawSplitHelper"]/*' />
        /// <devdoc>
        ///     Draws the splitter line at the requested location. Should only be called
        ///     by drawSpltBar.
        /// </devdoc>
        /// <internalonly/>
        private void DrawSplitHelper(int splitSize)
        {
            if (splitTarget == null)
            {
                return;
            }

            Rectangle r            = CalcSplitLine(splitSize, 3);
            IntPtr    parentHandle = ParentInternal.Handle;
            IntPtr    dc           = UnsafeNativeMethods.GetDCEx(new HandleRef(ParentInternal, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
            IntPtr    halftone     = ControlPaint.CreateHalftoneHBRUSH();
            IntPtr    saveBrush    = SafeNativeMethods.SelectObject(new HandleRef(ParentInternal, dc), new HandleRef(null, halftone));

            SafeNativeMethods.PatBlt(new HandleRef(ParentInternal, dc), r.X, r.Y, r.Width, r.Height, NativeMethods.PATINVERT);
            SafeNativeMethods.SelectObject(new HandleRef(ParentInternal, dc), new HandleRef(null, saveBrush));
            SafeNativeMethods.DeleteObject(new HandleRef(null, halftone));
            UnsafeNativeMethods.ReleaseDC(new HandleRef(ParentInternal, parentHandle), new HandleRef(null, dc));
        }
Esempio n. 8
0
            protected override void Dispose(bool disposing)
            {
                Debug.Assert(disposing, "Never let a graphics buffer finalize!");

                Debug.WriteLineIf(DoubleBuffering.TraceInfo, "Dispose(" + disposing + ") {");
                Debug.Indent();

                if (disposing && compatGraphics != null)
                {
                    Debug.WriteLineIf(DoubleBuffering.TraceVerbose, "Disposing compatGraphics");
                    compatGraphics.Dispose();
                    compatGraphics = null;
                }
                if (oldBitmap != IntPtr.Zero && compatDC != IntPtr.Zero)
                {
                    Debug.WriteLineIf(DoubleBuffering.TraceVerbose, "restoring bitmap to DC");
                    SafeNativeMethods.SelectObject(new HandleRef(this, compatDC), new HandleRef(this, oldBitmap));
                    oldBitmap = IntPtr.Zero;
                }
                if (compatDC != IntPtr.Zero)
                {
                    Debug.WriteLineIf(DoubleBuffering.TraceVerbose, "delete compat DC");
                    UnsafeNativeMethods.DeleteDC(new HandleRef(this, compatDC));
                    compatDC = IntPtr.Zero;
                }
                if (dib != IntPtr.Zero)
                {
                    Debug.WriteLineIf(DoubleBuffering.TraceVerbose, "delete dib");
                    SafeNativeMethods.DeleteObject(new HandleRef(this, dib));
                    dib = IntPtr.Zero;
                }
                bufferWidth   = -1;
                bufferHeight  = -1;
                virtualWidth  = -1;
                virtualHeight = -1;
                Debug.Unindent();
                Debug.WriteLineIf(DoubleBuffering.TraceInfo, "}");
            }
Esempio n. 9
0
            public DCMapping(HandleRef hDC, Rectangle bounds)
            {
                if (hDC.Handle == IntPtr.Zero)
                {
                    throw new ArgumentNullException("hDC");
                }

                bool success;

                NativeMethods.POINT viewportOrg             = new NativeMethods.POINT();
                HandleRef           hOriginalClippingRegion = NativeMethods.NullHandleRef;

                NativeMethods.RegionFlags originalRegionType = NativeMethods.RegionFlags.NULLREGION;

                this.translatedBounds = bounds;
                this.graphics         = null;
                this.dc = DeviceContext.FromHdc(hDC.Handle);

                this.dc.SaveHdc();

                // Retrieve the x-coordinates and y-coordinates of the viewport origin for the specified device context.
                success = SafeNativeMethods.GetViewportOrgEx(hDC, viewportOrg);
                Debug.Assert(success, "GetViewportOrgEx() failed.");

                // Create a new rectangular clipping region based off of the bounds specified, shifted over by the x & y specified in the viewport origin.
                HandleRef hClippingRegion = new HandleRef(null, SafeNativeMethods.CreateRectRgn(viewportOrg.x + bounds.Left, viewportOrg.y + bounds.Top, viewportOrg.x + bounds.Right, viewportOrg.y + bounds.Bottom));

                Debug.Assert(hClippingRegion.Handle != IntPtr.Zero, "CreateRectRgn() failed.");

                try
                {
                    // Create an empty region oriented at 0,0 so we can populate it with the original clipping region of the hDC passed in.
                    hOriginalClippingRegion = new HandleRef(this, SafeNativeMethods.CreateRectRgn(0, 0, 0, 0));
                    Debug.Assert(hOriginalClippingRegion.Handle != IntPtr.Zero, "CreateRectRgn() failed.");

                    // Get the clipping region from the hDC: result = {-1 = error, 0 = no region, 1 = success} per MSDN
                    int result = SafeNativeMethods.GetClipRgn(hDC, hOriginalClippingRegion);
                    Debug.Assert(result != -1, "GetClipRgn() failed.");

                    // Shift the viewpoint origint by coordinates specified in "bounds".
                    NativeMethods.POINT lastViewPort = new NativeMethods.POINT();
                    success = SafeNativeMethods.SetViewportOrgEx(hDC, viewportOrg.x + bounds.Left, viewportOrg.y + bounds.Top, lastViewPort);
                    Debug.Assert(success, "SetViewportOrgEx() failed.");

                    if (result != 0)
                    {
                        // Get the origninal clipping region so we can determine its type (we'll check later if we've restored the region back properly.)
                        NativeMethods.RECT originalClipRect = new NativeMethods.RECT();
                        originalRegionType = (NativeMethods.RegionFlags)SafeNativeMethods.GetRgnBox(hOriginalClippingRegion, ref originalClipRect);
                        Debug.Assert(originalRegionType != NativeMethods.RegionFlags.ERROR, "ERROR returned from SelectClipRgn while selecting the original clipping region..");

                        if (originalRegionType == NativeMethods.RegionFlags.SIMPLEREGION)
                        {
                            // Find the intersection of our clipping region and the current clipping region (our parent's)
                            //      Returns a NULLREGION, the two didn't intersect.
                            //      Returns a SIMPLEREGION, the two intersected
                            //      Resulting region (stuff that was in hOriginalClippingRegion AND hClippingRegion is placed in hClippingRegion
                            NativeMethods.RegionFlags combineResult = (NativeMethods.RegionFlags)SafeNativeMethods.CombineRgn(hClippingRegion, hClippingRegion, hOriginalClippingRegion, NativeMethods.RGN_AND);
                            Debug.Assert((combineResult == NativeMethods.RegionFlags.SIMPLEREGION) ||
                                         (combineResult == NativeMethods.RegionFlags.NULLREGION),
                                         "SIMPLEREGION or NULLREGION expected.");
                        }
                    }
                    else
                    {
                        // If there was no clipping region, then the result is a simple region.
                        // We don't need to keep track of the original now, since it is empty.
                        SafeNativeMethods.DeleteObject(hOriginalClippingRegion);
                        hOriginalClippingRegion = new HandleRef(null, IntPtr.Zero);
                        originalRegionType      = NativeMethods.RegionFlags.SIMPLEREGION;
                    }

                    // Select the new clipping region; make sure it's a SIMPLEREGION or NULLREGION
                    NativeMethods.RegionFlags selectResult = (NativeMethods.RegionFlags)SafeNativeMethods.SelectClipRgn(hDC, hClippingRegion);
                    Debug.Assert((selectResult == NativeMethods.RegionFlags.SIMPLEREGION ||
                                  selectResult == NativeMethods.RegionFlags.NULLREGION),
                                 "SIMPLEREGION or NULLLREGION expected.");
                }
                catch (Exception ex)
                {
                    if (ClientUtils.IsSecurityOrCriticalException(ex))
                    {
                        throw;
                    }

                    this.dc.RestoreHdc();
                    this.dc.Dispose();
                }
                finally {
                    // Delete the new clipping region, as the clipping region for the HDC is now set
                    // to this rectangle.  Hold on to hOriginalClippingRegion, as we'll need to restore
                    // it when this object is disposed.
                    success = SafeNativeMethods.DeleteObject(hClippingRegion);
                    Debug.Assert(success, "DeleteObject(hClippingRegion) failed.");

                    if (hOriginalClippingRegion.Handle != IntPtr.Zero)
                    {
                        success = SafeNativeMethods.DeleteObject(hOriginalClippingRegion);
                        Debug.Assert(success, "DeleteObject(hOriginalClippingRegion) failed.");
                    }
                }
            }
Esempio n. 10
0
            // bFillBitmapInfo
            //
            // Fills in the fields of a BITMAPINFO so that we can create a bitmap
            // that matches the format of the display.
            //
            // This is done by creating a compatible bitmap and calling GetDIBits
            // to return the color masks.  This is done with two calls.  The first
            // call passes in biBitCount = 0 to GetDIBits which will fill in the
            // base BITMAPINFOHEADER data.  The second call to GetDIBits (passing
            // in the BITMAPINFO filled in by the first call) will return the color
            // table or bitmasks, as appropriate.
            //
            // Returns:
            //   TRUE if successful, FALSE otherwise.
            //
            // History:
            //  07-Jun-1995 -by- Gilman Wong [gilmanw]
            // Wrote it.
            //
            //  15-Nov-2000 -by- Chris Anderson [chrisan]
            // Ported it to C#
            //
            private bool bFillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BITMAPINFO_FLAT pbmi)
            {
                IntPtr hbm  = IntPtr.Zero;
                bool   bRet = false;

                try {
                    //
                    // Create a dummy bitmap from which we can query color format info
                    // about the device surface.
                    //

                    hbm = SafeNativeMethods.CreateCompatibleBitmap(new HandleRef(null, hdc), 1, 1);

                    if (hbm == IntPtr.Zero)
                    {
                        throw new OutOfMemoryException(SR.GetString(SR.GraphicsBufferQueryFail));
                    }

                    pbmi.bmiHeader_biSize = Marshal.SizeOf(typeof(NativeMethods.BITMAPINFOHEADER));
                    pbmi.bmiColors        = new byte[NativeMethods.BITMAPINFO_MAX_COLORSIZE * 4];

                    //
                    // Call first time to fill in BITMAPINFO header.
                    //

                    IntPtr diRet = SafeNativeMethods.GetDIBits(new HandleRef(null, hdc),
                                                               new HandleRef(null, hbm),
                                                               0,
                                                               0,
                                                               IntPtr.Zero,
                                                               ref pbmi,
                                                               NativeMethods.DIB_RGB_COLORS);

                    if (pbmi.bmiHeader_biBitCount <= 8)
                    {
                        bRet = bFillColorTable(hdc, hpal, ref pbmi);
                    }
                    else
                    {
                        if (pbmi.bmiHeader_biCompression == NativeMethods.BI_BITFIELDS)
                        {
                            //
                            // Call a second time to get the color masks.
                            // It's a GetDIBits Win32 "feature".
                            //

                            SafeNativeMethods.GetDIBits(new HandleRef(null, hdc),
                                                        new HandleRef(null, hbm),
                                                        0,
                                                        pbmi.bmiHeader_biHeight,
                                                        IntPtr.Zero,
                                                        ref pbmi,
                                                        NativeMethods.DIB_RGB_COLORS);
                        }

                        bRet = true;
                    }
                }
                finally {
                    if (hbm != IntPtr.Zero)
                    {
                        SafeNativeMethods.DeleteObject(new HandleRef(null, hbm));
                        hbm = IntPtr.Zero;
                    }
                }

                return(bRet);
            }