A Win32 Conversation. ..probing for screen size.
The problem discussed here is that of screen size. I myself like the main window of a program to cover nearly all of the screen, only leaving the taskbar free ( exposed ) at the bottom. But not everyone is using 800 by 600 resolution, as I do; some use bigger, some smaller.
Somewhere I read about a call to a function GetSystemMetrics() to get the width and height of your screen. But I also found out that this function call was not ( yet ) implemented in Cuny's Win32lib. (It has since been implemented).
So I started searching, using Win32.hlp ( a rather large file, that can be downloaded from the Euphoria archives, and other programming sites ), and the dsearch.exw program found in the \Euphoria\Demo\Win32. folder. I struggled with it and eventually got it working. It was not until then, that I found out that Jacques Deschênes also had used it in some demo's he provided.
Here are the include file getsysms.ew and program file screensize1.exw, with some comments.
--
getsysms.ew:
-- an implementation of GetSystemMetrics
-- the function returns the width and height of total screen
-- to be used as nWidth and nHeight in create()
-- which are the 6th and 7th element of this function
-- most of the code 'taken' from Jacques Deschênes
-- Ad Rienks, September 13, 1998
include dll.e
include msgbox.e
integer ok
constant OpenFailed = "Failed to open user32.dll"
constant DefineFailed = "Failed to define one of the user32 functions."
constant FatalErr = "Fatal error in getsysms.ew"
constant user32 = open_dll("user32.dll")
if not user32 then
ok = message_box(OpenFailed, FatalErr, 0)
abort(1) -- if the 'open_dll' failed, report the error and exit.
end if
-- link to the function in user32.dll.
constant iGetSystemMetrics = define_c_func(user32, "GetSystemMetrics",
{C_INT}, C_INT)
if iGetSystemMetrics = -1 then
ok = message_box(DefineFailed, FatalErr, 0)
abort(1)
end if
-- this is the function that our demo program calls.
global function GetSystemMetrics(integer Screen)
return c_func(iGetSystemMetrics, {Screen})
end function
-- end of getsysms.ewFor beginners:
All the Windows DLL's are really just librarys of program code which you can use for your own purposes in Euphoria programs. In the example above we are calling the GetSystemMetrics function in the ( user32.dll ) library to return the two values which tell us the full screen size your computer is set up for.
The above code is also called a
'wrapper', because it acts as a translator to allow Euphoria code to
communicate with the 'C' code in which Windows DLL's are written.
Most Win32lib 'wrappers' are called .ew's, just as Win32lib.ew
is, itself.
--
screensize1.exw
-- Demo using GetSystemMetrics to read screenwidth and screenheight
-- (I like a Windows program to fill (most of) my screen!)
include getsysms.ew
include win32lib.ew
without warning
integer xScreen, yScreen
-- these are calls to an API function, declared in the getsysms.ew include.
xScreen = GetSystemMetrics(SM_CXSCREEN)
yScreen = GetSystemMetrics(SM_CYSCREEN)
constant Main = create(Window, "Adjusting screen", 0, 0, 0,
xScreen, -- calculated screenwidth.
yScreen - 30, -- and screenheight - 30.
0)
procedure onPaint_Main(atom self, atom event, sequence params) --(integer x1,integer y1,integer x2,integer y2)
-- behaviour when the main window paints/repaints itself.
setPenPos(Main, 0, 0)
wPuts(Main, "ScreenWidth = " & sprintf("%d ", xScreen))
setPenPos(Main, 0, 20)
wPuts(Main, "ScreenHeight = " & sprintf("%d ", yScreen))
-- ^^ prints out the screen dimensions at the specified positions.
end procedure
-- linking the onPaint 'event' for the main window and the procedure
-- to call. (the 'method' in OOP lingo)
setHandler( Main, w32HPaint, routine_id("onPaint_Main") )
WinMain(Main,Normal)
-- end of screensize1.exw..and here's a 'trick' screen shot, in which the x and y values which determined the actual screen size were divided by three, to give this 'tiny' view.
I hope I've used all my terms in the right way; maybe this is the biggest problem I encountered, reading about Windows programming. You'll have to learn a whole new jargon, about 'events' that 'send messages' to 'handles' and 'methods' to 'get' and/or 'set' 'properties' of 'instances' or of 'objects'. Maybe I'm 'swearing in church' now, but I don't think you'll have to know exactly what this all means, as long as you understand what the programs are doing and can replicate some of it in your own applications.
Homework question for today: Are there other ways to display the information about ScreenWidth and ScreenHeight? How would you do that?
Note: The GetSystemMetrics() function has been included in Win32lib for quite some time. I haven't wanted to change this lesson, because it gives a very simple view of how Win32lib has been created, by calling Windows functions - in just this way.
Now you can just use something
like:
the_screen_width = w32Func(
xGetSystemMetrics,{SM_CXSCREEN})
... and, of course, you
don't have to 'include' getsysms.ew.
..end of lesson.