Write screen saver using VB

zhaozj2021-02-11  185

Write screen saver using VB

http://www.tongyi.net Author: Liu Hits: 628

Friends who are familiar with the Windows operating system must be unfamiliar with the Windows screen saver. How do you write a Windows screen saver? When you read the following explanations, you can easily write a standard Windows screen saver! A standard screen saver has the following features: 1: It is the extension of the file! Two: It has three ways of operation. (1) Run in the preview box (for previewing the effect. In the "Display Properties" → "Screen Saver" → "Small Screen"). (See) (2) Run the setup program (used to set some related styles. In the "Display Properties" → "Screen Saver" → "Click the Setting button"). (3) Real running screensaver (the effect of the screen saver. On the "Display Properties" → "Screen Saver" → "Click Preview" or mouse, the keyboard is not working in the specified time). How to make the screensaver recognition which way is currently running? The answer is simple - Analyze the parameters of Windows calling screensaver. The following is a case where Windows 98 is analyzed to analyze the parameters of the calling screen. When Windows requires a screensaver display in the "Small Screen", you will add two parameters after the call screensaver. Such as: myscr.scr / p 7981 (parameter 1: / P means that the program is displayed in "Small Screen", the parameter 2: 7981 means "small screen" handle hwnd. This screen saver will know that Windows wants it to display In "Small Screen".) When Windows requires a screensaver display settings dialog box, you will not add or add two parameters after the calling screensault. Such as: myscr.scr or myscr.scr / c 7987 (parameter 1: / c means allowing the program display settings dialog, parameter 2: 7987 means the handle of the page.) When Windows is required to run a screen saver, it will call the screensaver Behind the back plus a parameter. Such as: myscr.scr / s (parameter: / s means that the screen saver is running.) Ok, I know how Windows makes three ways to run the screensaver, and then discuss how to implement them. Realization: Windows calls the screensaver in some way, the screen saver knows what it wants to do if you have the same instance in the current environment. If the operation mode of this instance is different from the running mode of this time, close the previous instance, if the operation mode of the instance is the same as the operation mode to be started, turn off the instance of this run. It is clear that this approach is not feasible by app.previnstance relying on VB. Because the purpose we have to achieve is to detect the previous instance to turn off it and start the program. App.PREVINSTANCE properties can only return an instance that is currently started and cannot do for the previous instance. (Examples are simply that the same object collection - the same program.) Introducing three API functions to you before implementing this method: getClassName, FindWindow, and SendMessage.

The prototype is as follows: Declare Function GetClassName Lib "user32" Alias ​​"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function FindWindow Lib "user32" Alias ​​"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Declare Function SendMessage Lib "user32" Alias ​​"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long GetClassName made for the class name of the form. Returns the length of the class name after the call is successful, and the failure returns zero. The function requires three parameters: parameter one. Handle of the form, parameter 2. Store the buffer of the class name, the parameter. The size of the buffer. FindWindow is used to look for a form. After the call is successful, return the handle of the form, and the failed returns zero. The function requires two parameters: parameter one. The class name of the form, the parameter. The title of the form. SendMessage is used to send a message to the form. The function requires four parameters: parameter 1. Handle of the form, parameter 2: Send message name, parameter three, four. Separate the parameters accompanying the message. Use these three functions to easily achieve a start-up instance before shutting down to achieve our goal. Second, we have to implement how to make the screen saver in the preview box ("Small Screen"). To make the screen saver display the style that must be dynamically changing the windows in the preview box makes it a "small screen" sub-form, so that the preview box is closed when the preview is closed. Dynamically change the style of the window to use GetWindowlong, SetWindowlong, and SetParent. Their prototype is as follows: Public Declare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, Byval Dwnewlong As Long AS Long Declare Function SetParent lib "User32" (Byval HwndNewParent As Long) is used to get the style of the form. Return to the form of the form after the call is successful. The function requires two parameters: parameter one. The handle of the form, the parameter. The style to obtain the form is only necessary to use the constant GWL_Style. SetWindowlong's role is used to set the style of the form. The function requires three parameters: parameter 1. Handle of the form, parameter 2. To set the form of the form, simply use the constant GWL_Style, the parameter. To set the style of the form. SETPARENT's role is used to set which parent form belongs to the subform. The function requires two parameters: parameter 1. Subform handle, parameter 2. The handle of the parent form. I know that the above two points can be written in standard screensavers. (About the effect, look at your own!) The paper is talking about a big programming. In order to focus on the implementation of the screening, the effect of the screen saver is simplified.

First, create a window to add a window, each property setting is as follows: Window Name Caption Border Form1 FRM_SETUP None 1 - None Form2 FRM_RUN Any 1 - Fixed Single The rest of the property is default. Add a TIMER control again in frm_run, change the name of the control to Timer_MOV, and the interval property is changed to 500. Add two modules, change the name of Module1 to mod_const, Module2 name change to mod_main, add the following code: MOD_CONST: OPTION Explicit public const wm_look = "Screensaver Preview (DEMO)" public const wm_set = "Screensaver setting (DEMO)" public Const WM_RUN = "screensaver is running (demo)" public Const HWND_TOP = 0 & public Const WS_CHILD = & H40000000 public Const GWL_STYLE = (- 16) Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type public Const SWP_NOZORDER = & H4 Public const swP_noactivate = & h10 public const swP_showwindow = & h40 public const wm_close = &

