MonitorOFF

General FreeBASIC programming questions.
Post Reply
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

MonitorOFF

Post by Coolman »

I recovered this code here (thanks) :

http://www.freebasic.net/forum/viewtopi ... sh#p185208

is linked to the library FBGUI

I need a command to turn off the screen only leaving the system active...

This example puts out the screen after 5 seconds ...

I'm curious to see opengl visual changes that you could make ...

Code: Select all

'*******************
' MonitorOFF
'*******************
#include "windows.bi"
#Include "window9.bi"
#Include "GL/glut.bi"

Dim Shared As Integer OpenGL = 1

Sub RenderScene
  Static i As Single
  glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
  glPushMatrix

  glLoadIdentity
  glTranslatef 0.0, 0.0, -3.0
  glRotatef i, 0, 1, 0

  glBegin GL_TRIANGLES
  glColor3f   1.0, 0.0, 0.0     'Red
  glVertex3f  0.0, 1.0, 0.0     'Top Of Triangle Front
  glColor3f   0.0, 1.0, 0.0     'Green
  glVertex3f -1.0,-1.0, 1.0     'Left Of Triangle Front
  glColor3f   0.0, 0.0, 1.0     'Blue
  glVertex3f  1.0,-1.0, 1.0     'Right Of Triangle Front
  glColor3f   1.0, 0.0, 0.0     'Red
  glVertex3f  0.0, 1.0, 0.0     'Top Of Triangle  Right
  glColor3f   0.0, 0.0, 1.0     'Blue
  glVertex3f  1.0,-1.0, 1.0     'Left Of Triangle Right
  glColor3f   0.0, 1.0, 0.0     'Green
  glVertex3f  1.0,-1.0,-1.0     'Right Of Triangle Right
  glColor3f   1.0, 0.0, 0.0     'Red
  glVertex3f  0.0, 1.0, 0.0     'Top Of Triangle Back
  glColor3f   0.0, 1.0, 0.0     'Green
  glVertex3f  1.0,-1.0,-1.0     'Left Of Triangle Back
  glColor3f   0.0, 0.0, 1.0     'Blue
  glVertex3f -1.0,-1.0,-1.0     'Right Of Triangle Back
  glColor3f   1.0, 0.0, 0.0     'Red
  glVertex3f  0.0, 1.0, 0.0     'Top Of Triangle Left
  glColor3f   0.0, 0.0, 1.0     'Blue
  glVertex3f -1.0,-1.0,-1.0     'Left Of Triangle Left
  glColor3f   0.0, 1.0, 0.0     'Green
  glVertex3f -1.0,-1.0, 1.0     'Right Of Triangle Left
  glEnd
  glPopMatrix
  i = i + 2.0f
  OpenGLGadgetSwapBuffers opengl
End Sub

'http://www.codeproject.com/Tips/141076/Transparent-OpenGL-window
Var win = OpenWindow ("", 0,0,640,480,  WS_POPUP Or WS_VISIBLE, WS_EX_LAYERED )
SetLayeredWindowAttributes(win, 0, 0, LWA_COLORKEY)
CenterWindow win

OpenGLGadget(OpenGL, 0, 0, WindowClientWidth(win), WindowClientHeight(win))

'glEnable GL_ALPHA_TEST
glEnable GL_DEPTH_TEST
'glEnable GL_COLOR_MATERIAL

'Transparent-OpenGL-window
'glEnable GL_BLEND
'glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
glClearColor 0, 0, 0, 0

Dim Deb As Double
Deb = Timer
Do
    RenderScene
    Sleep 1
Loop Until (Timer - Deb) > 5
SendMessage(GetForegroundWindow(),WM_SYSCOMMAND,SC_MONITORPOWER,2)
End

Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

weird. with win8 the window is invisible. but with win7 is not the case ...

if someone has the solution. I'd be glad to hear ...
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MonitorOFF

Post by VANYA »

