Exemple #1
0
 internal static NativeMethods.IconHandle CreateIconCursor(byte[] colorArray, int width, int height, int xHotspot, int yHotspot, bool isIcon)
 {
     NativeMethods.BitmapHandle bitmapHandle  = null;
     NativeMethods.BitmapHandle bitmapHandle2 = null;
     NativeMethods.IconHandle   result;
     try
     {
         NativeMethods.BITMAPINFO bitmapinfo = new NativeMethods.BITMAPINFO(width, -height, 32);
         bitmapinfo.bmiHeader_biCompression = 0;
         IntPtr zero = IntPtr.Zero;
         bitmapHandle = UnsafeNativeMethods.CreateDIBSection(new HandleRef(null, IntPtr.Zero), ref bitmapinfo, 0, ref zero, null, 0);
         if (bitmapHandle.IsInvalid || zero == IntPtr.Zero)
         {
             result = NativeMethods.IconHandle.GetInvalidIcon();
         }
         else
         {
             Marshal.Copy(colorArray, 0, zero, colorArray.Length);
             byte[] array = IconHelper.GenerateMaskArray(width, height, colorArray);
             Invariant.Assert(array != null);
             bitmapHandle2 = UnsafeNativeMethods.CreateBitmap(width, height, 1, 1, array);
             if (bitmapHandle2.IsInvalid)
             {
                 result = NativeMethods.IconHandle.GetInvalidIcon();
             }
             else
             {
                 result = UnsafeNativeMethods.CreateIconIndirect(new NativeMethods.ICONINFO
                 {
                     fIcon    = isIcon,
                     xHotspot = xHotspot,
                     yHotspot = yHotspot,
                     hbmMask  = bitmapHandle2,
                     hbmColor = bitmapHandle
                 });
             }
         }
     }
     finally
     {
         if (bitmapHandle != null)
         {
             bitmapHandle.Dispose();
             bitmapHandle = null;
         }
         if (bitmapHandle2 != null)
         {
             bitmapHandle2.Dispose();
             bitmapHandle2 = null;
         }
     }
     return(result);
 }
 private void Win32CreateCaret()
 {
     if (!this._isSelectionActive)
     {
         return;
     }
     if (!this._win32Caret || this._win32Height != this._height)
     {
         IntPtr             intPtr             = IntPtr.Zero;
         PresentationSource presentationSource = PresentationSource.CriticalFromVisual(this);
         if (presentationSource != null)
         {
             new UIPermission(UIPermissionWindow.AllWindows).Assert();
             try
             {
                 intPtr = (presentationSource as IWin32Window).Handle;
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
         }
         if (intPtr != IntPtr.Zero)
         {
             double y = presentationSource.CompositionTarget.TransformToDevice.Transform(new Point(0.0, this._height)).Y;
             NativeMethods.BitmapHandle hbitmap = UnsafeNativeMethods.CreateBitmap(1, this.ConvertToInt32(y), 1, 1, null);
             bool flag           = UnsafeNativeMethods.CreateCaret(new HandleRef(null, intPtr), hbitmap, 0, 0);
             int  lastWin32Error = Marshal.GetLastWin32Error();
             if (flag)
             {
                 this._win32Caret  = true;
                 this._win32Height = this._height;
                 return;
             }
             this._win32Caret = false;
             throw new Win32Exception(lastWin32Error);
         }
     }
 }
Exemple #3
0
 private void DestroyResources()
 {
     if (_dt != null)
     {
         _dt.Stop();
         _dt = null;
     }
     if (_hwnd != IntPtr.Zero)
     {
         HandleRef hwnd = new HandleRef(null, _hwnd);
         if (UnsafeNativeMethods.IsWindow(hwnd))
         {
             UnsafeNativeMethods.IntDestroyWindow(hwnd);
         }
         _hwnd = IntPtr.Zero;
     }
     if (_hBitmap != null && !_hBitmap.IsClosed)
     {
         UnsafeNativeMethods.DeleteObject(_hBitmap.MakeHandleRef(null).Handle);
         _hBitmap.Close();
         _hBitmap = null;
     }
     if (_wndClass != 0)
     {
         // Attempt to unregister the window class.  If the application has a second
         // splash screen which is still open this call will fail.  That's OK.
         if (UnsafeNativeMethods.IntUnregisterClass(new IntPtr(_wndClass), _hInstance) != 0)
         {
             _defWndProc = null; // Can safely release the wndproc delegate when there are no more splash screen instances
         }
         _wndClass = 0;
     }
     if (_resourceManager != null)
     {
         _resourceManager.ReleaseAllResources();
     }
 }
Exemple #4
0
        private bool CreateLayeredWindowFromImgBuffer(IntPtr pImgBuffer, long cImgBufferLen, bool topMost)
        {
            bool   bSuccess        = false;
            IntPtr pImagingFactory = IntPtr.Zero;
            IntPtr pDecoder        = IntPtr.Zero;
            IntPtr pIStream        = IntPtr.Zero;
            IntPtr pDecodedFrame   = IntPtr.Zero;
            IntPtr pBitmapSourceFormatConverter = IntPtr.Zero;
            IntPtr pBitmapFlipRotator           = IntPtr.Zero;

            try
            {
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CreateImagingFactory(UnsafeNativeMethods.WIC.WINCODEC_SDK_VERSION, out pImagingFactory));

                // Use the WIC stream class to wrap the unmanaged pointer
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CreateStream(pImagingFactory, out pIStream));

                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.InitializeStreamFromMemory(pIStream, pImgBuffer, (uint)cImgBufferLen));

                // Create an object that will decode the encoded image
                Guid vendor = Guid.Empty;
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CreateDecoderFromStream(pImagingFactory, pIStream,
                                                                    ref vendor, 0, out pDecoder));

                // Get the frame from the decoder. Most image formats have only a single frame, in the case
                // of animated gifs we are ok with only displaying the first frame of the animation.
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.GetFrame(pDecoder, 0, out pDecodedFrame));

                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CreateFormatConverter(pImagingFactory, out pBitmapSourceFormatConverter));

                // Convert the image from whatever format it is in to 32bpp premultiplied alpha BGRA
                Guid pixelFormat = UnsafeNativeMethods.WIC.WICPixelFormat32bppPBGRA;
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.InitializeFormatConverter(pBitmapSourceFormatConverter, pDecodedFrame,
                                                                      ref pixelFormat, 0 /*DitherTypeNone*/, IntPtr.Zero,
                                                                      0, UnsafeNativeMethods.WIC.WICPaletteType.WICPaletteTypeCustom));
                // Reorient the image
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CreateBitmapFlipRotator(pImagingFactory, out pBitmapFlipRotator));

                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.InitializeBitmapFlipRotator(pBitmapFlipRotator, pBitmapSourceFormatConverter,
                                                                        UnsafeNativeMethods.WIC.WICBitmapTransformOptions.WICBitmapTransformFlipVertical));
                Int32 width, height;
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.GetBitmapSize(pBitmapFlipRotator, out width, out height));

                Int32 stride = width * 4;

                // initialize the bitmap header
                MS.Win32.NativeMethods.BITMAPINFO bmInfo = new MS.Win32.NativeMethods.BITMAPINFO(width, height, 32 /*bpp*/);
                bmInfo.bmiHeader_biCompression = MS.Win32.NativeMethods.BI_RGB;
                bmInfo.bmiHeader_biSizeImage   = (int)(stride * height);

                // Create a 32bpp DIB.  This DIB must have an alpha channel for UpdateLayeredWindow to succeed.
                IntPtr pBitmapBits = IntPtr.Zero;
                _hBitmap = UnsafeNativeMethods.CreateDIBSection(new HandleRef(), ref bmInfo, 0 /* DIB_RGB_COLORS*/, ref pBitmapBits, null, 0);

                // Copy the decoded image to the new buffer which backs the HBITMAP
                Int32Rect rect = new Int32Rect(0, 0, width, height);
                UnsafeNativeMethods.HRESULT.Check(
                    UnsafeNativeMethods.WIC.CopyPixels(pBitmapFlipRotator, ref rect, stride, stride * height, pBitmapBits));

                _hwnd = CreateWindow(_hBitmap, width, height, topMost);

                bSuccess = true;
            }
            finally
            {
                if (pImagingFactory != IntPtr.Zero)
                {
                    Marshal.Release(pImagingFactory);
                }
                if (pDecoder != IntPtr.Zero)
                {
                    Marshal.Release(pDecoder);
                }
                if (pIStream != IntPtr.Zero)
                {
                    Marshal.Release(pIStream);
                }
                if (pDecodedFrame != IntPtr.Zero)
                {
                    Marshal.Release(pDecodedFrame);
                }
                if (pBitmapSourceFormatConverter != IntPtr.Zero)
                {
                    Marshal.Release(pBitmapSourceFormatConverter);
                }
                if (pBitmapFlipRotator != IntPtr.Zero)
                {
                    Marshal.Release(pBitmapFlipRotator);
                }

                if (bSuccess == false)
                {
                    DestroyResources(); // cleans up _hwnd and _hBitmap
                }
            }

            return(bSuccess);
        }
