Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Custom GDI+ class
Obtaining names and positions for shortcuts located on the Windows Desktop
GDI+: Implementing image scrolling with inertia
How to change display settings: screen resolution, screen refresh rate
Displaying icons in the system tray (VFP9)
Printing Image File, programmatically set print page orientation to landscape
How to load a user profile
Detecting changes in connections to removable drives (VFP9)
Displaying dimmed window behind VFP top-level form
Browsing Windows Known Folders (Special Folders)
How to put a horizontal text scrolling on the form (a news line, marquee)
Storing content of the Clipboard to a bitmap file
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Enumerating devices installed on the local machine
Moving shortcut to a specified position on the Windows Desktop
Winsock: sending email messages (SMTP, port 25)
How to control Adobe Reader 9.0 (SDI mode) from VFP application
Monitoring clipboard content changes (VFP9)
Pocket PC: custom RAPI class for operating with the System Registry
How to print FoxPro form
How to ping a remote site using IP Helper API calls
How to enable the SE_SHUTDOWN_NAME privilege for the application
Compressing and decompressing files with Windows API Runtime Library routines
Using Month Calendar Control (VFP9, Comctl32.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
  • #include "stdafx.h"
    #include < windows.h >
    #include "conio.h"
    
    void ListDisplayDevices()
    {
        int index=0;
        DISPLAY_DEVICE dd;
        dd.cb = sizeof(DISPLAY_DEVICE);
    
        while (EnumDisplayDevices(NULL, index++, &dd, 0))
        {
            if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) printf("* ");
            printf("%s, %s\n", dd.DeviceName, dd.DeviceString);
        }
    }
    
    DISPLAY_DEVICE GetPrimaryDevice()
    {
        int index=0;
        DISPLAY_DEVICE dd;
        dd.cb = sizeof(DISPLAY_DEVICE);
    
        while (EnumDisplayDevices(NULL, index++, &dd, 0))
        {
            if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) return dd;
        }
        return dd;
    }
    
    void ListDisplaySettings(int index)
    {
        DISPLAY_DEVICE dd;
        dd.cb = sizeof(DISPLAY_DEVICE);
        if (!EnumDisplayDevices(NULL, index, &dd, 0))
        {
            printf("EnumDisplayDevices failed:%d\n", GetLastError());
            return;
        }
    
        DISPLAY_DEVICE monitor;
        monitor.cb = sizeof(DISPLAY_DEVICE);
        if (!EnumDisplayDevices(dd.DeviceName, index, &monitor, 0))
        {
            printf("EnumDisplayDevices failed:%d\n", GetLastError());
            return;
        }
    
        DEVMODE dm;
        dm.dmSize = sizeof(DEVMODE);
    
        if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
        {
            printf("EnumDisplaySettings failed:%d\n", GetLastError());
            return;
        }
    
        printf("Device name: %s\n", dd.DeviceName);
        printf("Monitor name: %s\n", monitor.DeviceID);
        printf("Refresh rate, in hertz: %d\n", dm.dmDisplayFrequency);
        printf("Color depth: %d\n", dm.dmBitsPerPel);
        printf("Screen resolution, in pixels: %d x %d\n", 
            dm.dmPelsWidth, dm.dmPelsHeight);
    }
    
    void SetDisplayDefaults()
    {
        //changes the settings of the default display device
        //to the default mode
        ChangeDisplaySettings(NULL, 0);
    }
    
    BOOL SetDisplayResolution(long PelsWidth, long PelsHeight)
    {
        DISPLAY_DEVICE dd = GetPrimaryDevice();
        DEVMODE dm;
        dm.dmSize = sizeof(DEVMODE);
        if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
        {
            printf("EnumDisplaySettings failed:%d\n", GetLastError());
            return FALSE;
        }
    
        dm.dmPelsWidth = PelsWidth;
        dm.dmPelsHeight = PelsHeight;
        dm.dmFields = (DM_PELSWIDTH | DM_PELSHEIGHT);
        if (ChangeDisplaySettings(&dm, CDS_TEST) !=DISP_CHANGE_SUCCESSFUL)
        {
            printf("\nIllegal graphics mode: %d\n", GetLastError());
            return FALSE;
        }
    
        return (ChangeDisplaySettings(&dm, 0)==DISP_CHANGE_SUCCESSFUL);
    }
    
    void _tmain()
    {
        ListDisplayDevices();
        ListDisplaySettings(0);
    
        if (SetDisplayResolution(800, 600))
        {
            printf("\nPress Enter to return to default mode.\n");
            getch();
            SetDisplayDefaults();
        }
        else
        {
            printf("\nPress Enter to close this window.\n");
            getch();
        }
        return;
    }
    
    
    

    User rating: 10/10 (2 votes)
    Rate this code sample:
    • ~
    2648 bytes  
    Created: 2003-01-21 10:56:03  
    Modified: 2016-02-25 09:23:01  
    Visits in 7 days: 189  
    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-2017 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.6.37), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (54.225.54.120)
    17 sec.Function: 'GetMenuItemCount'
    32 sec.Function: 'WSAStartup'
    45 sec.Example: 'Using an Event Object. Part B: running an application responding to events'
    1 min.Function: 'mmioAscend'
    Function group: 'Windows Multimedia'
    1.27 min.Function: 'CeFindAllFiles'
    Function group: 'Remote Application Programming (RAPI)'
    1.52 min.
    Function group: 'GDI+ StringFormat'
    1.78 min.
    Function group: 'Dialog Box'
    2.05 min.Function: 'InternetCloseHandle'
    Function group: 'Internet Functions (WinInet)'
    2.32 min.Function: 'send'
    Function group: 'Windows Sockets 2 (Winsock)'
    2.53 min.Example: 'Peer-to-peer LAN messenger built with Mailslot API functions'
    Google
    Advertise here!