Solved

View/Change Screen resolution from within my program

Posted on 2003-11-21
12
3,783 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:WillowBrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9802107
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.
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9802110
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.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9802122
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 9803417
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:

http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/

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

http://www.experts-exchange.com/Community_Support/

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.
0
 

Author Comment

by:WillowBrown
ID: 9806358
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.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9806392
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 9806431
>>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.
0
 
LVL 30

Accepted Solution

by:
Axter earned 250 total points
ID: 9807498
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("\n\n\n")
                   __TEXT("8=256colors\n16=16bit medium\n32=32bit high\n");
      }
      else
      {
            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.
                   break;

            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?");
                   break;

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

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

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

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

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

      return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 9807544
>>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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9807581
WillowBrown,

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.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9809122
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9809760
WillowBrown,

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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move constructor only called if marked noexcept? 6 123
Dynamically allocate memory 9 76
Need some help with Microsoft Visual Studio C++ 2003 5 88
C++ help/ Toy problem 19 71
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question