Exemple #5
0
        private IntPtr CreateWindow(NativeMethods.BitmapHandle hBitmap, int width, int height, bool topMost)
        {
            if (_defWndProc == null)
            {
                _defWndProc = new MS.Win32.NativeMethods.WndProc(UnsafeNativeMethods.DefWindowProc);
            }

            MS.Win32.NativeMethods.WNDCLASSEX_D wndClass = new MS.Win32.NativeMethods.WNDCLASSEX_D();
            wndClass.cbSize        = Marshal.SizeOf(typeof(MS.Win32.NativeMethods.WNDCLASSEX_D));
            wndClass.style         = 3; /* CS_HREDRAW | CS_VREDRAW */
            wndClass.lpfnWndProc   = null;
            wndClass.hInstance     = _hInstance;
            wndClass.hCursor       = IntPtr.Zero;
            wndClass.lpszClassName = CLASSNAME;
            wndClass.lpszMenuName  = string.Empty;
            wndClass.lpfnWndProc   = _defWndProc;

            // We chose to ignore re-registration errors in RegisterClassEx on the off chance that the user
            // wants to open multiple splash screens.
            _wndClass = MS.Win32.UnsafeNativeMethods.IntRegisterClassEx(wndClass);
            if (_wndClass == 0)
            {
                if (Marshal.GetLastWin32Error() != 0x582) /* class already registered */
                {
                    throw new Win32Exception();
                }
            }

            int screenWidth  = MS.Win32.UnsafeNativeMethods.GetSystemMetrics(SM.CXSCREEN);
            int screenHeight = MS.Win32.UnsafeNativeMethods.GetSystemMetrics(SM.CYSCREEN);
            int x            = (screenWidth - width) / 2;
            int y            = (screenHeight - height) / 2;

            HandleRef nullHandle        = new HandleRef(null, IntPtr.Zero);
            int       windowCreateFlags =
                (int)NativeMethods.WS_EX_WINDOWEDGE |
                NativeMethods.WS_EX_TOOLWINDOW |
                NativeMethods.WS_EX_LAYERED |
                (topMost ? NativeMethods.WS_EX_TOPMOST : 0);

            // CreateWindowEx will either succeed or throw
            IntPtr hWnd = MS.Win32.UnsafeNativeMethods.CreateWindowEx(
                windowCreateFlags,
                CLASSNAME, SR.Get(SRID.SplashScreenIsLoading),
                MS.Win32.NativeMethods.WS_POPUP | MS.Win32.NativeMethods.WS_VISIBLE,
                x, y, width, height,
                nullHandle, nullHandle, new HandleRef(null, _hInstance), IntPtr.Zero);

            // Display the image on the window
            IntPtr hScreenDC  = UnsafeNativeMethods.GetDC(new HandleRef());
            IntPtr memDC      = UnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, hScreenDC));
            IntPtr hOldBitmap = UnsafeNativeMethods.SelectObject(new HandleRef(null, memDC), hBitmap.MakeHandleRef(null).Handle);

            NativeMethods.POINT newSize        = new NativeMethods.POINT(width, height);
            NativeMethods.POINT newLocation    = new NativeMethods.POINT(x, y);
            NativeMethods.POINT sourceLocation = new NativeMethods.POINT(0, 0);
            _blendFunc                     = new NativeMethods.BLENDFUNCTION();
            _blendFunc.BlendOp             = NativeMethods.AC_SRC_OVER;
            _blendFunc.BlendFlags          = 0;
            _blendFunc.SourceConstantAlpha = 255;
            _blendFunc.AlphaFormat         = 1; /*AC_SRC_ALPHA*/

            bool result = UnsafeNativeMethods.UpdateLayeredWindow(hWnd, hScreenDC, newLocation, newSize,
                                                                  memDC, sourceLocation, 0, ref _blendFunc, NativeMethods.ULW_ALPHA);

            UnsafeNativeMethods.SelectObject(new HandleRef(null, memDC), hOldBitmap);
            UnsafeNativeMethods.ReleaseDC(new HandleRef(), new HandleRef(null, memDC));
            UnsafeNativeMethods.ReleaseDC(new HandleRef(), new HandleRef(null, hScreenDC));

            if (result == false)
            {
                UnsafeNativeMethods.HRESULT.Check(Marshal.GetHRForLastWin32Error());
            }

            return(hWnd);
        }
