Change screen resolution in .NET 3.5

I need to change the screen resolution on XP machines using C# .NET 3.5 when a certain program launches. I don't need to "GET" anything such as available resolution settings, primary monitor, etc. This is all in house projects with the same machines and same software.

I have Googled this extensively. Some of the code I am reading is way too much. The code that is perfect is attached but I can't get it to work. It compiles and runs just fine without errors but doesn't change the resolution.

If someone could look at the attached code or offer a simple solution I would appreciate it very much.

Thanks in advance for your help.
proc.StartInfo.Arguments = "/x 800 /y 600"; // this will change the screen resolution to

800x600

using System;

using System.Windows.Forms;

using System.Diagnostics;

class Test

{

static void Main()

{

Screen scr = Screen.PrimaryScreen;

int oldWidth = scr.Bounds.Width;

int oldHeight = scr.Bounds.Height;

Process proc = new Process();

proc.StartInfo.FileName = @"c:\qres\ApplicationName.exe"; // put full path in here

proc.StartInfo.Arguments = "/x 800 /y 600"; // say

proc.Start();

proc.WaitForExit();

Console.WriteLine("Press enter to change back to original resolution and exit program");

Console.ReadLine();

proc.StartInfo.Arguments = "/x " + oldWidth.ToString() + "/y "+ oldHeight.ToString();

proc.Start();

proc.WaitForExit();

}

}

Open in new window

kylebocaAsked:
Who is Participating?
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.

Carl TawnSystems and Integration DeveloperCommented:
I don't think you can do it directly through the .Net framework, so you will have to fall back on the Windows API.

There is a sample of retrieving the available resolutions and changing the resolution here:

    http://www.news2news.com/vfp/?example=374&ver=vcs
0
kylebocaAuthor Commented:
Your link is the exact code I was referring to as way too much.

proc.StartInfo.Arguments = "/x 800 /y 600";  The author of this statement says it will change the resolution but it doesn't work for me but others have said that it does.

Can you give me a simple example of using a Windows API? This will require importing a DLL?

