# Changing screen resolution through Ruby

**URL:** <https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751>\
**Category:** Ruby\
**Created:** [November 10, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751 "2009-11-10T19:31:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 10, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/1 "2009-11-10T19:31:09Z")

</div>

Hi All,  
Thanks in advance to all the helpers.

I want to change screen resolution using ruby or selenium.

Please help me and let me know if it is possible in either way.

---

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 10, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/2 "2009-11-10T20:18:43Z")

</div>

Saturn wrote:

> Hi All,  
> Thanks in advance to all the helpers.
> 
> I want to change screen resolution using ruby or selenium.
> 
> Please help me and let me know if it is possible in either way.

## Do you need to change screen resolution, or simply window size? Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 10, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/3 "2009-11-10T20:26:07Z")

</div>

Saturn wrote:

> Hi All,  
> Thanks in advance to all the helpers.
> 
> I want to change screen resolution using ruby or selenium.
> 
> Please help me and let me know if it is possible in either way.

For Windows, there’s a WinAPI function I think, but I don’t know about  
Linux or Mac OS.

Btw. is there any good reference on how to manipulate the X server? I’d  
like to add some more automation functions to my xdo gem, but I wasn’t  
able to find out how to interact directly with X…

Marvin

---

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 11, 2009, 7:29am UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/4 "2009-11-11T07:29:52Z")

</div>

On Nov 11, 12:26 am, Marvin Gülker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Btw. is there any good reference on how to manipulate the X server? I’d  
> like to add some more automation functions to my xdo gem, but I wasn’t  
> able to find out how to interact directly with X…
> 
> ## Marvin
> 
> Posted viahttp://www.ruby-forum.com/.

Yes I need to change screen resolution itself like from 1280 X 800 to  
800 X 600.  
I can read the resolutions through win API but can’t write  
If you any way to change please let me know

- thanks

---

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 11, 2009, 11:49am UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/5 "2009-11-11T11:49:49Z")

</div>

Saturn wrote:

> On Nov 11, 12:26ï¿½am, Marvin Gï¿½lker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Btw. is there any good reference on how to manipulate the X server? I’d  
> > like to add some more automation functions to my xdo gem, but I wasn’t  
> > able to find out how to interact directly with X…
> > 
> > ## Marvin
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> Yes I need to change screen resolution itself like from 1280 X 800 to  
> 800 X 600.  
> I can read the resolutions through win API but can’t write  
> If you any way to change please let me know
> 
> - thanks

I’ve searched through Google and the Windows API, and found that this  
function can do the trick: ChangeDisplaySettings

> **[ChangeDisplaySettingsA function (winuser.h) - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-changedisplaysettingsa?redirectedfrom=MSDN)**
>
> The ChangeDisplaySettings function changes the settings of the default display device to the specified graphics mode. (ANSI)

An article on how to use it I found here:

> **[Changing Screen Orientation Programmatically](https://learn.microsoft.com/en-us/previous-versions/ms812499(v=msdn.10)?redirectedfrom=MSDN)**

You could use the function itself easily, but building up the DEVMODE  
struct seems to be very complex (note that I’m using the win32-api gem  
for such tasks). In C, you only have to change the values of a DEVMODE  
you want to change, but since you can’t access a struct from within  
Ruby, you have to build up the whole struct and then use Array#pack to  
turn it into a string which you can pass into the WinAPI function.  
That’s no problem with easy structs like the POINT struct - just do [0,  
0].pack(‘LL’) - but I’ve never found out how to manage that with complex  
ones, especially with ones containing other structs. If someone knows  
how to simplify that (besides a C extension written for the  
MSVC-compiled Ruby version), please let me know.

Marvin

---

<div class="post-metadata">

**Author:** ![Saturn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Saturn](https://www.ruby-forum.com/u/Saturn)\
**Post date:** [November 11, 2009, 1:20pm UTC](https://www.ruby-forum.com/t/changing-screen-resolution-through-ruby/178751/6 "2009-11-11T13:20:28Z")

</div>

Thanks Marvin,  
As you said problem is same here, I am unable to make it’s arguments:  
I tried following:  
require ‘Win32API’  
@e = Win32API.new(“user32”,“ChangeDisplaySettings”, [‘I’,‘I’], ‘I’ )  
p @e.call(800, 600)

it’s giving me error Or nothing if I remove p from the last line.

If any body has a clean solution like initialized all other DEVMODE  
settings with default and changing only resolution part please let me  
know.  
Thanks for helping me.
