Getting the size of a monitor
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Getting the size of a monitor
Is there a way to get the size of the current monitor in pixels? As I'd like to open a fullscreen window with my program without the user having to hit the maximise button.
Thanks,
Thanks,
~I see one problem with your reasoning: the fact is thats not a chicken~
Re: Getting the size of a monitor
Look at the "width/height of the Desktop" tip in the FAQ:
viewtopic.php?t=4876
Or, look at the Desktop library in PureBasic's Help.
viewtopic.php?t=4876
Or, look at the Desktop library in PureBasic's Help.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Here's a simple video playback app I did a little while back, this may help you:
Most importantly "GetSystemMetrics_(#SM_CYSCREEN), #PB_Window_SystemMenu | #PB_Window_WindowCentered, "Movie")
" is what you maybe after.
SWAN ...
Code: Select all
If InitMovie() = 0
MessageRequester("Error", "Can't initialize movie playback !", 0)
End
EndIf
MovieName$ = ProgramParameter()
If MovieName$ = "" Or FileSize(MovieName$) < 1
If FileSize("movie.mpg") > 1
MovieName$ = "movie.mpg"
Else
MovieName$ = OpenFileRequester("Choose the movie to play", "", "Movie files | *.avi;*.mpg", 0)
EndIf
EndIf
If MovieName$
If LoadMovie(0, MovieName$) And InitKeyboard()
GetSystemMetrics_(#SM_CYSCREEN), #PB_Window_SystemMenu | #PB_Window_WindowCentered, "Movie")
OpenWindow(0, 0, 0, GetSystemMetrics_(#SM_CXSCREEN), GetSystemMetrics_(#SM_CYSCREEN), #PB_Window_BorderLess, "Movie")
ResizeMovie(0, 0, GetSystemMetrics_(#SM_CXSCREEN), GetSystemMetrics_(#SM_CYSCREEN))
ShowCursor_(#False)
PlayMovie(0, WindowID())
RedrawWindow_(WindowID(),0,0,#RDW_INVALIDATE)
sw_quit.b = 0
Repeat
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Space)
If MovieStatus() = -1
ResumeMovie()
Else
PauseMovie()
EndIf
EndIf
If KeyboardReleased(#PB_Key_Home) And MovieStatus() > 0
MovieSeek(1)
EndIf
If GetAsyncKeyState_(#VK_RIGHT) And MovieStatus() <= (MovieLength()-12) And MovieStatus() > 0
MovieSeek(MovieStatus()+10)
EndIf
If GetAsyncKeyState_(#VK_LEFT) And (MovieStatus() >= 11)
MovieSeek(MovieStatus()-10)
EndIf
If GetAsyncKeyState_(#VK_ESCAPE) Or MovieStatus() >= (MovieLength()-1) Or WindowEvent() = #PB_Event_CloseWindow
sw_quit = 1
EndIf
Until sw_quit = 1
ShowCursor_(#True)
Else
MessageRequester("Error", "Can't load the movie...", 0)
EndIf
EndIf
MovieName$ = ProgramParameter()
If MovieName$ = "" Or FileSize(MovieName$) < 1
If FileSize("Start.exe") > 1
MovieName$ = "Start.exe"
Else
sw_quit = 0
EndIf
EndIf
If sw_quit = 1
RunProgram(MovieName$)
EndIf
End
" is what you maybe after.
SWAN ...
here's some code that may put you in the right direction, fred is implementing dual monitor stuff, but he's still missing monitor coordinates
the following may not run without the rest of the include file, but it may point you in the right direction
the following may not run without the rest of the include file, but it may point you in the right direction
Code: Select all
Procedure.l x_monitor_detect()
Protected info.MONITORINFOEX, device.DISPLAY_DEVICE, n.l
Global x_retval.l, x_monitor_n.l
Global x_monitor_sameformat.l, x_monitor_primary.l
Global x_desktop_x, x_desktop_y, x_desktop_width.l, x_desktop_height.l
;
; *** detect number of screens and retrieve their properties
;
; retval: n - number of screens
; out: x_monitor_n.l - number of screens
; x_monitor_primary.l - number of primary display
; x_monitor_sameformat.l - whaddayah think?
; x_desktop_widthidth.l - desktop width
; x_desktop_heighteight.l - desktop height
; x_desktop_x.l - top left corner of most top left screen
; x_desktop_y.l - top left corner of most top left screen
; x_monitor.x_monitorinfo() - linked list starting at 0 containing monitor info
;
; note:
;
; - forget nt3 nt4 w95... all pretty much hopeless when it comes to multimonitor support
; - set x_multimonitorsupport = #false to disable multimonitor support
; - looks like fred is adding natural support for multi monitor configs in purebasic... oh well... at least i had the excercise :-(
;
If OSVersion() = #PB_OS_Windows_95 Or OSVersion() = #PB_OS_Windows_NT3_51 Or OSVersion() = #PB_OS_Windows_NT_4
;
; just report to the system there is only one monitor...
;
x_monitor_n = 1
x_monitor_primary = 1
x_monitor_sameformat = #True
x_desktop_width = GetSystemMetrics_(#SM_CXSCREEN)
x_desktop_height = GetSystemMetrics_(#SM_CYSCREEN)
x_desktop_x = 0
x_desktop_y = 0
;
ClearList(x_monitor())
AddElement(x_monitor())
x_monitor()\n = x_monitor_n
x_monitor()\name = ""
x_monitor()\flags = 0
x_monitor()\f = 0
x_monitor()\d = 0
x_monitor()\w = x_desktop_width
x_monitor()\h = x_desktop_height
x_monitor()\x = x_desktop_x
x_monitor()\y = x_desktop_y
;
Else
;
;
; *** using GetSystemMetrics_()
;
; provides quite a bit info on display adapters, screens etc. but does not provide info
; per monitor / device on multi monitor setups
;
; x_monitor_n = GetSystemMetrics_(#SM_CMONITORS)
;
x_monitor_sameformat.l = GetSystemMetrics_(#SM_SAMEDISPLAYFORMAT)
;
x_desktop_x = GetSystemMetrics_(#SM_XVIRTUALSCREEN)
x_desktop_y = GetSystemMetrics_(#SM_YVIRTUALSCREEN)
x_desktop_width = GetSystemMetrics_(#SM_CXVIRTUALSCREEN)
x_desktop_height = GetSystemMetrics_(#SM_CYVIRTUALSCREEN)
;
; Debug "same display format on all screens (0 = no)"
; Debug x_monitor_sameformat
; Debug "counted using getsystemmetrics"
; Debug x_monitor_n
;
; *** using EnumDisplayMonitors()
;
; returns monitors overlapping a given rectangle, including pseudo / mirror monitors!
; after the call, for each monitor a handle will be stored in x_monitor()\hnd
; this can be used to obtain more information using GetMonitorInfo_()
; flags returned are different from EnumDisplayDevices()
;
; the results were not always conclusive when using these functions, so i choose to use
; EnumDisplayDevices instead (see below)
;
; ClearList(x_monitor())
; EnumDisplayMonitors_(0,0,@x_monitor_monitorenumproc(),0) ; call the function
; info.MONITORINFOEX ; create a struct for GetMonitorInfo_()
; info\cb = SizeOf(info) ; length of struct (important for MONITORINFO vs. MONITORINFOEX)
; x_monitor_n = 0 ; nr. of monitors
; ResetList(x_monitor()) ; reset and...
; While NextElement(x_monitor()) ; go through the list
; x_monitor_n = x_monitor_n+1
; x_monitor()\n = x_monitor_n ; sequential number
; GetMonitorInfo_(x_monitor()\hnd,@info) ; get info on the monitor
; x_monitor()\x = info\m_x1
; x_monitor()\y = info\m_y1
; x_monitor()\w = info\m_x2 - info\m_x1
; x_monitor()\h = info\m_y2 - info\m_y1
; x_monitor()\name = PeekS(@info\name,32) ; i want my fixed length strings!
; Wend
; ;
; Debug "counted using enumdisplaymonitors"
; Debug x_monitor_n
;
; *** using EnumDisplayDevices_()
;
; walks through all available devices, retrieve names and flags
; by using the returned flags all 'non-real' monitors can be filtered out
;
device.DISPLAY_DEVICE ; to store results from EnumDisplayDevices_()
device\cb = SizeOf(DISPLAY_DEVICE)
settings.DEVMODE ; to store results from EnumDisplaySettings_()
settings\dmSize = SizeOf(settings) ; DEVMODE is defined in purebasic itself
settings\dmDriverExtra = 0
;
ClearList(x_monitor())
;
n.l = 0
x_monitor_n = 0
x_desktop_x = 0
x_desktop_y = 0
While EnumDisplayDevices_(0,n,@device,0) > 0 ; check all devices
n = n+1
;
; the StateFlags field in the filled struct contains information on the device / monitor
; some values to check with (not all cards / drivers support all functions):
;
; #DISPLAY_DEVICE_ATTACHED_TO_DESKTOP - in use and part of the desktop
; #DISPLAY_DEVICE_PRIMARY_DEVICE - primary device
; #DISPLAY_DEVICE_VGA_COMPATIBLE - none reported
; #DISPLAY_DEVICE_MULTI_DRIVER
; #DISPLAY_DEVICE_ACTIVE
; #DISPLAY_DEVICE_ATTACHED
;
If device\StateFlags & #DISPLAY_DEVICE_ATTACHED_TO_DESKTOP ; check if it's part of the desktop
;
; if a device is part of the desktop, additional information can be retrieved using
; EnumDisplaySettings_() with the device name
;
If EnumDisplaySettings_(@device\DeviceName,#ENUM_CURRENT_SETTINGS,@settings) > 0
;
; first store some info found via EnumDisplayDevices_()
;
AddElement(x_monitor())
x_monitor()\n = x_monitor_n
x_monitor()\name = PeekS(@device\DeviceName,32) ; i want my fixed length strings!
x_monitor()\flags = device\StateFlags
;
If x_monitor()\flags & #DISPLAY_DEVICE_PRIMARY_DEVICE
x_monitor_primary = x_monitor_n
EndIf
;
; and more stuff found via EnumDisplaySettings_()
;
x_monitor()\f = settings\dmDisplayFrequency
x_monitor()\d = settings\dmBitsPerPel
x_monitor()\w = settings\dmPelsWidth
x_monitor()\h = settings\dmPelsHeight
x_monitor()\x = PeekL(@settings\dmOrientation) ; the default definition in purebasic didn't include the onion, well, this works as well
x_monitor()\y = PeekL(@settings\dmPaperLength)
;
; x_desktop_x = x_min( x_desktop_x , x_monitor()\x )
; x_desktop_y = x_min( x_desktop_y , x_monitor()\y )
; x_desktop_width = x_max( x_monitor()\x + x_monitor()\w - x_desktop_x , x_desktop_width )
; x_desktop_height = x_max( x_monitor()\y + x_monitor()\h - x_desktop_height , x_desktop_height )
;
; still, the handle is missing, so find it one using a point that is somewhere on the monitor
; theoretically, overlapping devices would be possible and thus screw retrieving any handle...
; haven't seent that though... yet :-( note that enummonitorsettings allows a different way to
; find screen handles
;
; work around for calling the monitorfrompoint function, purebasic tries to get the address of
; the function at program start, thus it will error out on nt4, however by opening the dll
; by hand and looking for the function by name we can circumvent this
;
; x_monitor()\hnd = MonitorFromPoint_(x_monitor()\x,x_monitor()\y,#MONITOR_DEFAULTTONULL)
;
x = OpenLibrary(#PB_Any,"USER32.DLL")
x_monitor()\hnd = CallFunction(x,"MonitorFromPoint",x_monitor()\x,x_monitor()\y,#MONITOR_DEFAULTTONULL)
CloseLibrary(x)
;
x_monitor_n = x_monitor_n+1
EndIf
EndIf
Wend
;
; Debug "counted using enumdisplaydevices"
; Debug x_monitor_n
;
EndIf
;
x_retval = x_monitor_n
ProcedureReturn x_retval
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
I got this way ...
at first i Open an window that is screencentered and windwcenterd , then i can got it like this :
screenh=2*WindowY()+WindowHeight()+24
screenw=2*WindowX()+WindowWidth()+6
hehe..... and it worked。why to add 24 and 6,I don't know....
the new version can got it by the new procedure DesktopWidth()
and the DesktopHeight()
--------follow are chinese ---- 以下为汉语 ------
首先打开一个在屏幕居中的窗口,然后用以下语句得到:
screenh=2*WindowY()+WindowHeight()+24
screenw=2*WindowX()+WindowWidth()+6
hehe.... 咋样?就是不知道为啥加24和6才对...
不过新版可以用desktopwidth()和DesktopHeight()获取。
screenh=2*WindowY()+WindowHeight()+24
screenw=2*WindowX()+WindowWidth()+6
hehe..... and it worked。why to add 24 and 6,I don't know....
the new version can got it by the new procedure DesktopWidth()
and the DesktopHeight()
--------follow are chinese ---- 以下为汉语 ------
首先打开一个在屏幕居中的窗口,然后用以下语句得到:
screenh=2*WindowY()+WindowHeight()+24
screenw=2*WindowX()+WindowWidth()+6
hehe.... 咋样?就是不知道为啥加24和6才对...
不过新版可以用desktopwidth()和DesktopHeight()获取。
So easy - are you shure?
Could'nt believe that it was so easy to obtain... Thank's for the hint, Straker (ROFL) 

Last edited by tms on Mon Nov 07, 2005 6:18 am, edited 1 time in total.