Solved

Change screen resolution in .NET 3.5

Posted on 2011-03-14
17
531 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:kyleboca
  • 7
  • 7
  • 2
17 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
Comment Utility
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
 

Author Comment

by:kyleboca
Comment Utility
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
 
LVL 33

Expert Comment

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

Author Comment

by:kyleboca
Comment Utility
If you have no idea then don't post in my threads.
0
 
LVL 33

Expert Comment

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

Author Comment

by:kyleboca
Comment Utility
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
 
LVL 52

Expert Comment

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

Author Comment

by:kyleboca
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
...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
 

Author Comment

by:kyleboca
Comment Utility
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 300 total points
Comment Utility
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
 
LVL 33

Expert Comment

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

Author Comment

by:kyleboca
Comment Utility
Thanks gentlemen. I will get headed in that direction.
0
 
LVL 33

Expert Comment

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

Author Comment

by:kyleboca
Comment Utility
Excellent information tgerbert. I appreciate it very much.

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Currency Conversion? 1 34
Trouble with References... 5 21
Hide Tab Page 3 18
C# DataGridView_RowsAdded event not firing 1 36
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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