Solved

View/Change Screen resolution from within my program

Posted on 2003-11-21
12
3,761 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
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:JensUniweb
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:jhance
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now