Link to home
Start Free TrialLog in
Avatar of kyleboca
kyleboca

asked on

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

SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kyleboca
kyleboca

ASKER

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?

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
If you have no idea then don't post in my threads.
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).
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.
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.
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?

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)
...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.
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?

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks gentlemen. I will get headed in that direction.
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

Excellent information tgerbert. I appreciate it very much.

I will give this a try. I have not been successful thus far.