using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;

namespace BBWY.Client.Helpers
{
    public  class UnitConverHelper
    {


        int dpiX = 0;
         int dpiY = 0;


        public UnitConverHelper()
        {

            GetDpi();
        }


        private void GetDpi()
        {
            var hDc = GetDC(IntPtr.Zero);

            dpiX = GetDeviceCaps(hDc, LOGPIXELSX);
            dpiY = GetDeviceCaps(hDc, LOGPIXELSY);
            ReleaseDC(IntPtr.Zero, hDc);
        }


        



        /// <summary>
        /// 毫米转像素(获取打印的宽和高)
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public PrintPx MmToPx(double mWidth, double mHeight)
        {
            if(dpiX<=0||dpiY<=0) GetDpi();

            return new PrintPx
            {
                Width = mWidth * dpiX / 25.4,
                Height = mHeight * dpiY / 25.4,

            };
        }

        /// <summary>
        /// 像素转毫米
        /// </summary>
        /// <param name="pxWidth"></param>
        /// <param name="pxHeight"></param>
        /// <returns></returns>
        public PrintPx PxToMm(double pxWidth, double pxHeight)
        {
            if (dpiX <= 0 || dpiY <= 0) GetDpi();
            return new PrintPx
            {
                Width = pxWidth * 25.4 / dpiX,
                Height = pxHeight * 25.4 / dpiY,

            };
        }


        private const int LOGPIXELSX = 88;
        private const int LOGPIXELSY = 90;

        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int index);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);

    }






    public class PrintPx
    {

        public double Width { get; set; }

        public double Height { get; set; }
    }

}