View/Change Screen resolution from within my program

I am writing a program which is best viewed in a resolution os 1024 x 786. I need for the program to test the current screen res and change it to a higher res if too less than 1024 x 768, then change it back again to original setting after program closes.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First of all. What platform are you talkning about???

If your talking about Windows I'm not sure you realy programaticly can change the resolution. What you can do is to create a virtual screen that is another resolution. But that will take you away from normal windows. You normaly do this when you want to draw grafics on the full screen using DirectX or OpenGL.
GetSystemMetrics( SM_CXSCREEN) and GetSystemMetrics( SM_CYSCREEN) should give you the current screen resolution.
Then use ChangeDisplaySettings(LPDEVMODE lpDevMode, DWORD dwflags) to change the resolution to what you want.

When your program starts, save the current screen resolution before you change it.  Then when you program is closing, change the screen resolution back to what it was.
I would defently remove a program that do that at once. I choose the resolution. I know what works for me. Not the progam. :-) But it is nice knowing how to do it.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

This is not a C++ language question and it off topic here.  A much better place for this would be the WINDOWS PROGRAMMING topic area:

You should post a comment for the moderator in the CS topic area:

and ask them to move your question to the right topic area.  It helps things all around and benefits you by getting Windows Programming experts on your question vs. C++ experts here.
WillowBrownAuthor Commented:
Let me re-phrase the question because I am not trying to progran the windows API. So I know I am in the right forum. I am writing a time and labor report which has hundreds of edit boxes and takes up the entire screen of a screen set to 1024 x 768. Many users have their screen set to 600 x 600 or less and therefore, my program is to big to fit. I need the code in my program during initialization that gets the screen res. Saves it. Then changes the res to 1024 x 768. Then after the user closes the program, restores the res to original settings. I have looked at the DEVMODE and other structures and I can now retrieve the settings X and Y in integer format. That's great. But I am having trouble with the code that changes the res. I am using Borland C++ Builder Enterprise in Windows XP.
But you're working in the Windows enviroment. And you're wrinting a program for the Windows envorment. So you need to use the Windows API to do what you want. The DEVMODE structure is part of the Windows API. And the function you call to get it is part of the Windows API. So I think it you might get better helt in the Windows area. It's not a C++ question. Because it's not in C++ standard to choose screen resolution.

Even if you can change the resolution I would suggest writing it in the requirements that you need to run in 1024x768 resolution. It might be that if you change it you will crash the users machine. You may choose a resolution that the machine is not able to show. And after that the user need to know how to start in failsafe mode. And that they might not know how.

And also. At many companies the change resolution option is disabled by the support organisation so they don't need to handle all the problems that will create. So in my opinion. Write it in the documents that it is a requirement.

Another note. Writing software require a specific resolution might not be a good solution. Ofcurse there is always time when you have to.
>>I am not trying to progran the windows AP

Indeed you are.  C++ has ABSOLUTELY NO facility for changing screen resolution.  In fact, C++ lacks even the concept of a screen.

This is a Windows Programming question, pure and simple.
Here's a small command line executable I wrote for changing the screen resolution.

#include <windows.h>

int main(int argc, char* argv[])
      int original_width = GetSystemMetrics(SM_CXSCREEN);
      int original_height = GetSystemMetrics(SM_CYSCREEN);
      TCHAR *szMsg = NULL;

      if (argc < 4)
            szMsg = __TEXT("Missing required arguments\n")
                   __TEXT("Example usage:\n")
                   __TEXT("ResolutionChange 640 480 8\n")
                   __TEXT("ResolutionChange 640 480 16\n")
                   __TEXT("ResolutionChange 800 600 8\n")
                   __TEXT("ResolutionChange 800 600 16\n")
                   __TEXT("ResolutionChange 1024 768 8\n")
                   __TEXT("ResolutionChange 1024 768 16\n")
                   __TEXT("ResolutionChange 1024 768 32\n")
                   __TEXT("8=256colors\n16=16bit medium\n32=32bit high\n");
            DEVMODE sDevMode;
            ZeroMemory(&sDevMode, sizeof(DEVMODE));
            sDevMode.dmSize = sizeof(DEVMODE);

            sDevMode.dmPelsWidth = atoi(argv[1]);
            sDevMode.dmFields |= DM_PELSWIDTH;

            sDevMode.dmPelsHeight = atoi(argv[2]);
            sDevMode.dmFields |= DM_PELSHEIGHT;

            sDevMode.dmBitsPerPel = atoi(argv[3]);
            sDevMode.dmFields |= DM_BITSPERPEL;

            LONG rVal = ChangeDisplaySettings(&sDevMode, CDS_UPDATEREGISTRY);
            switch (rVal)

            case DISP_CHANGE_SUCCESSFUL:  //The settings change was successful.

            case DISP_CHANGE_RESTART:        //The computer must be restarted in order
                                                       //  for the graphics mode to work.
                   szMsg = __TEXT("You must restart your computer before the ")
                               __TEXT("new settings will take effect.\n\n")
                               __TEXT("Do you want to restart your computer now?");

            case DISP_CHANGE_BADFLAGS:        //An invalid set of flags was passed in.
                   szMsg = "An invalid set of flags was passed in";

            case DISP_CHANGE_FAILED:        //The display driver failed the specified
                                                        //  graphics mode.
                   szMsg = "The display driver couldn't change the display.";

            case DISP_CHANGE_BADMODE:        //The graphics mode is not supported.
                   szMsg = "Invalid settings.\nOR Display mode cannot be set" ;

            case DISP_CHANGE_NOTUPDATED:  //Windows NT only: Unable to write settings
                                                        //  to the registry.                        
                   szMsg = "Unable to write settings to the registry." ;
                   szMsg = "Undocumented return value!!!!\n";

      if (szMsg) MessageBox(NULL, szMsg, "Screen Resolution 32", MB_OK);

      return 0;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>This is a Windows Programming question, pure and simple.

Hi jhance,
I agree that this is a Windows Programming question; however, I still don't think it's inappropriate to keep this question in the C++ topic area.

Most questions posted in this topic area are not pure C++ question.
One of the reasons I participate in EE instead of the C++ newsgroup is because you can ask anything C++ programming related, and not have worry about a bunch of topic police.

When I want to find out how to program something in Windows or UNIX, I prefer to get an answer from another C++ programmer, then to get it from a generic programmer.

Furthermore, I also find I get answers quicker and a much better answer if I post them in the C++ topic area.

I have posted UNIX programming questions simultaneously in the UNIX programming topic area and in the C++ topic area.  And I always get an answer in the C++ topic area first.

My rule of thumb is that as long as the question can be posted with C++ code, then there should be no problem with keeping the question in the C++ topic area.

I agree with the other experts in that changing the screen resolution is generally frowned upon.

If you really feel that this functionality will benefit the user, then I highly recommend that at the minimum you add an option to your program so as to allow the user to disable this feature.

You should also consider that some disable users keep the screen resolution low so as to allow them to read the screen with the larger fonts.
Your program would be of no use to such handicap users if you force the resolution to a size they can’t read.
I agree in some degree that this question could be asked here. But on the other hand. The answer is a pure Windows API answer valid in every other language. Ofcurse here he got it in C/C++.

Anyway, I hope WillowBrown got the answer he wanted. Even if I still think it's not a good idee to change the resolution. The user should be the one selecting resolution to his/hers needs.

When I right programs, I usually try to target for a 800x600 window as my MAX.
If your program can not fit into a 800x600 window because it has too many fields, consider adding TABs to your window, and then seperating your fields into multiple tabs.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.