Exemple #6
0
        // Also used by PenCursorManager
        // Creates a 32 bit per pixel Icon or cursor.  This code is moved from framework\ms\internal\ink\pencursormanager.cs
        internal static NativeMethods.IconHandle CreateIconCursor(
            byte[] colorArray,
            int width,
            int height,
            int xHotspot,
            int yHotspot,
            bool isIcon)
        {
            //   1. We are going to generate a WIN32 color bitmap which represents the color cursor.
            //   2. Then we need to create a monochrome bitmap which is used as the cursor mask.
            //   3. At last we create a WIN32 HICON from the above two bitmaps
            NativeMethods.BitmapHandle colorBitmap = null;
            NativeMethods.BitmapHandle maskBitmap  = null;

            try
            {
                // 1) Create the color bitmap using colorArray
                // Fill in the header information
                NativeMethods.BITMAPINFO bi = new NativeMethods.BITMAPINFO(
                    width,                                      // width
                    -height,                                    // A negative value indicates the bitmap is top-down DIB
                    32                                          // biBitCount
                    );
                bi.bmiHeader_biCompression = NativeMethods.BI_RGB;

                IntPtr bits = IntPtr.Zero;
                colorBitmap = MS.Win32.UnsafeNativeMethods.CreateDIBSection(
                    new HandleRef(null, IntPtr.Zero),                // A device context. Pass null in if no DIB_PAL_COLORS is used.
                    ref bi,                                          // A BITMAPINFO structure which specifies the dimensions and colors.
                    NativeMethods.DIB_RGB_COLORS,                    // Specifies the type of data contained in the bmiColors array member of the BITMAPINFO structure
                    ref bits,                                        // An out Pointer to a variable that receives a pointer to the location of the DIB bit values
                    null,                                            // Handle to a file-mapping object that the function will use to create the DIB. This parameter can be null.
                    0                                                // dwOffset. This value is ignored if hSection is NULL
                    );

                if (colorBitmap.IsInvalid || bits == IntPtr.Zero)
                {
                    // Note we will release the GDI resources in the finally block.
                    return(NativeMethods.IconHandle.GetInvalidIcon());
                }

                // Copy the color bits to the win32 bitmap
                Marshal.Copy(colorArray, 0, bits, colorArray.Length);


                // 2) Now create the mask bitmap which is monochrome
                byte[] maskArray = GenerateMaskArray(width, height, colorArray);
                Invariant.Assert(maskArray != null);

                maskBitmap = UnsafeNativeMethods.CreateBitmap(width, height, 1, 1, maskArray);
                if (maskBitmap.IsInvalid)
                {
                    // Note we will release the GDI resources in the finally block.
                    return(NativeMethods.IconHandle.GetInvalidIcon());
                }

                // Now create HICON from two bitmaps.
                NativeMethods.ICONINFO iconInfo = new NativeMethods.ICONINFO();
                iconInfo.fIcon    = isIcon;         // fIcon == ture means creating an Icon, otherwise Cursor
                iconInfo.xHotspot = xHotspot;
                iconInfo.yHotspot = yHotspot;
                iconInfo.hbmMask  = maskBitmap;
                iconInfo.hbmColor = colorBitmap;

                return(UnsafeNativeMethods.CreateIconIndirect(iconInfo));
            }
            finally
            {
                if (colorBitmap != null)
                {
                    colorBitmap.Dispose();
                    colorBitmap = null;
                }

                if (maskBitmap != null)
                {
                    maskBitmap.Dispose();
                    maskBitmap = null;
                }
            }
        }