Click here to Skip to main content
15,886,873 members
Articles / Multimedia / GDI
Tip/Trick

How to get or set screen resolution in GDI

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
7 Oct 2010CPOL 12.1K   3  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --