change screen resolution in C#

E

Ed Hochberg

Hi all, I found the following code on the web. It says it is for C#. Problem
is that it seems to be for ASP (it uses the Page_Load procedure) and I need
it for a regular Windows form. can anyone suggest how it may be modified for
my needs.

Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Page_Load Code

Screen Srn = Screen.PrimaryScreen;

tempHeight = Srn.Bounds.Width;

tempWidth = Srn.Bounds.Height;

Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script
type=\"text/javascript\">alert('" + "Your Current Resolution is = " +
tempHeight + " * " + tempWidth + "');</script>");

//if you want Automatically Change res.at page load. please uncomment this
code.



if (tempHeight == 600)//if the system is 800*600 Res.then change to

{

FixHeight = 768;

FixWidth = 1024;

Resolution.CResolution ChangeRes = new Resolution.CResolution(FixHeight,
FixWidth);

}

Change Resoultion

switch (cboRes.SelectedValue.ToString())

{

case "800*600":

FixHeight = 800;

FixWidth = 600;

Resolution.CResolution ChangeRes600 = new
Resolution.CResolution(FixHeight, FixWidth);

break;



case "1024*768":

FixHeight = 1024;

FixWidth = 768;

Resolution.CResolution ChangeRes768 = new
Resolution.CResolution(FixHeight, FixWidth);

break;

case "1280*1024":

FixHeight = 1280;

FixWidth = 1024;

Resolution.CResolution ChangeRes1024 = new
Resolution.CResolution(FixHeight, FixWidth);

break;

}
 
S

Scott M.

While I'm not familiar with all of the code you show, no ASP .NET code can
change a user's screen resolution. You can detect what resolution they have
in JavaScript (and the code you've shown uses ASP .NET C# to generate
client-side JavaScript that is sent to the browser), but you can't change
it.

I would think that to change the screen resolution in a Windows app., you'd
need to access a Windows API.

-Scott
 
A

Arne Vajhøj

Hi all, I found the following code on the web. It says it is for C#. Problem
is that it seems to be for ASP (it uses the Page_Load procedure) and I need
it for a regular Windows form. can anyone suggest how it may be modified for
my needs.

Call the native function ChangeDisplaySettingsEx via DllImport etc..

Arne
 
C

Chris Dunaway

Hi all, I found the following code on the web. It says it is for C#. Problem
is that it seems to be for ASP (it uses the Page_Load procedure) and I need
it for a regular Windows form. can anyone suggest how it may be modified for
my needs.

Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Page_Load Code

Screen Srn = Screen.PrimaryScreen;

tempHeight = Srn.Bounds.Width;

tempWidth = Srn.Bounds.Height;

Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script
type=\"text/javascript\">alert('" + "Your Current Resolution is = " +
tempHeight + " * " + tempWidth + "');</script>");

//if you want Automatically Change res.at page load. please uncomment this
code.

if (tempHeight == 600)//if the system is 800*600 Res.then change to

{

FixHeight = 768;

FixWidth = 1024;

Resolution.CResolution ChangeRes = new Resolution.CResolution(FixHeight,
FixWidth);

}

Change Resoultion

switch (cboRes.SelectedValue.ToString())

{

case "800*600":

FixHeight = 800;

FixWidth = 600;

Resolution.CResolution ChangeRes600 = new
Resolution.CResolution(FixHeight, FixWidth);

break;

case "1024*768":

FixHeight = 1024;

FixWidth = 768;

Resolution.CResolution ChangeRes768 = new
Resolution.CResolution(FixHeight, FixWidth);

break;

case "1280*1024":

FixHeight = 1280;

FixWidth = 1024;

Resolution.CResolution ChangeRes1024 = new
Resolution.CResolution(FixHeight, FixWidth);

break;

}

Arne showed you the Windows API call to change the screen resolution,
but please don't do that! The user has set their resolution to what
they like and you should not change it! You might have the intent of
changing it back when you are through, but if your program crashes,
then the user will have to change it back manually.

Generally speaking, it is bad form to change any of the user's desktop
settings.
 
A

Arne Vajhøj

Arne showed you the Windows API call to change the screen resolution,
but please don't do that! The user has set their resolution to what
they like and you should not change it! You might have the intent of
changing it back when you are through, but if your program crashes,
then the user will have to change it back manually.

Generally speaking, it is bad form to change any of the user's desktop
settings.

I can not see any need to change it either.

But the original poster must have had some idea with it.

Arne
 
S

Scott M.

Arne Vajhøj said:
I can not see any need to change it either.

But the original poster must have had some idea with it.

Arne

It may be for something like a video game, where there is an "Options"
section that allows the user to change resolution for the duration of the
game. It could be that the resolution change is actually initiated by the
user of the program and then the program returns the resolution back to its
original state when the program exits.

You are both right that this shouldn't be done without the user's consent,
but there may well be perfectly acceptable reasons to do it with the user's
concent.

-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top