Coolman wrote:weird. with win8 the window is invisible. but with win7 is not the case ...

if someone has the solution. I'd be glad to hear ...
Yes, I too had tried this code. On windows XP window is transparent, but with windows 7 I could not do it.
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

I just tried a winxp vm. the window is invisible. The most surprising is that it also works in a win2000 vm ...
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Re: MonitorOFF

Post by Merick »

I haven't tested this so I can't say if it's the problem, but on win7 try turning off Aero.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: MonitorOFF

Post by VANYA »

If you change the style of the windows on the simplified windows7 or classic, the window transparency is work
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

Thank you for your answers. win7 version tested is the basic version (no aero). the computer is a laptop with optimus. this technology allows you to use two graphics cards available intel and nvidia. the switch is automatically executed if the program requires a great graphic power (3d). by disabling the visual style. monitoroff works as expected, but in this case the display is very ugly. it is not a bug. another inconsistency microsoft. so no solution yet ...
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

Following is the update of Windows 8 to version 8.1. I have the same problem. transparency is inactive.it s a same computer. I have a little time today. Here is a version of MonitorOFF that should work on all versions of Windows ...

Code: Select all

'*******************
' MonitorOFF
'*******************
#include once "windows.bi"
#Include once "fbgfx.bi"

Dim xres As Integer, yres As Integer, x As Integer, y As Integer, s As String, Deb As Double
 
xres=540 : yres=50
ScreenRes xres, yres, 32,, fb.GFX_NO_FRAME
s = "--- EXTINCTION DE L'ECRAN DANS CINQ SECONDES ---"
x = (xres - Len(s) * 8) \ 2 : y = (yres - 1 * 8) \ 2
Line (0,0)-Step(xres,yres),RGB(127, 0, 0),BF
Line (2,2)-Step(xres-5,yres-5),RGB(255,255,255),b
Line (3,3)-Step(xres-7,yres-7),RGB(255,255,255),b
Draw String (x, y), s
Deb = Timer
Do		
	 Sleep 1
Loop Until (Timer - Deb) > 5
SendMessage(GetForegroundWindow(),WM_SYSCOMMAND,SC_MONITORPOWER,2)
End
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

Update... for some unknown reason, windows 7 refuses to block the session after some time of inactivity while it configures for, I might have to restart the system that currently has an uptime of about 51 days (this may be the reason for the bug) but I don't want to do it.... so I modified MonitorOFF by adding the LockWorkStation option...

Here is the code:

Code: Select all

'**********************************
' MonitorOFF and LockStation
'**********************************
#include once "windows.bi"
#Include once "fbgfx.bi"

Dim xres As Integer, yres As Integer, x As Integer, y As Integer, s As String, Deb As Double
Dim osvi As OSVERSIONINFO
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO)

xres=640 : yres=50
ScreenRes xres, yres, 32,, fb.GFX_NO_FRAME
s = "MISE EN VEILLE DE L'ECRAN ET BLOCAGE DE LA SESSION DANS CINQ SECONDES"
x = (xres - Len(s) * 8) \ 2 : y = (yres - 1 * 8) \ 2
Line (0,0)-Step(xres,yres),RGB(127, 0, 0),BF
Line (2,2)-Step(xres-5,yres-5),RGB(255,255,255),b
Line (3,3)-Step(xres-7,yres-7),RGB(255,255,255),b
Draw String (x, y), s
Deb = Timer
Do		
	 Sleep 1
Loop Until (Timer - Deb) > 5
SendMessage(GetForegroundWindow(),WM_SYSCOMMAND,SC_MONITORPOWER,2)
If GetVersionEx(@osvi) <> 0 Then
  	Sleep(1000)
	LockWorkStation()
End If
End
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: MonitorOFF

Post by MrSwiss »

@Coolman,
since the OS version check code only proofes: that the ver. is >= Win2000 (aka: NT 5.0),
it can safely be removed (interface implemented since NT 5.0, WinXP = 5.1).
You are only checking, if it is implemented, nothing else (would require more checks).

