About Monkey 2 › Forums › Monkey 2 Code Library › Windows Screensaver (with preview)
This topic contains 0 replies, has 1 voice, and was last updated by
Diffrenzy
1 year, 8 months ago.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
August 17, 2017 at 6:46 am #9880
To test, rename exe to “anynameyoulike.scr” , right click and choose “install”
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234#Import "<libuser32.a>"#Import "<std>"#Import "<windows.h>"#Import "<std>"#Import "<mojo>"Using mojo..Using std..ExternConst SM_CYVIRTUALSCREEN:IntConst SM_CXVIRTUALSCREEN:IntConst WS_CHILD:IntConst WS_CLIPCHILDREN:IntConst WS_CLIPSIBLINGS:IntConst WS_VISIBLE:IntConst GWL_STYLE:IntConst SWP_NOSIZE:IntAlias LPCTSTR:CStringStruct HWND__EndAlias HWND:HWND__ PtrAlias LONG_PTR:LONG PtrStruct RECTField left:LONGField top:LONGField right:LONGField bottom:LONGEndFunction GetClientRect:Bool(in:HWND,out:RECT Ptr)Function GetCommandLineW:LPCTSTR()Function MessageBoxA:Int (hWnd:HWND, lpText:CString, lpCaption:CString, uType:Int)Function SetParent:HWND(child:HWND,parent:HWND)Function FindWindow:HWND( lpClassName:CString,lpWindowName:CString)Function SetWindowLong:LONG Ptr(hWnd:HWND,nIndex:Int, dwNewLong:LONG)Function SetWindowPos:Bool(hWnd:HWND,hWndInsertAfter:HWND,x:Int,y:Int,cx:Int,cy:Int,flags:UInt)Function GetSystemMetrics:int(index:Int)PublicConst cMONKEYWINDOWTITLE:String = "MX2ScreenSaverPreview"Const cMONKEYWINDOWCLASSNAME:String = "SDL_app"Function GetScreenSaverMode:String()'Return "RunSaver" ' unrem if you are testing how your saver looks and your IDE cant send command line params, ( /s for RunSaver )Local cmd:= GetCommandLineW()cmd = cmd.ToLower()'MessageBoxA (Null, cmd,"Mode is", 0)Local pos:Int = cmd.FindLast(" ") ' safe to assume last space denotes /params ?If pos = -1 Then Return "Config"Local param:String = cmd.Right(cmd.Length-pos)If param.Contains("s")Return "RunSaver"Elseif param.Contains("c")Return "Config"Else ' it's a handle to the preview windowLocal parts:= param.Split(" ")If parts.Length>1 Return parts[1] ' previewEndifReturn "Config"End FunctionFunction Config()MessageBoxA (Null, "This screensaver has no configuration options","Screensaver", 0)End FunctionFunction Preview(shwnd:String)Local ihwnd:Int = Cast<Int>(shwnd)Local phwnd:HWND = Cast<HWND>(ihwnd)Local parentrect:RECTLocal result:= GetClientRect(phwnd, Varptr parentrect)If Not result returnLocal w:Int = parentrect.right - parentrect.leftLocal h:Int = parentrect.bottom - parentrect.topNew AppInstanceLocal win:= New MyWindow(cMONKEYWINDOWTITLE,w,h,phwnd,WindowFlags.Borderless,true)App.Run()End FunctionFunction RunSaver()New AppInstanceLocal parent:HWNDLocal w:=GetSystemMetrics( SM_CXVIRTUALSCREEN )Local h:=GetSystemMetrics( SM_CYVIRTUALSCREEN )Local win:= New MyWindow("Full",w,h,parent,WindowFlags.Borderless | WindowFlags.Fullscreen, false)App.Run()End FunctionFunction Main()Local mode:= GetScreenSaverMode()'MessageBoxA (Null, mode,"Commandline is", 0)Select modeCase "RunSaver"RunSaver()Case "Config"Config()DefaultPreview(mode)End selectEndClass MyWindow Extends WindowField mytitle:StringField count:intField parent:HWNDField parentSet:BoolField previewmode:BoolField lastx:IntField lasty:intMethod New(title:String, width:Int,height:int,parent:HWND ,flags:WindowFlags,runinpreviewmode:Bool)Super.New( title,width,height,flags)Self.parent = parentSelf.parentSet = parentSetpreviewmode = runinpreviewmodeIf Not previewmode Mouse.PointerVisible = FalseEndMethod OnMouseEvent(event:MouseEvent ) OverrideIf previewmode returnIf event.Type=EventType.MouseDown App.Terminate()If event.Type=EventType.MouseUp App.Terminate()If event.Type=EventType.MouseWheel App.Terminate()If event.Type=EventType.MouseMoveLocal dx:= event.Location.x - lastxLocal dy:= event.Location.y - lastyIf dx>3 Or dy >3 ' move a little befor quittingIf lastx<>0 And lasty<>0 ' on first pass these are 0App.Terminate()endifEndiflastx = event.Location.xlasty = event.Location.yEndifEndMethod OnKeyEvent( event:KeyEvent ) OverrideIf previewmode returnApp.Terminate()EndMethod OnRender( canvas:Canvas ) OverrideIf previewmode And parentSet = False' one time setup' MX2 / SDL creates several windows, this is the one we want:Local me:= FindWindow(cMONKEYWINDOWCLASSNAME,cMONKEYWINDOWTITLE)' clear WS_POPUP and set WS_CHILD style before changing parentConst previewstyle:LONG = WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGSSetWindowLong(me,GWL_STYLE,previewstyle)' make the render window a child of the Windows Screensaver Preview windowSetParent(me,parent)' move the window to 0,0 because we are now a child of the Windows Screensaver Preview windowLocal dummy:HWNDSetWindowPos(me,dummy,0,0,Width,Height,SWP_NOSIZE) ' SWP_NOSIZE to ignore size, because it's already set so Width,Height could be 0 here...parentSet = TrueEndif' render something!App.RequestRender()Local w:=WidthLocal h:=Heightcanvas.Color=Color.Redcanvas.DrawLine( 0,0,w-1,0 )canvas.DrawLine( w-1,0,w-1,h-1 )canvas.DrawLine( w-1,h-1,0,h-1 )canvas.DrawLine( 0,h-1,0,0 )canvas.LineWidth=2canvas.LineSmoothing=Truecanvas.Color=Color.Orangecanvas.DrawLine( 2,2,w-3,2 )canvas.DrawLine( w-3,2,w-3,h-3 )canvas.DrawLine( w-3,h-3,2,h-3 )canvas.DrawLine( 2,h-3,2,2 )SeedRnd( Millisecs() )For Local i:=0 Until 100canvas.LineWidth=Rnd( 2,5 )canvas.Color=New Color( Rnd(1),Rnd(.75),Rnd(.5) )canvas.DrawLine( Int( Rnd(w) ),Int( Rnd(h) ),Int( Rnd(w) ),Int( Rnd(h) ) )Nextcanvas.DrawText( "Hello Screensaver World",Width/2,Height/2,.5,.5 )EndEnd -
AuthorPosts
Viewing 1 post (of 1 total)
You must be logged in to reply to this topic.