Win32api find display size

Try the following code using the system module below

w,h = System.screenSize

SYSTEM

module System

@getUserName 			= Win32API.new("advapi32", 	"GetUserName", 			['P','P'],

‘L’)
@getComputerName = Win32API.new(“kernel32”, “GetComputerName”,
[‘P’,‘P’], ‘L’)
@getLastError = Win32API.new(“kernel32”, “GetLastError”, [], ‘L’)
@getSystemTime = Win32API.new(“kernel32”, “GetSystemTime”, [‘P’],
‘V’)
@setSystemTime = Win32API.new(“kernel32”, “SetSystemTime”, [‘P’],
‘V’)
@ods = Win32API.new(“kernel32”, “OutputDebugString”, [‘P’],
‘V’ )
@shellExecute = Win32API.new(“shell32”, “ShellExecute”,
[‘L’,‘P’,‘P’,‘P’,‘P’,‘L’], ‘L’)
@getSystemMetrics = Win32API.new(“user32”, “GetSystemMetrics”, [‘I’],
‘I’)

def getUserName

	# 1. Reserve lots of space
	buffer = " "*1000

	# 2. Convert the length
	len = [buffer.length].pack('L')

	# 3. Make the call
	if( @getUserName.call( buffer, len ) == 1 )

		# 4. return computer name if successful
		return buffer[0,len.unpack('L')[0]-1]

	end

	# 5. otherwise return empty string
	""

end

module_function :getUserName

def getSystemTime

	# 1. Allocate space for a SYSTEMTIME structure
	systemtime = " "*16

	# 2. Execute the function
	@getSystemTime.call( systemtime )

	# 3. unpack back to ruby stuff
	yr,mn,dw,dm,hr,mnt,sec,ms = systemtime.unpack('ssssssss')

	# 4. and return a gm time based on the values
	Time.gm(yr,mn,dm,hr,mnt,sec,ms)

end

module_function :getSystemTime

def setSystemTime ( tm )

	raise ArgumentError if !tm.instance_of?(Time)

	# 1. Conver the time to GMT
	gmt = tm.gmtime

	# 2. Build a SYSTEMTIME structure
	systemtime = [gmt.year,gmt.month,gmt.wday,gmt.day,

gmt.hour,gmt.min,gmt.sec,gmt.usec].pack(‘ssssssss’)

	# 3. And call the OS
	@setSystemTime.call( systemtime )

end

module_function :setSystemTime

def getComputerName

	# 1. Reserve lots of space
	buffer = " "*1000

	# 2. Convert the length
	len = [buffer.length].pack('L')

	# 3. make the call
	if( @getComputerName.call( buffer, len ) == 1 )

		# 4. return name if successful
		return buffer[0,len.unpack('L')[0]]
	end

	# 5. otherwise return empty string
	""

end

module_function :getComputerName

def getLastError

	# this one is easy
	@getLastError.call

end

module_function :getLastError

def shellExecute ( p1, p2, p3, p4, show=3 )

@shellExecute.call(0,p1,p2,p3,p4,show)

end

module_function :shellExecute

def ods ( msg )
@ods.call(msg)
end

module_function :ods

···

#------------------------------------------------------------
# setup constants for system metrics
# see windows docs for explanation of values
# in GetSystemMetrics
#------------------------------------------------------------
SM_CXSCREEN = 0
SM_CYSCREEN = 1
SM_CXVSCROLL = 2
SM_CYHSCROLL = 3
SM_CYCAPTION = 4
SM_CXBORDER = 5
SM_CYBORDER = 6
SM_CXDLGFRAME = 7
SM_CYDLGFRAME = 8
SM_CYVTHUMB = 9
SM_CXHTHUMB = 10
SM_CXICON = 11
SM_CYICON = 12
SM_CXCURSOR = 13
SM_CYCURSOR = 14
SM_CYMENU = 15
SM_CXFULLSCREEN = 16
SM_CYFULLSCREEN = 17
SM_CYKANJIWINDOW = 18
SM_MOUSEPRESENT = 19
SM_CYVSCROLL = 20
SM_CXHSCROLL = 21
SM_DEBUG = 22
SM_SWAPBUTTON = 23
SM_RESERVED1 = 24
SM_RESERVED2 = 25
SM_RESERVED3 = 26
SM_RESERVED4 = 27
SM_CXMIN = 28
SM_CYMIN = 29
SM_CXSIZE = 30
SM_CYSIZE = 31
SM_CXFRAME = 32
SM_CYFRAME = 33
SM_CXMINTRACK = 34
SM_CYMINTRACK = 35
SM_CXDOUBLECLK = 36
SM_CYDOUBLECLK = 37
SM_CXICONSPACING = 38
SM_CYICONSPACING = 39
SM_MENUDROPALIGNMENT = 40
SM_PENWINDOWS = 41
SM_DBCSENABLED = 42
SM_CMOUSEBUTTONS = 43
SM_CXFIXEDFRAME = SM_CXDLGFRAME
SM_CYFIXEDFRAME = SM_CYDLGFRAME
SM_CXSIZEFRAME = SM_CXFRAME
SM_CYSIZEFRAME = SM_CYFRAME
SM_SECURE = 44
SM_CXEDGE = 45
SM_CYEDGE = 46
SM_CXMINSPACING = 47
SM_CYMINSPACING = 48
SM_CXSMICON = 49
SM_CYSMICON = 50
SM_CYSMCAPTION = 51
SM_CXSMSIZE = 52
SM_CYSMSIZE = 53
SM_CXMENUSIZE = 54
SM_CYMENUSIZE = 55
SM_ARRANGE = 56
SM_CXMINIMIZED = 57
SM_CYMINIMIZED = 58
SM_CXMAXTRACK = 59
SM_CYMAXTRACK = 60
SM_CXMAXIMIZED = 61
SM_CYMAXIMIZED = 62
SM_NETWORK = 63
SM_CLEANBOOT = 67
SM_CXDRAG = 68
SM_CYDRAG = 69
SM_SHOWSOUNDS = 70
SM_CXMENUCHECK = 71
SM_CYMENUCHECK = 72
SM_SLOWMACHINE = 73
SM_MIDEASTENABLED = 74
SM_MOUSEWHEELPRESENT = 75
SM_XVIRTUALSCREEN = 76
SM_YVIRTUALSCREEN = 77
SM_CXVIRTUALSCREEN = 78
SM_CYVIRTUALSCREEN = 79
SM_CMONITORS = 80
SM_SAMEDISPLAYFORMAT = 81

