/// <summary> /// Create a Mode from a windows DEVMODE structure /// </summary> /// <param name="d">windows DEVMODE structure</param> /// <returns>new graphics mode in a graphicsMode structure</returns> public static graphicsMode fromDevMode(DEVMODE d) { graphicsMode g = new graphicsMode(); g.bitsPerPixel = d.dmBitsPerPel; g.dimensions = new Size(d.dmPelsWidth, d.dmPelsHeight); g.refreshRate = d.dmDisplayFrequency; return(g); }
/// <summary> /// Create a Mode from a set of paramaters /// </summary> ///<param name="dimensions">Size of the matrix of pixels.</param> /// <param name="bitsPerPixel">pixel depth</param> /// <param name="refreshRate">scan rate</param> /// <returns>new graphics mode in a graphicsMode structure</returns> public static graphicsMode fromSpecs(Size dimensions, int bitsPerPixel, int refreshRate) { graphicsMode g = new graphicsMode(); g.bitsPerPixel = bitsPerPixel; g.refreshRate = refreshRate; g.dimensions = dimensions; return(g); }
/// <summary> /// Changes the screen mode of the specified device /// </summary> /// <param name="g">The graphics mode</param> /// <param name="displayCompatableOnly">Include modes the video card can produce, but the monitor can't use.</param> /// <param name="changeType">Nature of change to perform</param> /// <returns>ChangeDisplaySettingsReturnValue as per winsdk</returns> public ChangeDisplaySettingsReturnValue changeResolution(graphicsMode g, bool displayCompatableOnly, displayChangeType changeType) { int index = getIndexOfMode(g, displayCompatableOnly); if (index != -1) { return(changeResolution(index, displayCompatableOnly, changeType)); } else { return(ChangeDisplaySettingsReturnValue.DISP_CHANGE_BADMODE); } }
/// <summary> /// Gets the index of the specified resolution if available /// </summary> /// <param name="g">The graphics mode</param> /// <param name="displayCompatableOnly">Include modes the video card can produce, but the monitor can't use.</param> /// <returns>Returns -1 if mode is not supported</returns> public int getIndexOfMode(graphicsMode g, bool displayCompatableOnly) { int i; graphicsMode[] modes = enumerateModes(displayCompatableOnly); for (i = 0; i < modes.Length; i++) { if (g.compare(modes[i]) == 0) { return(i); } } return(-1); }
/// <summary> /// Get all available graphics modes /// </summary> /// <param name="displayCompatableOnly">Use all modes that the adapter can produce (but the attached display might not support)</param> /// <returns>An array of available graphics modes</returns> protected graphicsMode[] enumerateModes(bool displayCompatableOnly) { int i = 0; DEVMODE devmode = initNewDEVMODE(); ArrayList modes = new ArrayList(); while (EnumDisplaySettingsEx(this.DeviceName, i, ref devmode, displayCompatableOnly ? 0 : EDS_RAWMODE)) { i++; modes.Add(graphicsMode.fromDevMode(devmode)); } graphicsMode[] finalModes = new graphicsMode[modes.Count]; for (i = 0; i < finalModes.Length; i++) { finalModes[i] = (graphicsMode)modes[i]; } return(finalModes); }
/// <summary> /// Compares to graphics modes /// </summary> /// <returns>-1 if other is less than, 1 if greater than, 0 if equal </returns> public int compare(graphicsMode other) { int c; c = compare(refreshRate, other.refreshRate); if (c != 0) { return(c); } c = compare(dimensions.Height, other.dimensions.Height); if (c != 0) { return(c); } c = compare(dimensions.Width, other.dimensions.Width); if (c != 0) { return(c); } c = compare(bitsPerPixel, other.bitsPerPixel); return(c); }
/// <summary> /// Create a Mode from a string /// </summary> /// <param name="s">String representing mode</param> /// <returns>new graphics mode in a graphicsMode structure</returns> public static graphicsMode fromString(string s) { graphicsMode g = new graphicsMode(); try { if (s.IndexOfAny(@"\/".ToCharArray()) != -1) { //tree based mode specifier string[] tokens = s.Split(@"\/".ToCharArray()); g.dimensionString = tokens[0]; g.bitsPerPixelString = tokens[1]; g.refreshRateString = tokens[2]; return(g); } throw new Exception("TODO: better error handelling"); } catch { //return null; throw new Exception("TODO: better error handelling"); } }
/// <summary> /// Nearest Mode of less than or equal specs /// </summary> /// <param name="width">width of display mode</param> /// <param name="height">height of display mode</param> /// <param name="useNearest">find nearest is exact nt possible</param> /// <param name="newSize">The new display size</param> /// <returns>True if sucsessful, otherwise false.</returns> public bool changeToMode(int width, int height, bool useNearest, out Size newSize) { graphicsMode[] modes = availableModes; newSize = this.Bounds.Size; if (modes.Length <= 0) { return(false); } //are we already in a similar mode if ((this.Bounds.Size.Width == width) && (this.Bounds.Size.Height == height)) { return(true); } //lets not exceed 75Hz graphicsMode g = graphicsMode.fromSpecs(width, height, 32, 75); //changeResolution int nearest = -1; int i; for (i = 0; i < modes.Length; i++) { //-1 if other is less than, 1 if greater than, 0 if equal if (g.compare(modes[i]) <= 0) { if (nearest == -1) { nearest = i; } else { if (modes[nearest].compare(modes[i]) > 0) { nearest = i; } } } } //end for if (nearest == -1) { return(false); } if (!useNearest) { if (modes[nearest].dimensions.Width != width) { return(false); } if (modes[nearest].dimensions.Height != height) { return(false); } } ChangeDisplaySettingsReturnValue r = changeResolution(nearest, true, displayChangeType.noStartMenuOrToolBars); if (r == ChangeDisplaySettingsReturnValue.DISP_CHANGE_SUCCESSFUL) { newSize = modes[nearest].dimensions; return(true); } else { return(false); } }