Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to get or set screen resolution in GDI

0.00/5 (No votes)
7 Oct 2010 5  
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:


C++
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:


C++
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:


C++
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 :


C++
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here