Click here to Skip to main content
12,507,343 members (73,998 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.8K views
3 bookmarked
Posted

How to get or set screen resolution in GDI

, 7 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
GDI screen resolution manipulation

Introduction

I have always wanted to get or set my system screen resolution with GDI. Now I have found how to do that, and want to share my finding with others

Let's go!...

Step1

First you should find your active display device and its settings:

EnumDisplaySettingsW(NULL,ENUM_CURRENT_SETTINGS,&devMode);
m_PrevMode = devMode;
disDev.cb = sizeof(DISPLAY_DEVICE);
disDev.StateFlags = DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;

Step2

Then you can save found information somewhere to print:

strTemp.Format(_T("Active Display Device settings : \n\nDevice Name : %s\nDevice Resolution : %d × %d\nBits per Pixel: %d bits\nFrequency : %d Hz")
        ,disDev.DeviceString
        ,devMode.dmPelsWidth
        ,devMode.dmPelsHeight
         ,devMode.dmBitsPerPel
         ,devMode.dmDisplayFrequency);
    GetDlgItem(IDC_RESMSG)->SetWindowTextW(strTemp);//print them

Step3

After that you may want to find all available Display mode like this:

for (unsigned i = 0;EnumDisplaySettingsW(NULL, i, &devMode);i++)
  {
      CStringW strTemp;
      m_Array.Add(devMode);//m_Array is a place to save found Modes
      strTemp.Format(_T("%d × %d, %d bits, %d Hz")          /*save them */
          ,devMode.dmPelsWidth
          ,devMode.dmPelsHeight
          ,devMode.dmBitsPerPel
          ,devMode.dmDisplayFrequency
   );
   ((CListBox*)GetDlgItem(IDC_RESLIST))->AddString(strTemp);//print them

Step4

Finally you can set any found mode you want :

m_Array.GetAt(iCurSel).dmSize = sizeof(DEVMODE);
       m_Array.GetAt(iCurSel).dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFREQUENCY;
       result = ChangeDisplaySettingsW(&m_Array.GetAt(iCurSel),CDS_UPDATEREGISTRY);
       if(DISP_CHANGE_SUCCESSFUL == result)
       {
           if(IDYES == AfxMessageBox(_T("Do you want to keep this setting ?"),MB_YESNO))
               m_PrevMode = m_Array.GetAt(iCurSel);
           else
           {
               if(DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsW(&m_PrevMode,CDS_UPDATEREGISTRY))
                   AfxMessageBox(_T("Couldn't restore previous Display Mode"),MB_ICONERROR);
           }
       }
       else
       {
           switch(result)
           {
               case DISP_CHANGE_BADDUALVIEW:
                   strMessage.Format(_T("The settings change was unsuccessful because system is DualView capable."));
                   break;
               case DISP_CHANGE_FAILED:
                   strMessage.Format(_T("The display driver failed the specified graphics mode."));
                   break;
               case DISP_CHANGE_RESTART:
                   strMessage.Format(_T("The computer must be restarted in order for the graphics mode to work."));
                   break;
               case DISP_CHANGE_NOTUPDATED:
                   strMessage.Format(_T("Unable to write settings to the registry."));
                   break;
           }
           AfxMessageBox(strMessage,MB_ICONERROR);
       }
   }
   else
       AfxMessageBox(_T("No Display Setting was selected to Apply"),MB_ICONINFORMATION);

Note that if you find this article having source code instead of explanation,its due to the very busy time that I have this days. Please accept my sorriness!

Points of Interest

Note that this app has a undo feature!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PE32_64
President
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 7 Oct 2010
Article Copyright 2010 by PE32_64
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid