コード例 #1
0
        public static void ChangeDisplayMode(int width, int heigth)
        {
            DisplayMode?target = null;

            foreach (DisplayMode mode in ScreenHelper.GetSupportedDisplayModes())
            {
                if (mode.dmPelsWidth == width && mode.dmPelsHeight == heigth)
                {
                    if (target == null)
                    {
                        target = mode;
                    }
                    else
                    {
                        if (mode.dmDisplayFrequency > target.Value.dmDisplayFrequency || mode.dmBitsPerPel > target.Value.dmBitsPerPel)
                        {
                            target = mode;
                        }
                    }
                }
            }

            if (target == null)
            {
                throw new Exception("ScreenResolution settings Not Supported");
            }

            ScreenHelper.ChangeDisplayMode(target.Value);
        }
コード例 #2
0
        /// <summary>
        /// ChangeDisplayMode 更改系统的显示设置。
        /// </summary>
        public static void ChangeDisplayMode(ScreenResolution screenResolution)
        {
            if (screenResolution.BitsPerPixels == 0 || screenResolution.DisplayFrequency == 0)
            {
                ScreenHelper.ChangeDisplayMode(screenResolution.Width, screenResolution.Height);
                return;
            }

            bool found = false;

            foreach (DisplayMode mode in ScreenHelper.GetSupportedDisplayModes())
            {
                if (mode.dmPelsWidth == screenResolution.Width && mode.dmPelsHeight == screenResolution.Height &&
                    mode.dmDisplayFrequency == screenResolution.DisplayFrequency && mode.dmBitsPerPel == screenResolution.BitsPerPixels)
                {
                    found = true;
                    ScreenHelper.ChangeDisplayMode(mode);
                    break;
                }
            }

            if (!found)
            {
                throw new Exception("ScreenResolution settings Not Supported");
            }
        }
コード例 #3
0
        /// <summary>
        /// GetSupportedDisplayModes 获取系统支持的屏幕分辨率。
        /// </summary>
        public static List <ScreenResolution> GetSupportedScreenResolutions()
        {
            Dictionary <string, DisplayMode> dic = new  Dictionary <string, DisplayMode>();

            foreach (DisplayMode mode in ScreenHelper.GetSupportedDisplayModes())
            {
                if (mode.dmPelsWidth >= mode.dmPelsHeight)
                {
                    string token = string.Format("{0}*{1}", mode.dmPelsWidth, mode.dmPelsHeight);
                    if (!dic.ContainsKey(token))
                    {
                        dic.Add(token, mode);
                    }
                    else
                    {
                        if (mode.dmBitsPerPel > dic[token].dmBitsPerPel)
                        {
                            dic[token] = mode;
                        }
                        else if (mode.dmBitsPerPel == dic[token].dmBitsPerPel)
                        {
                            if (mode.dmDisplayFrequency > dic[token].dmDisplayFrequency)
                            {
                                dic[token] = mode;
                            }
                        }
                    }
                }
            }

            List <ScreenResolution> list = new List <ScreenResolution>();

            foreach (DisplayMode mode in dic.Values)
            {
                list.Add(mode.GetScreenResolution());
            }
            return(list);
        }