Screen resolution problem

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
Hi, I have been away from VB.NET for coming up to a year now and am well rusty to my horry :(

I have an app which will have two forms, one 1024*768 and one 800*600.

I want my application to determine what resolution they are running at and open the correct form.

Any pointers to what object I should be using? I tried the screen object but that talks about rectangles etc?

Thnx

Hog
 
If you want to determine the users current resolution, you can use the following code

Visual Basic:
SystemInformation.PrimaryMonitorSize.Width
SystemInformation.PrimaryMonitorSize.Height

I've attached a class that you can use in your project to change the resolution. Don't remember where I found it but it works. Just call the
ChangeRes method and give it the height and width that you want to change your users resolution too. Hope this helps.

-=Simcoder=-
 

Attachments

I've attached a class that you can use in your project to change the resolution. Don't remember where I found it but it works. Just call the
ChangeRes method and give it the height and width that you want to change your users resolution too. Hope this helps.

I've used the class and it was successful, however, on the line lResult = ChangeDisplaySettings(DevM, CDS_TEST) and also on the line lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY) I get the following:

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'ChangeResolution!WindowsApplication1.Resolution::ChangeDisplaySettings' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Is this some kind of warning? Is it safe to use it anyways? Is it possible to fix? How?
 
It sounds like one of the definitions is incorrect try
Visual Basic:
Private Declare Auto Function EnumDisplaySettings Lib "user32" (ByVal deviceName As String, ByVal modeNum As Integer, ByRef deviceMode As DEVMODE) As Integer

Private Declare Auto Function ChangeDisplaySettings Lib "user32" (ByRef deviceMode As DEVMODE, ByVal flags As Integer) As Integer
for the declarations and see if that makes a difference.
 
With your declarations, it didn't work... However, I added the "Alias" from the old declarations into yours and it worked... Is this ok?

But I have another problem (not yet tested with these new declarations), I'm trying to run this on Vista too, but it does not work as on XP. I have created a form with 2 buttons, one changes the res to 1024x768 and the other to 1280x800. These works fine on XP, I can change to either one of them, but on Vista, I can change the res to 1024 but I can't change it back to 1280... The code is exact the same, any reason why?
 
Out of interest try changing the declares to begin with
Visual Basic:
Private Declare Unicode
but without the alias and see if that makes a difference, the only thing I can imagine causing problems on Vista is the security / UAC model - will check when I get home tonigh as I'm not running Vista on this PC.
 
What about the Auto keyword? should I add it or not?

I don't think it's the UAC, I'm running VS2005 with administrator rights (maybe the compiled application is not), anyway, as I said, the resolution changes to 1024x768 when I'm at 1280x800 but I can't change back from 1024x768 to 1280x800, dunno why...
 
Didn't work, I tried Unicode instead of Auto and with out the Alias but it didn't work so I just reverted to Auto with the Alias. It works that way...

However, the same problem persists... On Vista, I can easily change to 1024x768 but not back to 1280x800... Dunno why... Already tried to run the compiled exe with the administrative privileges but it didn't help...
 
There's a long instead of an integer in one of the api definitions.

My version attached.

I tried to use your code but it still doens't work on Vista... It works fine on XP, I can change to 1024x768 and 1280x800 back and forth, but on Vista, I can't swap to 1280x800 but it works swapping to 1024x768...

Anyone knows why?
 
Back
Top