Method of extracting icons Office

demand

A few years ago, a good sound in a unique program to attract a lot of players in the form of a concept note and the audience, aimed at "only looking for quality sound"; when the sound may have their own characteristics, have to see the color value, of course, this is understandable. Although touch on a bit far, I want to express the balance between stability and development of software aesthetics, also, or functional requirements and user experience to grasp the relationship problems. Some people think that only the power to meet demand, the interface is secondary; and actual results, the function is not powerful, the interface operation is very complicated; I personally think the contrary, if our software does not achieve a powerful, stable, we have to strengthen the development of the interface, correctly guide the user, reducing the chance of error.

Here is the Excel 2019 interface, leading the entire industry interface style:

Now we need to extract its icon how to operate?

achieve

Some icons by extraction software can not be extracted, the only method you can use is to have the official transfer of GetImageMso method.

Development Environment: VS2019 + VSTO, with Excel, for example.

Add In First create a new project:

Second, create an Application object in ThisAddIn class for global access.

 public static Excel.Application App;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
       App = this.Application;
}

  

Then, add a Ribbon, and add buttons and related events.

Next, download the appropriate method GetImageMso name, size image.

IPictureDisp pictureDisp = ThisAddIn.App.CommandBars.GetImageMso(name, 32, 32);

  

Finally, the output IPictureDisp conversion transparent icon, which is the second key point:

public class ImageHelper
    {
        /// <summary>
        /// IPictureDisp转Bitmap
        /// </summary>
        /// <param name="ipd">IPictureDisp</param>
        /// <returns>Bitmap</returns>
        public static Bitmap ConvertPixelByPixel(IPictureDisp ipd)
        {
            // get the info about the HBITMAP inside the IPictureDisp
            var dibsection = new DIBSECTION();
            GetObjectDIBSection((IntPtr)ipd.Handle, Marshal.SizeOf(dibsection), ref dibsection);
            var width = dibsection.dsBm.bmWidth;
            var height = dibsection.dsBm.bmHeight;

            // create the destination Bitmap object
            var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            unsafe
            {
                // get a pointer to the raw bits
                var pBits = (RGBQUAD*)(void*)dibsection.dsBm.bmBits;

                // copy each pixel manually
                for (var x = 0; x < dibsection.dsBmih.biWidth; x++)
                    for (var y = 0; y < dibsection.dsBmih.biHeight; y++)
                    {
                        var offset = y * dibsection.dsBmih.biWidth + x;
                        if (pBits[offset].rgbReserved != 0)
                        {
                            bitmap.SetPixel(x, y, Color.FromArgb(pBits[offset].rgbReserved, pBits[offset].rgbRed, pBits[offset].rgbGreen, pBits[offset].rgbBlue));
                        }
                    }
            }
            return bitmap;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        private struct RGBQUAD
        {
            public byte rgbBlue;
            public byte rgbGreen;
            public byte rgbRed;
            public byte rgbReserved;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct BITMAP
        {
            public int bmType;
            public int bmWidth;
            public int bmHeight;
            public int bmWidthBytes;
            public short bmPlanes;
            public short bmBitsPixel;
            public IntPtr bmBits;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct BITMAPINFOHEADER
        {
            public int biSize;
            public int biWidth;
            public int biHeight;
            public short biPlanes;
            public short biBitCount;
            public int biCompression;
            public int biSizeImage;
            public int biXPelsPerMeter;
            public int biYPelsPerMeter;
            public int biClrUsed;
            public int bitClrImportant;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct DIBSECTION
        {
            public BITMAP dsBm;
            public BITMAPINFOHEADER dsBmih;
            public int dsBitField1;
            public int dsBitField2;
            public int dsBitField3;
            public IntPtr dshSection;
            public int dsOffset;
        }

        [DllImport("gdi32.dll", EntryPoint = "GetObject")]
        public static extern int GetObjectDIBSection(IntPtr hObject, int nCount, ref DIBSECTION lpObject);
}

  

Achievement

Word icon:

Excel icon:

PowerPoint icon:

Project icon:

Visio icon:

reference

https://blog.csdn.net/blackwoodcliff/article/details/89891405

https://github.com/OfficeDev/office-fluent-ui-command-identifiers

Guess you like

Origin www.cnblogs.com/liweis/p/11762679.html