H10 Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Declare Function GetClassName Lib "user32" Alias ​​"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function FindWindow Lib "user32" Alias ​​"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Declare Function SendMessage Lib "user32" Alias ​​"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Public Declare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfte R As Long, Byval X As Long, Byval Y As Long, BYVAL WFLAGS As Long AS Long Declare Function Showcursor LIB "User 32" (BYVAL BSHOW AS Long) As long_main: Option Explicit Sub main () 'Program Run Entry DIM ClassName AS String * 64' Storage window Class name DIM EXECMD AS STRING 'Store command line parameters getClassName frm_setup.hwnd, classname, 64' get the class name of the window EXECMD = ucase (Command $) 'Convert the parameters of the call to the parameters to store the Variable Execmd if not (Instr (execmd, "/ p") = 0) THEN' Check the screen save the call parameter in the call parameter in the "/ P" parameter if not (FindWindow (ClassName, WM_LOOK = 0) THEN END 'If you find an instance of the same running method exists, the program endsprewndow classname, wm_set' closes the first-lapped other running mode, WM_Run 'is the upper SCR_LOOK ELSEIF NOT ( INSTR (execmd, "/ s") =

0) Then If Not (FindWindow (ClassName, WM_RUN) = 0) Then End ClosePreWindow ClassName, WM_LOOK 'supra ClosePreWindow ClassName, WM_SET' supra Scr_Run Else If Not (FindWindow (ClassName, WM_SET) = 0) Then End ClosePreWindow ClassName, WM_LOOK ' Ibid ClosePreWindow ClassName, WM_RUN 'ibid Scr_Setup End If End Sub Public Sub ClosePreWindow (ClassName as String, WinCaption as String) Dim PreWnd as Long PreWnd = FindWindow (ClassName, WinCaption)' seeking class called ClassName, titled If Not WinCaption window (PreWnd = 0) then Call SendMessage (PreWnd, WM_CLOSE, 0, 0) 'If the window is found close it End Sub Public Sub SCR_Look () Dim LookScrWnd As Long Dim Style As Long Dim LookRect As RECT Frm_Run.Caption = WM_LOOK' = = VAL (Right (Command $, LEN (COMMAND $) - 2)) 'Number of window handle style = getWindowlong (frm_run.hwnd, gwl_style)' STYLE = STYLE or WS_CHILD 'Adds a child form constant setWindowlong fm_run.hwnd, gwl_style, style' changes the form of the form set set set of the form of the form of the form of the form of the form of the form of the form of the form of the form set. lientRect LookScrWnd, LookRect 'made small screen size SetWindowPos Frm_Run.hwnd, HWND_TOP, 0, 0, LookRect.Right, LookRect.Bottom, SWP_ NOZORDER Or SWP_NOACTIVATE Or SWP_SHOWWINDOW' form and display size to form a small screen Size in order to cover small screen END SUB PUBLIC SUB SCR_SETUP () FRM_Run.caption = WM_SET '..... S s 标 标 标 标 标 标 标 标 标 标 标ShowCursor False 'hide the mouse Frm_Run.Move 0, 0, screen.Width, screen.Height Frm_Run.Show End Sub Public Sub CloseSCR () ShowCursor True' display mouse unload Frm_Setup 'unload the form close the screensaver unload Frm_Run' Ibid End Sub Public Function Scan_run () as boolean 'detects the operating mode IF of the current screen saver (frm_run.caption =

WM_RUN) THEN 'If the screen saver is running in operation, returns "true", otherwise returns "false" scan_run = true else scan_run = false end if end function frm_run: Option Explicit Dim i as integer' Defines the loop variable DIM Oldx as integer 'Defining the Save Old Mouse Level Coordinate DIM Oldy AS Integer' Defines the Depreciated Mouse Vertical Coordinate DIM PIC (1) AS New Stdpicture 'Defines an Array Private Sub Form_KeyDown (Keycode As Integer, Shift as Integer) if MOD_MAIN. Scan_run dam, if it is running the screen saver, shut down the screen save mod_main.closec end if End sub private subform_load () i = 1 'for the cyclic variable assignment OLDX = -1' for the old mouse level, the initial value Oldy = -1 'For the old mouse vertical coordinate standard set pic (0) = loadingPicture (please write a picture one path and name)' Read Picture One SET PIC (1) = LoadPicture (please write to a picture 2 path and Name) 'Read Picture Two Sub Private Sub Form_Mousedown (Button As INTEGER, SHIFT AS INTEGER, X as Single, Y A SINGLE) IF MOD_MAIN.SCAN_Run THEN' If this is running screen saver, turn off screen save mod_main.closeSCR END If End Sub Private Sub Form_Mousemove (Button As Integer, Shift As Integer, X as Single, Y As Single) IF MOD_MAIN.SCAN_RUN THEN IF (Oldx = -1) and (ildy = -1) Then Oldx = x ildy = y elseIF (Scalex (X-OLDX), VBTWIPS, VBPIXELS> = 3) Then MOD_MAIN.CLOSESCR 'Reduces the current horizontal coordinates and vertical coordinates of the mouse to the horizontal coordinates of the old mouse, if it is more than 3 Pixel exit screenker end if end if end if End sub private subform_unload (Cancel as integer) mod_main.closeSCR 'Close screen save End sub private sub timer_mov_timer () if (i> = 2) Then i = 1' If the loop variable is greater than The number of pictures variables are assigned to 1 else i = i 1 'Otherwise, the cyclic variable plus one end if frm_run.paintPicture Pic (I-1), 0, 0, Width, Height, 0, 0, Scalex (PIC (I-1) .Width, vbhimetric, vbtwips, scaley (PIC (I-

转载请注明原文地址:https://www.9cbs.com/read-5437.html

New Post(0)