Thus, a simplified version, of your code (and, english MSG):

Code: Select all

'
' Monitor=OFF and lock Session/PC
'
#include once "windows.bi"
#Include once "fbgfx.bi"

'#Define _screendef_ ' just comment it out, after defining the visals (then recompile)

' constants
Const As UShort xres = 472, yres = 80       ' screen related definitions
Const As ULong  white = &hFFFFFFFF, d_red = &hFF7F0000  ' color's def.
Const As String s = "In 5 Seconds: switch OFF Monitor and lock Session!"
' variables
Dim As Double   startt                      ' for start-timer value

' define screen: size, color depth, appearance and position priority
ScreenRes(xres, yres, 32,, fb.GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP)
Width xres\8, yres\16                       ' 8 x 16 font size
Color(white, d_red) : Cls                   ' preset colors

' initialize display stuff
Line (0, 0)-Step(xres, yres), d_red, BF     ' filled box
Line (2, 2)-Step(xres-5, yres-5), white, B  ' draw border (1)
Line (4, 4)-Step(xres-9, yres-9), white, B  ' draw border (2)
Locate 3, 5 : Print s                       ' info to user

#Ifdef _screendef_
    Sleep(5000) : End 1                     ' quit early (until visuals are defined)
#EndIf  ' _screendef_

' the real action starts below ...
startt = Timer                              ' set start of waiting loop
While Not ((Timer - startt) >= 5.0)         ' run's for 5 seconds
    Sleep(100, 1)                           ' no user interference, permitted
Wend

SendMessage(GetForegroundWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
LockWorkStation()
Sleep(1000, 1) : End 0                      ' wait a second, then terminate
Btw: replaced all *Integer's* with better fitting Data-Type's (reducing memory usage).
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

my version is much simpler but why not, it is what makes the strength of a source code, the possibility to modify it at will... thank you for your modification...

that said, I am happy to see that freebasic continues to evolve, I now often use lazarus/freepascal but freebasic remains unbeatable for small programs...
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: MonitorOFF

Post by Coolman »

Update, after reflection I made a slight modification to let the choice to activate or not the LockWorkStation function, the option will only be active if the program is launched with the /LockStation parameter...

Example : MonitorOFF /LockStation...

Code: Select all

'***************************************
' MonitorOFF and LockStation (option)
'***************************************
#include once "windows.bi"
#Include once "fbgfx.bi"

Dim xres As Integer, yres As Integer, x As Integer, y As Integer, s As String, Deb As Double
'-------------------------------------------------------
Dim As boolean LockStation = false : Dim As Integer i = 1 : Dim As String param
Do
    param = Command(i)
    If Len(param) = 0 Then Exit Do : End If
    If UCase(param) = "/LOCKSTATION" Then LockStation = true : Exit Do : End if
    i += 1
Loop
'-------------------------------------------------------
xres=640 : yres=50
ScreenRes xres, yres, 32,, fb.GFX_NO_FRAME
If LockStation Then
	s = "MISE EN VEILLE DE L'ECRAN ET BLOCAGE DE LA SESSION DANS CINQ SECONDES"
Else
	s = "MISE EN VEILLE DE L'ECRAN DANS CINQ SECONDES"
End If
x = (xres - Len(s) * 8) \ 2 : y = (yres - 1 * 8) \ 2
Line (0,0)-Step(xres,yres),RGB(127, 0, 0),BF
Line (2,2)-Step(xres-5,yres-5),RGB(255,255,255),b
Line (3,3)-Step(xres-7,yres-7),RGB(255,255,255),b
Draw String (x, y), s
Deb = Timer
Do		
	 Sleep(100)
Loop Until (Timer - Deb) > 5
SendMessage(GetForegroundWindow(),WM_SYSCOMMAND,SC_MONITORPOWER,2)
If LockStation Then : Sleep(1000) : LockWorkStation() : End If
End
Post Reply