Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to change display settings: screen resolution, screen refresh rate
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Converting Unicode data from the Clipboard to a character string using a given code page
Copying picture of the active form to the Clipboard using Enhanced Metafile API functions
Enumerating data formats currently available on the clipboard
Enumerating raw input devices attached to the system (keyboard, mouse, human interface device)
GDI+: copying to the Clipboard (a) image of active FoxPro window/form, (b) image file
Disabling mouse and keyboard input for the main VFP window (with the app still running)
Storing content of the Clipboard to a bitmap file
Current keyboard type
Displaying icons in the system tray (VFP9)
Custom HttpRequest class (WinINet)
How to activate Windows Calculator
Attaching menu to a top-level form
Storing screen shot of a form to bitmap file
Using Beep and Sleep functions to make the old tin buzz sing (WinNT only?)
Minimizing all running applications
Obtaining current Internet Explorer browser version and UserAgent
Using InternetSetFilePointer when resuming interrupted download from the Internet
How to print a bitmap file
Sending email messages with Simple MAPI
Using EnumPrinters function to enumerate locally installed printers
Drawing Windows predefined bitmaps using the LoadBitmap functions
Using Path functions from Shell Lightweight Utility APIs (shlapi.dll)
How to change display settings: screen resolution, screen refresh rate

User rating: 10/10 (2 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
This code sample shows how to switch the monitor resolution to 800x600 pixels and back, keeping the existing refresh rate.

Before changing the resolution the code verifies if the hardware supports the requested graphics mode.



See also:
  • How to adjust monitor brightness (Vista, monitor with DDC support)
  • How to detect if additional monitor is connected and active
  • using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace DisplaySettings
    {
        public partial class Form1 : Form
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct DISPLAY_DEVICE
            {
                public int cb;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
                public string DeviceName;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
                public string DeviceString;
                public int StateFlags;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
                public string DeviceID;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
                public string DeviceKey;
    
                public DISPLAY_DEVICE(int flags)
                {
                    cb = 0;
                    StateFlags = flags;
                    DeviceName = new string((char)32, 32);
                    DeviceString = new string((char)32, 128);
                    DeviceID = new string((char)32, 128);
                    DeviceKey = new string((char)32, 128);
                    cb = Marshal.SizeOf(this);
                }
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct DEVMODE
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
                public string dmDeviceName;
                public short dmSpecVersion;
                public short dmDriverVersion;
                public short dmSize;
                public short dmDriverExtra;
                public int dmFields;
                public short dmOrientation;
                public short dmPaperSize;
                public short dmPaperLength;
                public short dmPaperWidth;
                public short dmScale;
                public short dmCopies;
                public short dmDefaultSource;
                public short dmPrintQuality;
                public short dmColor;
                public short dmDuplex;
                public short dmYResolution;
                public short dmTTOption;
                public short dmCollate;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
                public string dmFormName;
                public short dmUnusedPadding;
                public short dmBitsPerPel;
                public int dmPelsWidth;
                public int dmPelsHeight;
                public int dmDisplayFlags;
                public int dmDisplayFrequency;
            }
    
            public Form1()
            {
                InitializeComponent();
                EnumDevices();
            }
    
            private void listDevices_SelectedIndexChanged(
                object sender, EventArgs e)
            {
                int devNum = listDevices.SelectedIndex;
                bool isMain = MainDevice(devNum);
                btnSet.Enabled = isMain; // enable only for the main device
                EnumModes(devNum);
            }
    
            private void btnSet_Click(object sender, EventArgs e)
            { //set selected display mode
                int devNum = listDevices.SelectedIndex;
                int modeNum = listSettings.SelectedIndex;
                DEVMODE d = GetDevmode(devNum, modeNum);
                if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0 
                    & d.dmPelsHeight != 0)
                {
                    ChangeDisplaySettings(ref d, 0);
                }
            }
    
            private void EnumModes(int devNum)
            {
                listSettings.Items.Clear();
    
                string devName = GetDeviceName(devNum);
                DEVMODE devMode = new DEVMODE();
                int modeNum = 0;
                bool result = true;
                do
                {
                    result = EnumDisplaySettings(devName, 
                        modeNum, ref devMode);
    
                    if (result)
                    {
                        string item = DevmodeToString(devMode);
                        listSettings.Items.Add(item);
                    }
                    modeNum++;
                } while (result);
    
                if (listSettings.Items.Count > 0)
                {
                    DEVMODE current = GetDevmode(devNum, -1);
                    int selected = listSettings.FindString(
                        DevmodeToString(current));
                    if (selected >= 0)
                    listSettings.SetSelected(selected, true);
                }
            }
    
            private DEVMODE GetDevmode(int devNum, int modeNum)
            { //populates DEVMODE for the specified device and mode
                DEVMODE devMode = new DEVMODE();
                string devName = GetDeviceName(devNum);
                EnumDisplaySettings(devName, modeNum, ref devMode);
                return devMode;
            }
    
            private string DevmodeToString(DEVMODE devMode)
            {
                return devMode.dmPelsWidth.ToString() +
                    " x " + devMode.dmPelsHeight.ToString() +
                    ", " + devMode.dmBitsPerPel.ToString() + 
                    " bits, " + 
                    devMode.dmDisplayFrequency.ToString() + " Hz";
            }
    
            private void EnumDevices()
            { //populates Display Devices list
                this.listDevices.Items.Clear();
                DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
    
                int devNum = 0;
                bool result;
                do
                {
                    result = EnumDisplayDevices(IntPtr.Zero, 
                        devNum, ref d, 0);
    
                    if (result)
                    {
                        string item = devNum.ToString() + 
                            ". " + d.DeviceString.Trim();
                        if ((d.StateFlags & 4) != 0) item += " - main";
                        this.listDevices.Items.Add(item);
                    }
                    devNum++;
                } while (result);
            }
    
            private string GetDeviceName(int devNum)
            {
                DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
                bool result = EnumDisplayDevices(IntPtr.Zero, 
                    devNum, ref d, 0);
                return (result ? d.DeviceName.Trim() : "#error#");
            }
    
            private bool MainDevice(int devNum)
            { //whether the specified device is the main device
                DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
                if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
                {
                    return ((d.StateFlags & 4) != 0);
                } return false;
            }
    
            [DllImport("User32.dll")]
            private static extern bool EnumDisplayDevices(
                IntPtr lpDevice, int iDevNum, 
                ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
    
            [DllImport("User32.dll")]
            private static extern bool EnumDisplaySettings(
                string devName, int modeNum, ref DEVMODE devMode);
    
            [DllImport("user32.dll")]
            public static extern int ChangeDisplaySettings(
                ref DEVMODE devMode, int flags);
    
        }
    }
    

    User rating: 10/10 (2 votes)
    Rate this code sample:
    • ~
    6846 bytes  
    Created: 2003-01-21 10:56:03  
    Modified: 2013-09-20 13:29:51  
    Visits in 7 days: 613  
    Listed functions:
    ChangeDisplaySettings
    EnumDisplayDevices
    EnumDisplaySettings
    GetDesktopWindow
    GetMonitorInfo
    MonitorFromWindow
    Printer friendly API declarations
    My comment:
    A screenshot of C# version of this code sample.


    * * *
    Five ways of obtaining current monitor resolution:

  • The SYSMETRIC(1 | 2) is the simplest one. Very likely it is a wrapper around either GetSystemMetrics (SM_CXFULLSCREEN | SM_CYFULLSCREEN) call or SystemParametersInfo call. Each of three reports data for the primary monitor only.

  • Presented in the code sample above, the EnumDisplaySettings populates the DEVMODE structure. Members dmPelsWidth and dmPelsHeight of this structure contain the width and the height for enumerated monitor.

  • The GetMonitorInfo populates the MONITORINFO structure. The rcMonitor member in this structure is the RECT structure itself. It contains the display monitor rectangle expressed in virtual-screen coordinates.

  • The GetWindowPlacement populates a rectangle -- RECT rcNormalPosition. Call this function with the handle to the desktop window. For multiple monitor configuration, such desktop rectanlge is a combination of all active monitor rectangles.

  • Normally the mouse cursor is confined inside the desktop window. The GetClipCursor copies the desktop window dimensions into the RECT structure.
  • Word Index links for this example:
    Translate this page:
      Spanish    Portuguese    German    French    Italian  
    FreeTranslation.com offers instant, free translations of text or web pages.
    User Contributed Notes:
    Rosemar | 2006-12-08 11:49:22
    I have 2 computers.

    One of then, with Windows XP and another one, with Windows 98.

    In the machine with Windows XP this code works right, but, in another computer with Windows 98, when I run the code, the desktop area lost the original icons positions and the Windows Task Bar became HIDED outside the desktop area.

    This problem have solution ?

    Thank's.
    A.M. | 2006-12-11 20:09:50
    You may try to refresh the Desktop window by calling an API function like ShowWindow, MoveWindow, SetWindowPos, SetForegroundWindow, SwitchToThisWindow.

    Win98 -- this is an old system. Some API calls are not supported or behave in a different way.

    Copyright 2001-2014 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

    Privacy policy
    Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.68), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (54.205.205.47)
    2 sec.Function: 'Escape'
    4 sec.Example: 'Enumerating ODBC drivers available on the local computer'
    6 sec.Function: 'RtlCompressBuffer'
    8 sec.Function: 'mmioCreateChunk'
    Function group: 'Windows Multimedia'
    11 sec.Function: 'waveOutOpen'
    Function group: 'Windows Multimedia'
    Google
    Advertise here!