def getSystemMetrics ( value )
	@getSystemMetrics.call(value)
end

module_function :getSystemMetrics

def screenWidth
getSystemMetrics(SM_CXSCREEN)
end
module_function :screenWidth

def screenHeight
getSystemMetrics(SM_CYSCREEN)
end
module_function :screenHeight

def isMouseInstalled?
numMouseButtons > 0
end
module_function :isMouseInstalled?

def numMouseButtons
getSystemMetrics(SM_CMOUSEBUTTONS)
end
module_function :numMouseButtons

def screenSize
[ getSystemMetrics(SM_CXSCREEN), getSystemMetrics(SM_CYSCREEN) ]
end
module_function :screenSize

def borderSize
[ getSystemMetrics(SM_CXBORDER), getSystemMetrics(SM_CYBORDER) ]
end
module_function :borderSize

def cursorSize
[ getSystemMetrics(SM_CXCURSOR), getSystemMetrics(SM_CYCURSOR) ]
end
module_function :cursorSize

def iconSize
[ getSystemMetrics(SM_CXICON), getSystemMetrics(SM_CXICON) ]
end
module_function :iconSize

end

-----Original Message-----
From: Michael Hale [mailto:michael@halefamilysite.com]
Sent: April 12, 2004 11:32 AM
To: ruby-talk ML
Subject: win32api find display size

Michael Hale wrote in message:

         ?

require ‘Win32API’

SM_CXSCREEN, SM_CYSCREEN = 0, 1
GetSystemMetrics = Win32API.new(‘user32’, ‘GetSystemMetrics’, ‘I’, ‘I’)

x = GetSystemMetrics.call(SM_CXSCREEN)
y = GetSystemMetrics.call(SM_CYSCREEN)

p [x, y] #=> [1280, 1024]

daz

Your message showed up as all blank for me… which I’ve found my
messages do too if I post from Apple Mail with signing enabled. In any
case, I have the following code to get the desktop size, which may or
may not be what you want, as it excludes the space taken up by the task
bar:

require ‘dl/import’
require ‘dl/struct’

module User32
extend DL::Importable

 typealias "BOOL", "int"
 typealias "UINT", "unsigned int"
 typealias "PVOID", "void *"
 typealias "LONG", "long"
 typealias "HWND", "void *"

 RECT = struct [
   "LONG left",
   "LONG top",
   "LONG right",
   "LONG bottom"
 ]

 dlload "user32.dll"
 extern "BOOL SystemParametersInfo(UINT, UINT, PVOID, UINT)"

 SPI_GETWORKAREA = 48

 def self.get_work_area
   rect = RECT::malloc
   if(systemParametersInfo(SPI_GETWORKAREA, 0, rect.to_ptr, 0) != 0)
     [rect.left, rect.top, rect.right - rect.left, rect.bottom - 

rect.top]
else
raise “System call failed”
end
end
end

HTH,

Nathaniel
Terralien, Inc.

<:((><

···

On Apr 12, 2004, at 09:32, Michael Hale wrote:

Hi!

  • Michael Hale:

FAQ suggestion:

Don’t write subject-only mails. Ask your question in the mail body.

Josef ‘Jupp’ Schugt

Thanks Nathaniel. Your code works perfectly, even inside exerb!

···

On Apr 12, 2004, at 10:41 PM, Nathaniel Talbott wrote:

On Apr 12, 2004, at 09:32, Michael Hale wrote:

Your message showed up as all blank for me… which I’ve found my
messages do too if I post from Apple Mail with signing enabled. In any
case, I have the following code to get the desktop size, which may or
may not be what you want, as it excludes the space taken up by the
task bar:

require ‘dl/import’
require ‘dl/struct’

module User32
extend DL::Importable

typealias "BOOL", "int"
typealias "UINT", "unsigned int"
typealias "PVOID", "void *"
typealias "LONG", "long"
typealias "HWND", "void *"

RECT = struct [
  "LONG left",
  "LONG top",
  "LONG right",
  "LONG bottom"
]

dlload "user32.dll"
extern "BOOL SystemParametersInfo(UINT, UINT, PVOID, UINT)"

SPI_GETWORKAREA = 48

def self.get_work_area
  rect = RECT::malloc
  if(systemParametersInfo(SPI_GETWORKAREA, 0, rect.to_ptr, 0) != 0)
    [rect.left, rect.top, rect.right - rect.left, rect.bottom - 

rect.top]
else
raise “System call failed”
end
end
end

HTH,

Nathaniel
Terralien, Inc.

<:((><