0
Todd GerbertIT ConsultantCommented:
I have no idea if it contains what you need, but the XNA Framework has .Net wrappers around some of the DirectX stuff: http://msdn.microsoft.com/en-us/aa937791.aspx
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kylebocaAuthor Commented:
If you have no idea then don't post in my threads.
0
Todd GerbertIT ConsultantCommented:
It's called a useful hint, jerk-bag - I'm not gonna write your code for you for free.  Get off your lazy butt and go find out for yourself whether or not there's a class in that framework that'll do what you want (or ignore the comment if you already know that it doesn't).
0
kylebocaAuthor Commented:
I pay to use this forum so I expect more than a "hint".

I can assure you that if we were in the same room there would be no name calling.

Please stay out of my threads.
0
Carl TawnSystems and Integration DeveloperCommented:
It's worth bearing in mind that, even though you pay to access the site, none of the "experts" are employed by, or paid by, Experts Exchange. Everybody who contributes to the site does so on voluntarily and on their own time. While it can be frustrating when people post comments that you do not deem relevant (and believe me it happens a lot), being openly hostile to commentors, especially high ranking ones, isn't going to endear you to people and may make people less inclined to offer solutions to your issues in the future.
0
kylebocaAuthor Commented:
Yes, agreed. My comment did come off as hostile but wasn't intended to be. I should have said "please don't post..."  However it didn't warrant being called a name that kindergartners use.

Any more it seems that help is hard to find here if it isn't easy points and that is what triggered my comment. I apologize. Enough said.

Lets get back on point.

I have read that others claim the code I attached worked for them. I have tried but I am unable to get it to work. Based on everything I have read it appears to be too simple. Any comments on this?

0
Todd GerbertIT ConsultantCommented:
carl_tawn's right, but I over-reacted.  Though I stand-by my point that a hint is a perfectly valid comment (many answers to my own questions come from a slight push in the right direction, rather than the answer being spelled out for me).  Also, as was alread pointed out, you don't pay anyone here to give you answers - we're all volunteers.  For that reason I'm not inclined to always hand-out detailed code as an answer (particularly for difficult questions / advanced programmers), seeing as how I'm not in the business of doing other people's work for free - I am happy to help/teach/suggest/hint/throw in my 2 cents though.

Realizing you're not gonna like this, I'm gonna give you another suggestion: check out the Windows API's EnumDisplaySettings() and ChangeDisplaySettings() (http://msnd.microsoft.com/library), it's not terribly difficult to implement them in C# using P/Invoke, and you can find .Net declarations for them at http://www.pinvoke.net/.

(Incidentally, I picked up "jerk-bag" from the character Bender on the television show Futurama, and he is not a kidnergardener)
0
Todd GerbertIT ConsultantCommented:
...they're a couple of the same API's in the article that carl_tawn already alluded to, but that example seemed a little unnecessarily convoluted - probably better to just start from scratch yourself, there's really only a couple lines of code you need.
0
kylebocaAuthor Commented:
Well I spent hours investigating, trying code, trying code from the net and  learning how to import a DLL. I only come here as a last resort. Most of my programming experience is industrial using ladder logic with PLCs. In fact this project is a Windows-based touch-screen HMI. I am a newcomer to .NET. I'm sure you get students or "lazy butt jerk-bags" looking for freebies but I am not one of them.

I will look into ChangeDisplaySettings(). Is EnumDisplaySettings()necessary when I am 100% sure on the hardware and software when this will be used? For example all 9 pieces of hardware are exactly the same. The OS is the same and it will be running the same software. 99% of the time the resolution will be 800 x 600. When 1 program is launched I need to change the resolution on the fly to 1024 x 768 then back to 800 x 600 when the application is closed.

So you can see why the attached code example looked perfect for my situation but apparently the author's claims are not true?

0
Todd GerbertIT ConsultantCommented:
You'll have to pass a DEVMODE structure to ChangeDisplaySettings(), and it needs to filled out correctly. The way to do that is use EnumDisplaySettings() to get a DEVMODE that's already filled  out with whatever the current settings are, change what values need changing, and then pass it to ChangeDisplaySettings.
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
Todd GerbertIT ConsultantCommented:
You might also want to download the Windows SDK - you might need to find the values of API constants in the C++ .h header files from time to time.
0
kylebocaAuthor Commented:
Thanks gentlemen. I will get headed in that direction.
0
Todd GerbertIT ConsultantCommented:
Don't forget to head to www.pinvoke.net, there you'll usually find all the "[DllImport]" and struct definitions you need to make the API's work (much easier to copy & paste that stuff).  Also be sure to check documentation on EnumDisplaySettings() (http://msdn.microsoft.com/en-us/library/dd162611(VS.85).aspx) and ChangeDisplaySettings() (http://msdn.microsoft.com/en-us/library/dd183411(v=VS.85).aspx) for info on the finer nuances.

I believe all you need to do is call EnumDisplaySettings(), passing "null" for the deviceName parameter, and -1 for the iMode parameter, to get the current settings of the current display device.  Change the height & width of the DEVICEMODE struct, and then pass it to ChangeDisplaySettings.

public static bool SetResolution(int Width, int Height)
{
	// Declare a DEVMODE struct and set the dmSize field
	DEVMODE dm = new DEVMODE();
	dm.dmSize = (short)Marshal.SizeOf(dm);

	// EnumDisplaySettings will fill-out that DEVMODE struct for us
	if (!EnumDisplaySettings(null, -1, ref dm))
		return false;

	// Change the height & width, set the dmFields field so
	// ChangeDisplaySettings knows *which* settings you're changing
	dm.dmPelsWidth = Width;
	dm.dmPelsHeight = Height;
	dm.dmFields = (DM.PelsWidth | DM.PelsHeight);

	// On success ChangeDisplaySettings will return 0,
	// otherwise you'll get one of the return values outlined in the docs
	int result = ChangeDisplaySettings(ref dm, 0);
	return result == 0;
}

Open in new window

0
kylebocaAuthor Commented:
Excellent information tgerbert. I appreciate it very much.

I will give this a try. I have not been successful thus far.
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.